C - Constants
C - Constants
C - Constants
A constant in C is a user-assigned name to a location in the memory, whose value
cannot be modified once declared. This is in contrast to a variable in C, which is
also a named memory location, however whose value may be changed during the
course of the code.
You can declare a constant in C program with either of the following two ways −
For example,
Example
https://www.tutorialspoint.com/cprogramming/c_constants.htm 1/5
6/16/24, 11:01 AM C - Constants
#include <stdio.h>
int main(){
const float PI = 3.14159265359;
float radius = 5;
float area = PI*radius*radius;
printf ("area: %f", area);
return 0;
}
Output
area: 78.539818
In case of variables, you can declare a variable and assign it a value later on in the
program, however you cannot follow the same process in case of a constant.
You can declare a constant in C without assigning it a value. But when you try to
assign it a value afterwords, then the compiler will throw an error.
https://www.tutorialspoint.com/cprogramming/c_constants.htm 2/5
6/16/24, 11:01 AM C - Constants
Note: "sizeof" returns "size_t". The type of unsigned integer of "size_t" can vary
depending on platform. And, it may not be long unsigned int everywhere. In such
cases, we use "%zu" for the format string instead of "%d".
This is because the compiler assigns a random garbage value at the time of
declaration, which you cannot change afterwards. Hence, you must declare and
initialize the constant at once.
A constant in C can be of any of the data types including primary data types such
as int, float, char, and derived data types such as struct.
#define PI = 3.14159265359
Although the constant so defined can also be used in any expression (just as the one
with the const keyword), there is a difference between the two.
The constants created by the #define directive are not handled by the compiler.
Instead, they behave as macros, whose values are substituted at the runtime.
The other notable difference is that you need not mention the data type of the value
to be assigned to the constant when using the #define directive.
Given below is another example of a constant defined using the #define directive −
#include <stdio.h>
#define LENGTH 10
#define WIDTH 5
#define NEWLINE '\n'
int main() {
int area;
area = LENGTH * WIDTH;
printf("length: %d width: %d", LENGTH, WIDTH);
https://www.tutorialspoint.com/cprogramming/c_constants.htm 3/5
6/16/24, 11:01 AM C - Constants
printf("%c", NEWLINE);
printf("value of area : %d", area);
return 0;
}
Output
Upon running this code, you will get the following output −
length: 10 width: 5
value of area : 50
We have seen that it is not possible to assign a new value to an already defined
constant. However, there exists a workaround with which a new value can be
assigned to a constant.
The technique uses the concept of pointers in C. A Pointer is a variable that stores
the address of another variable. Since it is a variable, its value can be changed.
Moreover, this change reflects in the original variable.
The following code demonstrates how to change the value of a constant with the
pointer mechanism −
#include <stdio.h>
int main(){
const int x = 10;
printf("Initial Value of Constant: %d\n", x);
https://www.tutorialspoint.com/cprogramming/c_constants.htm 4/5
6/16/24, 11:01 AM C - Constants
// y is a pointer to constant x
int* y = &x;
Output
Note that this technique is effective only for those constants which are defined
using the const qualifier.
If you have defined your constant using the #define directive, then you cannot
apply this process. This is because the pointer has a data type, and it must be of the
same type whose address is to be stored. On the other hand, the constant defined
using the #define directive doesn't really have a data type. It is in fact a macro
whose value is substituted during the runtime.
https://www.tutorialspoint.com/cprogramming/c_constants.htm 5/5