**Questions on Constants in C Language**

**1. What is a constant?**

Constant in C language is that item whose value remains same during the execution of the program from the beginning till the end of a program.

Constant may be a numeric constant or an unknown numeric constant. Although a constant's value is specified only once, a constant may be referenced many times in a program. Using a constant instead of specifying a value multiple times in the program can not only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings to a standard code location (for example, at the beginning).

2. How to declare and use constants?

2. How to declare and use constants?

The keyword const can be added to the declaration of an object to make that object a constant rather than a variable.

The general syntax of constant declaration is:

Syntax: const data-type name = value;

Here const is known as a keyword that must be used to declare a constant. Name is the name of the constant, data-type is the type of the data which we are declaring and value is the constant value of the data-type.

For example: const int manager_id=001;

Here we declared a constant named as manager_id of type integer which is assigned a value 001.

There are two simple ways in C to define constants:

• Using #define preprocessor.

• Using const keyword.

The #define Preprocessor: Following is the form to use #defines preprocessor to define a constant:

#define identifier value

For example:

• #define FIRST_NUMBER 1

The const Keyword: You can use const prefix to declare constants with a specific type as follows:

const type variable = value;

For example:

• const float pie =3.147;

• const int radius =4;

• const char c = 'A';

**3. What is the benefit of using #define to declare a constant?**

Using the #define method of declaring a constant enables you to declare a constant in one place and use it throughout your program. This helps make your programs more maintainable, because you need to maintain only the #define statement and not several instances of individual constants throughout your program.

For instance, if your program used the value of pi (approximately 3.14159) several times, you might want to declare a constant for pi as follows #define PI 3.14159.

Using the #define method of declaring a constant is probably the most familiar way of declaring constants to traditional C Programmers. Besides being the most common method of declaring constants, it also takes up the least memory.

Constants defined in this manner are simply placed directly into your source code, with no variable space allocated in memory. Unfortunately, this is one reason why most debuggers cannot inspect constants created using the #define method.

4. What is the difference between Constants and Variables?

A constant, Variable or a keyword can be formed from any combination of Alphabets, Digits and Special Symbols. A constant is an entity whose value does not change throughout the program execution.

A variable is an entity whose value keeps on changing throughout the program execution. However, it’s not a rule that the value of the variable will change. A variable value might remain same throughout the program execution. However the main difference between variable and constant is that we can’t change the value of constant in between the program, but we can change the value of the variable during program execution.

Variable's syntax is: data_type variable_name;

Constant's syntax is: const data type constant_name=value;

Variables can be initialized after its declaration where as constants must be initialized at the time of their declaration otherwise they will take a garbage value.

Let us take an example:

We define a variable named x in a program. Let us say x=3.After sometime during execution we change the value of x to 6. Now, x=6.Here value of x is getting changed. But the value of 3 and 6 will never change. Hence, x is called a variable and 3 is called a constant.

5. What are the different types of Constants in C language?

There are four basic types of constants in C. They are:

• Integer constants

• Floating-point constants

• Character constants

• String constants

Integer and floating-point constants represent numbers. They are often referred to as numeric-type constants.

6. What are the rules to be applied to all numeric-type constants?

The following rule applies to all numeric type constants:

• Comma and blank spaces cannot be included within the constants.

• Constants can be preceded by a – or + sign, if desired. If either sign does not precede the constant it is assumed to be positive.

• The value of a constant cannot exceed specified minimum and maximum bounds. For each type of constant, these bound vary from one C compiler to another.

7. What are Integer constants in C language?

Integer constants are whole numbers without any fractional part. Thus integer constants consist of a sequence of digits. An "integer constant" is a decimal (base 10), octal (base 8), or hexadecimal (base 16) number that represents an integral value. Use integer constants to represent integer values that cannot be changed.

8. What are the rules for constructing Integer constants?

• An integer constant must have at least one digit.

• It must not have a decimal point.

• It can either be positive or negative.

• No commas or blanks are allowed within an integer constant.

• If no sign precedes an integer constant, it is assumed to be positive.

• The allowable range for integer constants is -32768 to 32767.

9. What are decimal integers?

A decimal integer constant consists of any combination of digits taken from the set 0 through 9. If the decimal constant contains two or more digits, the first digit must be something other than 0. The following are valid decimal integer constants.

Examples:

0

1

1234

-786

10. What are octal integer constants?

An octal integer constant can consist any combination of digits taken from the set 0 through 7. However, the first digit must be 0, in order to identify the constant as an octal number. The following are valid octal integer constants.

Examples:

0

01

0125

0555

11. What are hexadecimal integer constants?

A hexadecimal integer constant must begin with either 0x or 0X. It can then be followed by any combination of digits taken from the set 0 through 9 and A through F (either upper-case or lower-case). The following are valid hexadecimal integer constants.

Examples:

0X0

0x1

0XAB125

-0x555

12. What are unsigned and long integer constants?

Unsigned integer constants: Exceed the ordinary integer by magnitude of 2, they are not negative. A character U or u is prefixed to number to make it unsigned.

Long Integer constants: These are used to exceed the magnitude of ordinary integers and are appended by L.

For example,

50000U decimal unsigned.

1234567889L decimal long.

0123456L octal long.

0777777U octal unsigned.

13. What are real or Floating-point constants in C language?

A real or floating-point constant is a base-10 number that contains either a decimal point or an exponent or both. A floating-point constant can be written in two forms: Factorial form or Exponential form. A floating-point constant in a fractional form must have at least one digit each to the left and right of the decimal point. A floating-point in exponent form consists of a mantissa and an exponent. The mantissa itself is represented as a decimal integer constant or a decimal floating-point constant in fractional form. The letter E or e and the exponent follow the mantissa. The exponent must be a decimal integer. The actual number of digits in the mantissa and the exponent depends on the computer being used.

14. What are the Rules for constructing Real constants (Fractional Form)?

Rules for constructing Real constants (Fractional Form) are,

• A real constant must have at least one digit

• It must have a decimal point

• It could be either positive or negative

• If no sign precedes an integer constant, it is assumed to be positive.

• No commas or blanks are allowed within a real constant.

E.g.: +867.9, -26.9876, 654.0

15. what are the Rules for constructing Real constants (Exponential Form)?

Rules for constructing Real constants (Exponential Form) are,

• The mantissa part and the exponential part should be separated by the letter ‘e’

• The mantissa may have a positive or negative sign(default sign is positive)

• The exponent must have at least one digit

• The exponent must be a positive or negative integer (default sign is positive)

• The range of real constants in exponential form is -3.4e38 and +3.4e38

E.g.: +3.2e-4, 4.1e8, -0.2e+4, -3.2e-4

## 0 comments:

## Post a Comment