In C Language, Constants are fixed values that do not change during the execution of a program.
Constants can be of various types and are used to represent fixed values such as numbers, characters, or strings.
There are several types of constants in C Language:
Integer constants are whole numbers without decimal points.
int num1 = 123; int num2 = -456; int num3 = 0;
float pi = 3.14; float smallNumber = -0.001; float bigNumber = 2.5e3; // 2.5 * 10^3
Floating-point constants are numbers with decimal points or in exponential notation.
char ch1 = 'A'; char ch2 = 'b'; char ch3 = '1';
char greeting[] = "Hello, C!";
Character constants represent individual characters enclosed within single quotes.
enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN }; enum Weekday today = WED;
#define PI 3.14159265359 #define MAX_SIZE 100
String constants are sequences of characters enclosed within double quotes.
const int ARRAY_SIZE = 10; const int MAX_VALUE = 100; int array[ARRAY_SIZE];
#include <limits.h> #include <float.h> int main() { printf("Maximum value of int: %d\n", INT_MAX); printf("Minimum value of int: %d\n", INT_MIN); printf("Maximum value of float: %f\n", FLT_MAX); printf("Minimum value of float: %f\n", FLT_MIN); return 0; }
Enumeration constants are identifiers that represent a set of named integer constants.
Symbolic constants are identifiers that represent fixed values. They are typically defined using the #define preprocessor directive.
Constant expressions are expressions composed of only constants and operators.
C provides some predefined constants through header files, such as "NULL", "EOF", "INT_MAX", "INT_MIN", "FLT_MAX", "FLT_MIN", etc.
Constants are used in C programs to improve code readability, eliminate magic numbers, and make it easier to maintain and modify the code.
Using constants also allows for easier changes to be made to the program's behaviour by modifying the constant's value at a single location.