© Slobodan Dmitrović 2021
S. DmitrovićModern C for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6643-4_19

19. Const Qualifier

Slobodan Dmitrović1  
(1)
Belgrade, Serbia
 
To make the variable a read-only, we apply the const qualifier to its declaration. Once initialized or assigned to, these variables become read-only. Attempting to change their values results in a compile-time error. Let us write an example that defines a few simple constants:
#include <stdio.h>
int main(void)
{
      const char c = 'a';
      const int x = 123;
      const double d = 456.789;
      printf("We have defined three constants.\n");
      printf("Their values are: %c, %d, %.3f.\n", c, x, d);
}
Output:
We have defined three constants.
Their values are: a, 123, 456.789.
This ...

Get Modern C for Absolute Beginners: A Friendly Introduction to the C Programming Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.