Constants in C

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 14 Feb, 2023 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • What should we use void main() or int main() ?

    There is question that what should we use void main() or int main() ? void main() { /* ... */ } Or, int main() { /* ... */…

    2 min read
  • C Comments

    Prerequisite – C Language Introduction C comments explain code and improve readability. These do not affect program execution. You can use comments in C to clarify code and describe…

    1 min read
  • C Programming Language Standard

    Prerequisite – C Language Introduction The C programming language has various versions: C89/C90, C99, C11, and C18. C89/C90: Released in 1989/1990. It introduced key language features. C99: This…

    2 min read
  • Features of C Programming Language

    Prerequisite – C Language Introduction C is a simple language made in 1972 by Dennis Ritchie. It’s for system programming. C has low-level memory access, basic keywords, good…

    1 min read
  • C Language Introduction

    C is a programming language developed by Dennis Ritchie in 1972. It has a significant historical impact. It was originally created at Bell Laboratories of AT&T Labs for…

    4 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read