Constants play a major role during the static initialization of variables. In C, Constants are roughly divided into 3 types:

• Numeric constants
• Character constants
• String constants

## Numeric Constants:

Numeric constants are categorized into:

• Integer constants
• floating-point constants

## Integer constants:

Integer constants are the integer values that are used to perform operations with integer variables.

Based on their usage integer constants are divided into 3 types

• Decimal
• Octal

In Decimal constants, each digit varies from 0 to 9 i.e (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)

• Type specifier for the decimal numbers is %d.

Examples:

In Octal constants, each digit varies from 0 to 7  i.e (0, 1, 2, 3, 4, 5,  6, 7).

• The first digit in an octal number should be 0.
• Each digit represents a 3-bit value.
• Type specifier for an octal number is %o.

Examples:

0123   0234   0543

In Hexadecimal constants, each digit varies from 0 to F i.e (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F).  10 to 15 are considered as A to F.

• Each digit represents a 4-bit value.
• Type specifier for hexadecimal is %x.

Examples:

0x123   0xdddf   0x15daf

Example program with decimal, octal and hexadecimal integers.

#include <stdio.h>

int main()
{
int a = 10;
int b = 012;
int c = 0xA;
/*
%d specify the number as decimal integer
%o specify the number as octal integer
%x specify the number as hexadecimal integer
*/
printf(" In decimal = %d, In octal = %o, In Hexadecimal = %x ", a,b,c);
return 0;
}

Note: Just type the above code in your VIM editor,  execute it and see the output.

### Output:

In decimal = 10, In octal = 12, In Hexadecimal = a

## Floating-point constants:

The decimal (.) point in a numeric value represents the value as a floating-point constant.

Important Points:

• In order to represent the value as a floating-point constant, we need to suffix the value with 'f' or 'F'.
• If we won't suffix with 'f' or 'F' then it will be considered by default as double constant.
• If the value exceeds the range of 'float' and 'double' then we need to suffix with l or L to represent it as 'long double'.

Example:

• Decimal value = 23456
• Floating point value = 23.456f or 23.456F
• double  = 23.456
• long double = 3453453.4345345345334L
#include <stdio.h>

int main()
{
float a = 10.56f;     // or 10.56F
double b = 10.56;     // capable of storing more precision value
long double =10.56L;    // capable of storing more and more accurate precision
/*
%f is for float
%lf is for double
%Lf is for long double
*/
printf(" In float= %f, In double = %lf, In long double = %Lf ", a,b,c);
return 0;
}

Output:

In float= 10.56, In double = 10.56, In long double = 10.56

## Character constants:

A single character that is enclosed in a single quote is said to be a character constant. As we have seen in our previous article "ASCII TABLE" There will be an ASCII representation for each character in C.

Valid character constants:

• 'a'
• 'b'
• '5'

Invalid character constants:

• 'abc' (Should not contain more than one character.)
• ''  (Empty characters are not allowed).

Example program on character constants in C

#include <stdio.h>

int main()
{
char variable1 = 'a';
char variable2 = 97;
printf("Variable initialized with character a= %c \n", variable1);
printf("Variable initialized with its ASCII value a= %c", variable2);
return 0;
}

## Explanation:

• In the above example program, variable1 is initialized directly with the character 'a'. Variable2 is initialized with its ASCII value i.e. 97. (Please check ASCII table).

Output:

Variable initialized with character = a
Variable initialized with its ASCII value a= a

## String Constants:

A string constant contains one or more than one character in it (Including the NULL i.e. character ' \0' ). In string constant, characters are enclosed within double quotes.

Example:

• "Hello\0"  (Length of the string = no of characters i.e  6)
• "How are you\0" (Length of the string = 12)

Important points:

• The string constant always contains '\0' i.e a NULL the character at the end, which will be used to identify the end of the string. If you don't keep '\0', the compiler will automatically add '\0' at the end.
• The length of the string will be calculated including the' \0' character.
• Remember, always a character is enclosed within single quotes & the string is enclosed within double quotes.

## Example C program on string constants:

#include <stdio.h>

int main()
{
char *string = "Hello World\0";
/* %s is the type specifier used to specify the string */
printf ("The string is %s ", string);
return 0;
}

Output:

The string is Hello World!