Constants and Literals

A constant is a container to store a value. But you can’t change the value of that container during the execution of the program. Thus, the constant is a fixed value, it remains the same through the complete program.

There are two broad categories of constant in C language:


Constants and Literals in C Programming


Literal constant:

Literal constants are just fixed values that may not change during its execution. Depending of the type of data,there are different types of literals in C language:

  1. Integer literal constant
  2. Character literal constant
  3. Floating point literal constant
  4. String literals

Integer literal constant

An integer constant is an integer valued number e.g 10, 20, -7, 50 etc.

Thus it consists of a sequence of digits. The Integer constant can be written in three different number system:

  1. Decimal  constant
  2. Octal constant
  3. Hexadecimal constant

Decimal constants can consist of any combination  of digits from 0 to 9 and if the literal contains two or more digits (means more than ones number), the first digit must be something other than 0.Some valid and invalid decimal literal constants are given below:

1           /* legal decimal constant */
134        /* legal decimal constant */
78          /* legal decimal constant */
0            /* legal decimal constant */
12,345    /*illegal decimal constant */
0900       /* illegal  for decimal constant */


Octal constants can consist of a combination of digits from 0 to 7 and the first digit must be 0 to recognize the constant as an octal constant.Some valid and invalid octal constants:

03          /* legal octal constant */
0761      /* legal octal constant */
098        /* illegal octal constant with digit 8 and 9*/
56          /*  illegal octal constant: does not begin with 0 */


Hexadecimal constants  can consist of any combination from 0 to 9 and  from A to F( either uppercase or lowercase). A hexadecimal integer must begin with either 0x or 0X.

Note that the letters A to F represent the decimal number 10 to 15 respectively.

Some valid and invalid hexadecimal constants:

0x1        /* legal hexadecimal constant */
0x7A      /* legal hexadecimal constant */
0x12G    /* illegal hexadecimal constant */


Note:  We can use integer literal with  a suffix  of U and L, for unsigned and long, respectively. The suffix can be lowercase or uppercase.


20U         /* legal unsigned constant */
123786L  /* legal long integer constant */
0xFeeL     /* legal long hexadecimal constant /*
0562356L /* legal long octal constant /*

Floating point constants

Floating point constants consist of numbers from 0 to 9 with combination of either a decimal point or an exponent. Some valid and invalid floating point constants:

0.2            /* legal floating point constant */
0.007e-3    /* legal floating point constant */
.89            /* legal floating point constant */
2E-8          /* legal floating point constant */
3E 10        /* illegal floating point constant: blank space in exponent */

It should be understood  that floating point constants are approximations, whereas integer constants are exact values.

Character constants

A character constant is a single character, enclosed in apostrophes sign (simply single quotation mark). It will be char type data.

Some valid and invalid character constants:

'T'      /* legal char constant */
'3'      /* legal char constant */
'R'      /* legal char constant */
'$'      /* legal char constant */
"TR"     /* illegal char constant: with single quotes */

String constants

A String constant consists of any number of consecutive characters( including none) enclosed in double quotes. Some valid string literals are given below:

"Hello"              /* legal string constant */
"12-45-h-234"        /* legal string constant */

We can use string with escape sequences:For example:

"\t hello"     /* legal string  constant with horizontal tab*/

The compiler automatically places a null character at the end of every string in C language. This is not visible when the string is displayed; it is represented by ‘/0’.

Symbolic constant

A symbolic constant is a name that refers  to a sequence of characters. It can be a numeric constant or a character constant or a string constant. Symbolic constants can be defined by the user.

In C language, there are two ways to define symbolic constant.

  1. Using  # define preprocessor
  2. Using const keyword

Syntax for # define preprocessor -:

#define <variable name>  value

When a program is compiled each occurrence of symbolic constant # define is replaced by its corresponding data.

Let us understand this through a program:

#define CONSTANT 45
int main( )
    int x=20, result;
    result= x*CONSTANT;
    printf("%d", result);
    return 0;

In the above program, during the compilation CONSTANT will be replaced by its value which is 45.

result = x * 45;

Symbolic constant # define usually defined at the beginning of the program and the definition do not end with semicolon.

Syntax for const keyword -:

const <data type> <variable name> = value;

We have to use const  as a prefix to declare a constant in c language. For example :

int  main( )
    const int v=89;
    printf("%d", v);
    printf("%d", v);
    return 0;

The above given program will raise an error called  “assignment of read-only variable `v`.