Different ways to define symbolic constants in C    Caption

  • Traditional way to define symbolic constants (using C pre-processor):

      #define   PI   3.14159 ; 

  • Alternate way to define symbolic constants:

      int const  PI = 3.14159  

  • The keyword const specifies that a variable cannot be modified:

       PI = 4 ; // Will cause a compile error

Example showing the effect of the keyword const

 

  • Program showing that assignment to a const qualified variable is illegal:

       int main(int argc, char *argv[])             
       {
          int const a = 4;   // Must initialize a constant at definition 
        
          printf (" a = %d\n", a);
        
          a = 1;         // Compile error - illegal !!!  
        
          printf (" a = %d\n", a);
       } 

DEMO: demo/C/set1/const1.c