C Programming

Constants With const

C provides another way to define the constants with keyword const. Any attempt to modify such variable in program, results into compilation error. The syntax for defining constant with const is as follows.

          const data_type constant_name = value;

where const is reserved word, data_type is the type of constant, constant_name is name of the constant just like variable and value denotes value of constant. The default data_type is int. Following declaration defines constant max with value 5.

          const int max = 5;

As default data type is int, the above statement is same as

          const max = 5;

Same way, we can create constant of any valid data type. Following statement declares the constant pie with value 3.14.

          const float pie = 3.14;

If you do not initialize the constant at the time of declaration, C automatically initializes it to 0. For example,

          const int count;

defines count as constant with value