# constant in c programming

A constant is an entity that doesn’t change.

In other words, a fixed value that may not be altered by a program during its execution is called as constant. A constant in c programming is also called as literal. Moreover, you can assume a constant as a regular variable whose value can not be modified after its definition. Any basic data type like a floating constant, an integer constant, a character constant or a string literal can be a constant in c programming. By the way, in the C language enumeration constant also exist.

There are two types of a constant in c programming namely

• Primary Constants
• Secondary Constants

### Primary Constants

Primary constants are also known as fundamental constants or basic constants. Primary Constants are of three types namely

• Integer Constant
• Real Constant
• Character Constant

### Integer Constant

All numbers either positive or negative without the decimal point are an integer constant. It can be a decimal, octal, or hexadecimal constant. An integer constant in c programming might have a prefix that specifies the base or radix : 0 for octal, 0x or 0X for hexadecimal.

An integer constant in c programming can also have a suffix that is a combination of U (unsigned ) and L (long). The suffix of an integer constant can be uppercase or lowercase and can be in any order.

Have You enjoyed the article on constant in c programming!

### Rules For Constructing Integer Constants

You must follow these steps to construct an integer constant in c programming.

• An integer constant must have at least one digit.
• It must not have a decimal point.
• An integer constant can be either positive or negative.
• If no sign precedes then integer constant is assumed to be positive.
• No blank space or commas are allowed within an integer constant.
• Mostly the allowable range for integer constant is -2147483648 to +214748647. However, the range of an integer constant depends upon the compiler. For compilers like visual studio, gcc, the range mentioned above is true whereas for compilers like turbo the range is -32768 to +32767.

For example, 35, -20, 0, 123, 0xStudent, 314u are valid integer constant.

`21                     /* Valid*/251u                   /* Valid*/0xStudent              /* Valid*/85                     /* decimal */0213                   /* octal */0x4b                   /* hexadecimal */30u                    /* unsigned int */30l                    /* long */30ul                   /* unsigned long */078                    /*  Invalid: 8 is not an octal digit */ 032UU                  /* Invalid: cannot repeat a suffix */`

### Real Constant

Real constants are often called Floating point constants. The real constants could be written in two forms Fractional form and Exponential Form. The exponential form is usually used if the value of the constant is either too small or too large. In addition, a floating-point constant has an integer part, a decimal point, a fractional part, and an exponent part.

### Rules for Constructing Real Constants (1)

Following rules must be especially observed while constructing real constant in c programming that is expressed in fractional form.

• A real constant must have at least one digit.
• It must have a decimal point.
• Likewise an integer constant, real constant could be positive or negative.
• Of course, the default sign is positive.
• Similar to an integer constant, in real constant also commas or blanks are not allowed.

For example,

`-60.40        /* Valid*/-50.455       /* Valid*/450.0         /* Valid*/+456.78       /* Valid*/`

### Rules for Constructing Real Constants (2)

Following rules must be especially observed while constructing real constant in c programming that is expressed in exponential form.

• The mantissa part may have a positive or negative sign.
• Moreover, default sign of mantissa is positive.
• Furthermore, the mantissa part and the exponential part should be separated by e or E.
• The exponent must have at least one digit, which may be positive or negative. However, the default sign is positive.

For example,

`-4.5e - 6        /* Valid */-7.0E + 3        /* Valid */+3,6E - 5        /* Valid */9.1e7            /* Valid */`

Have You enjoyed the article on constant in c programming!

### Character Constant

All character symbols are character constants if they are enclosed in a single quote and of unit length.

### Rules For Constructing Character Constants

• A character constant is a single alphabet, a single digit or a single special symbol enclosed within a single quote or inverted commas ‘ ‘.
• In addition, both inverted commas should point to left.

For example,

`'A'     /* Valid*/'5'     /* Valid*/'='     /* Valid*/`

### Secondary Constants

Secondary constants are also known as a derived constant in c programming. Because they are derived from primary constants.

Arrays, strings, pointers, structures, union, enumerators are secondary constants. Please, read about these in upcoming tutorials as these are very important and big topics. So, I have covered these topics separately.

### How to Define Constants?

You can define constants in two ways :

• Using #define preprocessor.

• Other is using,  const keyword.

### Using Preprocessor :

Syntax

`#define variable_name Variable_value`

Example

`#define age 30`

### Using const Keyword :

Syntax

`const data_type_of_variable variable_name = Variable_value;`

Example

`const int age = 30;`

<< Prev                                                                                                              Next >>