Responsive Ads Here

Constants & Literals in C

Constants are like normal variable, whose value cannot be altered in a program. Constants can be of any of the basic data types like an integer constant, a floating constant, a character constant, or a string literal. There are enumeration constants as well. Constants refer to fixed values. They are also called Literals.

Syntax : 
const data_type variable_name;

As mentioned, an identifier also can be defined as a constant.

const double PI = 3.14

Here, PI is a constant. Basically what it means is that, PI and 3.14 is same for this program.


Types of C Constants : 

  • Integer Constants
  • Floating-Point Constants
  • Character Constants
  • Escape Sequences
  • String Constants
  • Enumeration Constants

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.
  • There are three types of integer constants in C programming:

  1. decimal constant(base 10)
  2. octal constant(base 8)
  3. hexadecimal constant(base 16)


For example:

Decimal constants: 0, -9, 22 etc
Octal constants: 021, 077, 033 etc
Hexadecimal constants: 0x7f, 0x2a, 0x521 etc

Floating-Point Constants :

  • A floating-point Constants has an integer part, a decimal point, a fractional part, and an exponent part. 
  • Floating-point Constants are represented either in decimal form or exponential form.
  • It could be either positive or negative.

For example:

-2.0
0.0000234
-0.22E-5

Character Constants :

  • A character constant is a single alphabet, a single digit or a single special symbol enclosed within single quotes.
  • The maximum length of a character constant is 1 character.


For example: 

'a', 'l', 'm', 'F'


Escape Sequences :

  • There are some characters which have special meaning in C language.
  • They should be preceded by backslash symbol to make use of special function of them.
  • Given below is the list of special characters and their purpose.

Escape Sequences

Escape SequencesCharacter
\bBackspace
\fForm feed
\nNewline
\rReturn
\tHorizontal tab
\vVertical tab
\\Backslash
\'Single quotation mark
\"Double quotation mark
\?Question mark
\0Null character


String Constants :

  • String literals or constants are enclosed in double quotes "". 
  • A string contains characters that are similar to character literals: plain characters, escape sequences, and universal characters.


For example:

    "good"                  //string constant
    ""                      //null string constant
    "      "                //string constant of six white space
    "x"                     //string constant having single character.
    "Earth is round\n"      //prints string with newline


Enumeration Constants :


  • Keyword enum is used to define enumeration types. 
For example:


    enum color {yellow, green, black, white};

Here, color is a variable and yellow, green, black and white are the enumeration constants having value 0, 1, 2 and 3 respectively.