As the name suggests the name constants is given to such variables or values in C programming language which cannot be modified once they are defined. They are fixed values in a program. There can be any types of constants like integer, float, octal, hexadecimal, character constants etc. Every constant has some range. The integers that is too big to fit into an int will be taken as a long. Now there are various ranges that differ from unsigned to signed bits. Under signed bit the range of an int, varies from -128 to +127 and under unsigned bit int varies from 0 to 255.
How to define constants?
In C program we can define constants in two ways as shown below:
- using #define preprocessor directive
- using a const keyword.
Let us now learn about above two ways in details:
using #define preprocessor directive : This directive used to declare an alias name for existing variable or any value. We can use this to declare a constant as shown below:
#define identifierName value
where identifierName is the name given to constant and value is any value assigned to it.
Below is the C program to explain how to use #define to declare constants:
#define val 10
#define floatVal 4.5
#define charVal 'G'
"Integer Constant: %d\n"
"Floating point Constant: %f\n"
"Character Constant: %c\n"
Integer Constant: 10 Floating point Constant: 4.500000 Character Constant: G
Refer Macros and Preprocessors in C for details.
using a const keyword: Using const keyword to define constants is as simple as defining variables, the difference is you will have to precede the definition with a const keyword.
Below program shows how to use const to declare costants of different data types:
intVal = 10;
// int constant
floatVal = 4.14;
// Real constant
// char constant
// string constant
"Integer constant :%d \n"
, intVal );
"Floating point constant : %f \n"
, floatVal );
"Character constant : %c \n"
, charVal );
"String constant : %s \n"
Integer constant :10 Floating point constant : 4.140000 Character constant : A String constant : ABC
- String Literals : When string value is directly assigned to a pointer, in most of the compilers, it’s stored in a read only block (generally in data segment) that is shared among functions.
In the above line “GfG” is stored in a read only location, but pointer str is stored in a read-write memory. You can change str to point something else but cannot change value at present str. So this kind of string should only be used when we don’t want to modify string at a later stage in program.
The below program may crash (gives segmentation fault error) because the line *(str+1) = ‘n’ tries to write a read only memory.
/* Stored in read only part of data segment */
/* Problem: trying to modify read only memory */
Refer Storage for Strings in C for details
This article is contributed by Chinmoy Lenka. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Data type of character constants in C and C++
- Blog | Programming Guidelines
- AKTU (UPTU) Previous Year Solved Papers | C Programming
- OpenMP | Hello World program
- Ratio of the distance between the centers of the circles and the point of intersection of two direct common tangents to the circles
- Compare sum of first N-1 elements to Nth element of an array
- Check if given Preorder, Inorder and Postorder traversals are of same tree | Set 2
- Number of positions such that adding K to the element is greater than sum of all other elements
- getdate() and setdate() function in C with Examples
- Find number of magical pairs of string of length L
- Smallest and Largest sum of two n-digit numbers
- Find the Diameter or Longest chord of a Circle
- Determine the position of the third person on regular N sided polygon
- Predict the winner in Coin Game