C constants with example

C Constants

What is Constants:

C constants are nothing but the one type of variables which values cant be change during the program execution. C constants  treat like variables but key difference is that its value doesn’t change.

Like variables c constants can be of many data types such as int,float,char etc.

C defines  some rules to define constants these are given below-

Rules to for naming c constants:

  1. A constants name can have any letters , digits and underscore .

  2. The first letter of constants must be  a letter .

  3. Generally it is good programming  to type constants name in upper case(capital letters). But however there is no restriction to type in uppercase.

  4. Upper case and lower case treated as different. It is case secretive.
In c we can define constants in many ways. These are given below

Defining constant using const Keyword:

By using const keyword we can declare constant. Const keyword is used as prefix,


const data type  constant_name;


for example:

#include <stdio.h>
int main() {
const int  NUMBER1= 20;
const int  NUMBER2 = 10;
const char NEW_LINE = '\n';
int addition;

 addition = NUMBER1+NUMBER2;
printf("addition is : %d", addition);
printf("%c", NEW_LINE);
return 0;


Addition is:30

Defining constant using #define

In c it is an also simplest way to define constant using the #define keyword. This type of constant doesn’t need to declare their data type. The are declared after the declaring header files.



#define constant_name value


For example:

#include <stdio.h>

#define NUMBER1 10  

#define NUMBER2  20

#define NEW_LINE '\n'

int main()


int addition; 


printf("Addition is : %d", addition);

printf("%c", NEW_LINE);

return 0;




Addition is:30

Literals in C Language:

Following are the different literals which can be used as constants.

1. Integer literals

Integer literals are the literals which only stores the numerical values without any floating point.integer literals are of three types:

  • decimal
    :  decimal literals means
    which numbers has base 10. For ex. 22,-9 25 etc.

  • octal literals:   octal literals having
    base 8. These literals start with 0. For ex. 0236,023,014 etc.

  • hexadecimal  literals:  hexadecimal literals hiving base 16.  These literals start with 0x or 0X.

2. Floating-point literals

A floating point literal is nothing but numeric value that has either a fractional form or an exponent form. For 




3. Character literals

A character literal is nothing but  constant which having single quotation around
single character. For example: 'M', 'l', 'Y', 's'

Enumeration constants

 This is another most important type of constants in c.enum keyword is used to define enumeration types constants .

 For example:

enum colors {yellow, blue, black,

Here, colors is a variable  of enumeration type and yellow, blue, black, pink are the enumeration constants having value 0, 1, 2 and 3.

Post a Comment

Previous Post Next Post