Constants and Literals in C (const and #define) Explained
Learn how to use constants and literals in C programming. Discover the difference between the const keyword and #define macros for writing secure, bug
Constants and Literals in C (const and #define) Explained
Introduction In Chapter 6, we learned that a variable is a storage box in memory whose contents can change (vary) while the program runs. But what happens when you have a value that should never change? Imagine you are writing software for a bank to calculate compound interest. The number of months in a year is exactly 12. If another programmer accidentally writes code that changes the number of months to 13, the entire financial system will produce catastrophic errors. To prevent these disasters, software engineers use Constants . In this chapter, we will learn how to lock our variables so they cannot be tampered with. We will explore the differences between "Literals," the const keyword, and the powerful #define macro. What are Literals in C? Before we define constants, we must understand literals. A Literal is raw data written directly into your code. It is a value that cannot be changed because it is hardcoded. Integer Literal: 100 , -45 Floating-Point Literal: 3.14 , 9.81 Character …
About the author
Jayanta Mondal is a BCA student, web developer, and the founder of NeoGyan. He is passionate about simplifying complex tech concepts for beginners.