Abstract
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); }
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Author information
Authors and Affiliations
Rights and permissions
Copyright information
© 2021 Slobodan Dmitrović
About this chapter
Cite this chapter
Dmitrović, S. (2021). Const Qualifier. In: Modern C for Absolute Beginners. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-6643-4_19
Download citation
DOI: https://doi.org/10.1007/978-1-4842-6643-4_19
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-6642-7
Online ISBN: 978-1-4842-6643-4
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)