- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
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.
Instead of repeatedly using hard-coded values in a program, it is advised to define a constant and use it. Constants in a C program are usually employed to refer to a value which may be error-prone if it is to be used repetitively in the program, at the same time its value is not likely to change.
For example, the value of mathematical constant PI is a high-precision floating point number 3.14159265359, and if it is likely to appear frequently, it is declared as a constant and used by its name.
We may consider a constant in C as a read-only variable, as its value can only be used subsequently but cannot be modified.
You can declare a constant in C program with either of the following two ways −
Using the const Keyword
Using the #define Directive
Let's understand these two ways of declaring a constant in C.
Defining a Constant Using the const Keyword
The syntax of declaring a constant is as follows −
const type NAME = val;
For example,
const float PI = 3.14159265359;
We can now use PI in any expression, as we would use any variable.
Example
Take a look at the following example −
#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
On running this code, you will get following output −
area: 78.539818
However, changing the value of a constant is prohibited. The following statement gives a compiler error −
const float PI = 3.14159265359; PI=22/7;
Here, you will get the following error message −
error: assignment of read-only variable 'PI'
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.
const float PI; PI = 3.14159265359;
Here, you will get this error message −
error: assignment of read-only variable 'PI'
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.
Defining a Constant Using the #define Directive
Using the #define preprocessor directive is also an effective method to define a constant. Here is its syntax −
#define name = value
Take a look at the following example:
#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.
Example: Define a Constant Using the #define
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); 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
Since a constant is also an identifier in C, it follows all the rules of forming an identifier. Identifiers in C are case-sensitive. Hence the convention followed while defining a constant in C is that it uses uppercase characters, however it is not mandatory.
Changing the Value of a Constant
By definition, constants are immutable. Why would you change the value of a constant in the first place? We use constants whose value is supposed to remain unchanged. To be able to change the value, we would define a variable rather than a constant.
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.
Example: Change the Value of a Constant
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); // y is a pointer to constant x int* y = &x; // assign new value *y = 100; printf("Value of Constant after change: %d", x); return 0; }
Output
On executing this code, you will get the following output −
Initial Value of Constant: 10 Value of Constant after change: 100
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.