Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals.
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 are treated just like regular variables except that their values cannot be modified after their definition.
85 /* decimal */
0213 /* octal */
0x4b /* hexadecimal */
30 /* int */
30u /* unsigned int */
30l /* long */
30ul /* unsigned long */
3.14159 /* Legal */
314159E-5L /* Legal */
510E /* Illegal: incomplete exponent */
210f /* Illegal: no decimal or exponent */
.e55 /* Illegal: missing integer or fraction */
#include <stdio.h>
int main(>
{
printf("Hello\tWorld\n\n">
;
return 0;
}
When the above code is compiled and executed, it produces the following result −
Hello World
#define identifier value
The following example explains it in detail −
#include <stdio.h>
#define LENGTH 10
#define WIDTH 2
#define NEWLINE '\n'
int main(>
{
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area>
;
printf("%c", NEWLINE>
;
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of area : 20
const type variable = value;
The following example explains it in detail −
#include <stdio.h>
int main(>
{
const int LENGTH = 10;
const int WIDTH = 2;
const char NEWLINE = '\n';
int area;
area = LENGTH * WIDTH;
printf("value of area : %d", area>
;
printf("%c", NEWLINE>
;
return 0;
}
When the above code is compiled and executed, it produces the following result −
value of area : 20