Constants in C
As the name suggests the name constants is given to such variables or values in C/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 are too big to fit into an int will be taken as long. Now there are various ranges that differ from unsigned to signed bits. Under the signed bit, the range of an int varies from -128 to +127 and under the unsigned bit, int varies from 0 to 255.

Defining Constants:
In C/C++ program we can define constants in two ways as shown below:
- Using #define preprocessor directive
- Using a const keyword
Literals: The values assigned to each constant variables are referred to as the literals. Generally, both terms, constants and literals are used interchangeably. For eg, “const int = 5;“, is a constant expression and the value 5 is refered to as constant integer literal.
Let us now learn about above two ways in details:
-
Using #define preprocessor directive: This directive is 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
- identifierName: It is the name given to constant.
- value: This refers to any value assigned to identifierName.
Example:
C
#include<stdio.h>#define val 10#define floatVal 4.5#define charVal 'G'intmain(){printf("Integer Constant: %d\n",val);printf("Floating point Constant: %.1f\n",floatVal);printf("Character Constant: %c\n",charVal);return0;}chevron_rightfilter_noneC++
#include <iostream>usingnamespacestd;#define val 10#define floatVal 4.5#define charVal 'G'intmain() {cout <<"Integer Constant: "<< val <<"\n";cout <<"Floating point Constant: "<< floatVal <<"\n";cout <<"Character Constant: "<< charVal <<"\n";return0;}chevron_rightfilter_noneOutput:Integer Constant: 10 Floating point Constant: 4.5 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:
C
#include <stdio.h>intmain(){// int constantconstintintVal = 10;// Real constantconstfloatfloatVal = 4.14;// char constantconstcharcharVal ='A';// string constantconstcharstringVal[10] ="ABC";printf("Integer constant:%d \n", intVal );printf("Floating point constant: %.2f\n", floatVal );printf("Character constant: %c\n", charVal );printf("String constant: %s\n", stringVal);return0;}chevron_rightfilter_noneC++
#include <iostream>usingnamespacestd;intmain() {// int constantconstintintVal = 10;// Real constantconstfloatfloatVal = 4.14;// char constantconstcharcharVal ='A';// string constantconststring stringVal ="ABC";cout <<"Integer Constant: "<< intVal <<"\n";cout <<"Floating point Constant: "<< floatVal <<"\n";cout <<"Character Constant: "<< charVal <<"\n";cout <<"String Constant: "<< stringVal <<"\n";return0;}chevron_rightfilter_none
Output:Integer constant: 10 Floating point constant: 4.14 Character constant: A String constant: ABC
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 contribute@geeksforgeeks.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.
Recommended Posts:
- Constants vs Variables in C language
- Data type of character constants in C and C++
- Find the remainder when First digit of a number is divided by its Last digit
- Find the remaining balance after the transaction
- Count the number of occurrences of a particular digit in a number
- asctime() and asctime_s() functions in C with Examples
- Program to find the Nth Prime Number
- time.h header file in C with Examples
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Check whether Quadrilateral is valid or not if angles are given
- Find the sum of the first half and second half elements of an array
- Time taken per hour for stoppage of Car
- Check whether the triangle is valid or not if angles are given
- __builtin_inf() functions of GCC compiler



