Constants in C | Set 2

Last Updated :

Prerequisite – Constants in C
Constants refer to fixed values that the program may not alter during its execution. These fixed values are also called literals. Constants can be of any of the basic data types.

Types of Constant in C :

C supports several types of constants in C language as

  1. Character Constants, i.e., Single Character Constant, and String Constant.
  2. Numeric Constants :
    • Integer Constant, i.e., Decimal Integer constant, Octal integer constant, and Hexadecimal Integer constant.
    • Real Constant
  3. Backslash Character constants
  4. Symbolic constants

Defining Constants :

There are two simple ways in C to define constants − Using #define preprocessor, and Using const keyword.

  1. Using #define preprocessor directive –
    Given below is the form to use #define preprocessor to define a constant −

    #define identifierName value 
    • identifierName : It is the name given to constant.
    • value : This refers to any value assigned to identifierName.

    Example :

    #include <stdio.h>
     
    #define LENGTH 100   
    #define WIDTH  50
    #define NEWLINE '\n'
     
    int main() {
       int area;  
     
       //test these values 
       printf("Value of LENGTH is %d", LENGTH);
       printf("%c", NEWLINE);
       printf("Value of WIDTH is %d", WIDTH);
       printf("%c", NEWLINE);
    
       //check using calculation 
       area = LENGTH * WIDTH;
       printf("Value of area is %d", area);
     
       return 0;
    }
    

    Output :

    Value of LENGTH is 100
    Value of WIDTH is 50
    Value of area is 5000 
  2. Using const Keyword :
    You can use const prefix to declare constants with a specific type as follows −

    const type variable = value; 

    Example :

    #include <stdio.h>
    
    int main() {
    
       
    
       const int  LENGTH = 100;
       const int  WIDTH = 50;
       const char NEWLINE = '\n';
       
       //test these values 
       printf("Value of LENGTH is %d", LENGTH);
       printf("%c", NEWLINE);
       printf("Value of WIDTH is %d", WIDTH);
       printf("%c", NEWLINE);
       
       //check using calculation
       int area;  
       area = LENGTH * WIDTH;
       printf("value of area is %d", area);
       printf("%c", NEWLINE);
    
       return 0;
    }
    

    Output :

    Value of LENGTH is 100
    Value of WIDTH is 50
    value of area is 5000 



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.

Comment
Next Article
CPP_Programmer Published 17 Aug, 2020 · 2 min read

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