Constants in C language

Constants are defined as the fixed values in the program that do not alter its execution. They are also called literals. In general, constants can be different types like integer, float, character, string, etc. 

Constants are also variables that cannot be modified or changed. Let's explore more about the Literals or Constants.

 

Integer Literals

Integer literals can be identified as decimal, octal, or hexadecimal constant. It can be prefixes or suffixes. Here are some examples of integer literals.

 

45         /* decimal */

0412       /* octal */

1x4a       /* hexadecimal */

40         /* int */

20u        /* unsigned int */

10l        /* long */

35ul       /* unsigned long */

 

Floating-point Literals

Floating-point literals include a decimal point, fractional, exponent, etc. Make sure to include the decimal point while using the decimal form. Here are some examples of floating-point literals. The exponent is defined by e or E.

 

3.16248       /* Legal */

325159E-6L    /* Legal */

522E          /* Incomplete exponent */

241f          /* No decimal or exponent */

.e25          /* Missing integer or fraction */

 

Character Constants

Character constants are used in single quotes for example 'y' can be stored in a variable of character type. It can be a plain character or a universal character. 

 

String Constants

String literals are used in double quotes "". A string includes characters like plain characters, universal characters, etc. Also, you can break into multiple lines by using string literals. Here are a few examples of identical strings

 

"Hello, World"

 

"Hello, \

 

World"

 

"Hello, " "W" "orld"

 

Const Keyword

The const keyword is used in c language to define constants. Let’s know in brief about const with this program example.

 

 Program:

#include <stdio.h>

 

#define LENGTH 20   

#define WIDTH  10

#define createNEWLINE '\n'

 

int main() {

   int area;  

  

   area = LENGTH * WIDTH;

   printf("Initial value of area: %d", area);

   printf("%c", createNEWLINE);

 

   return 0;

}

 

Output:

Initial value of area: 200

 

Subscribe For Daily Updates