Constants in C

By | February 14, 2023

Constants in C are fixed values that cannot be changed during program execution. They are declared using the keyword “const” and can be of various data types such as integer, floating point, character, or string. Constants provide a way to assign meaningful names to fixed values in a program, making the code more readable and maintainable. Constants can be defined globally or locally, and they can be used to represent values that are used frequently in the program or to define parameters that are used in a calculation or algorithm.

Syntax :
In C programming language, constants are declared using the “const” keyword, followed by the data type and the name of the constant. The syntax of defining a constant in C is as follows:

const data_type constant_name = value;

Here, “data_type” represents the data type of the constant, such as int, float, char, or double. “constant_name” is the name given to the constant, and “value” is the value assigned to the constant. The value assigned to the constant must be a constant expression, which means that it cannot be changed during program execution.

For example, to define an integer constant named “MAX” with the value 100, the syntax would be:

const int MAX = 100;

Similarly, to define a string constant named “GREETING” with the value “Hello, world!”, the syntax would be:

const char* GREETING = "Hello, world!";

Note that the type of the constant must match the type of the value assigned to it, or the compiler will generate an error.

Constants in C with examples :
In C programming language, constants can be declared using the “const” keyword. Here are some examples of how to define constants in C:

  1. Integer Constants –

    // defining a constant integer named MAX with value 100
    const int MAX = 100; 
    
    // defining a constant integer named MIN with value 0
    const int MIN = 0;   
    
  2. Floating-Point Constants –
    // defining a constant float named PI with value 3.14159
    const float PI = 3.14159; 
    
    // defining a constant double named E with value 2.71828
    const double E = 2.71828; 
    
  3. Character Constants –
    // defining a constant character named NEWLINE with value '\n'
    const char NEWLINE = '\n'; 
    
    // defining a constant character named TAB with value '\t'
    const char TAB = '\t';     
    
  4. String Constants –
    // defining a constant string named GREETING with value "Hello, world!"
    const char* GREETING = "Hello, world!"; 
    
    // defining a constant string named NAME with value "John"
    const char* NAME = "John";              
    

Note that in the case of string constants, the pointer to the string is declared constant, not the contents of the string itself.

Read related article – Ways to define Constant in C

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.