Do you have use macro constants and constant variables in your code? If you’re reading this article, then it is likely that you answered yes.
I believe below questions definitely comes to your mind that are the followings.
- What is the difference between macro constants and constant variables in C?
- How do I best use the const keyword in C?
- What is the difference between #define and const in C?
- What are the advantages of using const variables over macros in C?
- Comparison of #define and const keyword in C?
- What are the benefits of using const variable instead of macros in C?
Don’t worry in this blog post we will answer the above-mentioned questions. We will also clear your doubts regarding the constant variable and macro constant with help of programming examples. If you are not familiar with these two fancy words, don’t worry before explaining the difference I will give a small introduction about the C macro (#define) and const qualifier. So, let’s get started,
👍 Directly jump to #define vs const.
What is the const keyword in C?
A const keyword is a type qualifier. It declares an object to be nonmodifiable. That means const qualifier specifies that a variable’s value is constant and tells the compiler to prevent the programmer from modifying it.
Consider the below example,
//iData without const int iData = 2; //iData is variable> modifiable iData = 3; // Ok
You can see the above code, in which I am able to change the value of the variable.
But what will happen if I use the const keyword with variable?
Yes, you are right!. I can’t change the value, see the following code.
#include <stdio.h> int main() { //const qualify object const int iData = 5; iData = 10; //error return 0; }
Output:
error: assignment of read-only variable ‘iData’
What is a macro in C?
Macros are handled by the pre-processor. We can categorize the C macros into two types, object-like macros (object macro could be multi-line), and function-like macros.
A macro is a segment of the code that has some unique name. Whenever in a program we used the macro name, it is replaced by the definition of the macro. In the C programming, we defined the macro by #define directive.
Consider the below example program,
#include <stdio.h> /* You can see semicolon (;) is not require. */ #define ATICLEWORLD 16 int main() { // Print the message printf("ATICLEWORLD is %d", ATICLEWORLD); return 0; }
Output:
ATICLEWORLD is 16
Now I believe you have a basic understanding of const and #define. But still, you have doubts then it is my recommendation to read these articles.
➤ What is a const qualifier in C?
Difference between macro constants and constant variables in C (#define vs const):
There are the following differences between const and macro in C:
➤ Const is a type qualifier while the macro is preprocessor directive.
➤ Const keyword is handled by the compiler, in another hand, a macro is handled by the preprocessor directive. The pre-processor does text replacement in your source file.
➤ Const is type-safe while #define (macro) is not.
➤ Const is scoped by C block, #define applies to a file (or more strictly, a compilation unit). See the below example,
Example 1:
#include <stdio.h> void test() { #define ATICLEWORLD 16 } int main() { // Print the message printf("ATICLEWORLD is %d", ATICLEWORLD); return 0; }
Output:
ATICLEWORLD is 16
The above code will compile successfully using the gcc compiler.
Now you are thinking how? Reason is that macro is globally accessible throughout the program after their definition. That means there is no concept of block-level scope for macro constants, and they replaced by the preprocessor before compilation.
Example 2:
You will get a compiler error.
#include <stdio.h> void test() { const int ATICLEWORLD = 16; } int main() { // Print the message printf("ATICLEWORLD is %d", ATICLEWORLD); return 0; }
Output:
‘ATICLEWORLD’ undeclared (first use in this function).
The above code gives the compiler error; reason is that const variable follow scope rule. The const variable ‘ATICLEWORLD’ follow the scope rule and accessible within the block (scope) where they are defined.
➤ Constant variables occupy the memory space while Macro constants do not occupy memory (reason is that they are text replacements performed by the preprocessor).
➤ Const cannot use as a case in the switch case statement while macro could be used.
➤The debugger’s symbol table contains a symbol for Const, which makes debugging easier. It is more likely that #define will not have a symbol, leaving you wondering what it is.
➤ Const can be passed as call by reference while constant macro could not.
➤ Const cannot be used as a dimension for arrays at global scope while #define can be used.
Example 1:
You will get the compiler error.
#include <stdio.h> //constant const int ARRAY_SIZE = 5; int arr[ARRAY_SIZE]; int main() { return 0; }
Output:
error: variably modified ‘arr’ at file scope
Example 2:
It will work perfectly.
#include <stdio.h> //macro #define ARRAY_SIZE 5 int arr[ARRAY_SIZE]; int main() { return 0; }
➤ Also, const cannot be used as a dimension for static arrays at function scope. Example,
#include <stdio.h> //macro const int ARRAY_SIZE = 5; int main() { static int arr[ARRAY_SIZE]; return 0; }
➤ Const can not use to initialize the static variable while using the #define we can initialize.
➤ Const could not disable the piece of code or functionality but macro could be because it handles by the preprocessor.
Recommended Post
- C Programming Courses And Tutorials
- CPP Programming Courses And Tutorials.
- Const qualifier in C.
- C Macros with example code.
- How to Write Multiline macro in C Language
- typedef vs #define in C.
- 100 C interview Questions.
- Important const qualifier interview Questions.
- Difference between const qualifier and volatile qualifier.