The Wayback Machine - https://web.archive.org/web/20240618020432/https://www.geeksforgeeks.org/diffference-define-const-c/
Open In App

Difference between #define and const in C?

Last Updated : 13 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

#define is a preprocessor directive. Data defined by the #define macro definition are preprocessed, so that your entire code can use it. This can free up space and increase compilation times.

const variables are considered variables, and not macro definitions. 
 

Long story short: CONSTs are handled by the compiler, where as #DEFINEs are handled by the pre-processor.

The big advantage of const over #define is type checking. #defines can’t be type checked, so this can cause problems when trying to determine the data type. If the variable is, instead, a constant then we can grab the type of the data that is stored in that constant variable. 

Since const are considered variables, we can use pointers on them. This means we can typecast, move addresses, and everything else you’d be able to do with a regular variable besides change the data itself, since the data assigned to that variable is constant.

In general, const is a better option if we have a choice and it can successfully apply to the code. There are situations when #define cannot be replaced by const. For example, #define can take parameters (See this for example). #define can also be used to replace some text in a program with another text. 

Let us see the differences in a tabular form -:

  #define const
1. #define is a preprocessor directive. Constants are used to make variables constant such that never change during execution once defined.
2. is used to define micro substitution Constants are also called literals.
3.

Its syntax is -:

#define token value  

Its syntax is -:

const type constant_name;

4. It should not be enclosed with a (;) semicolon It should be enclosed with a (;) semicolon

 


Similar Reads

Difference between const int*, const int * const, and int const *
int const* int const* is pointer to constant integer This means that the variable being declared is a pointer, pointing to a constant integer. Effectively, this implies that the pointer is pointing to a value that shouldn't be changed. Const qualifier doesn't affect the pointer in this scenario so the pointer is allowed to point to some other addre
3 min read
Difference between const char *p, char * const p and const char * const p
Prerequisite: Pointers There is a lot of confusion when char, const, *, p are all used in different permutations and meanings change according to which is placed where. Following article focus on differentiation and usage of all of these. The qualifier const can be applied to the declaration of any variable to specify that its value will not be cha
3 min read
“static const” vs “#define” vs “enum”
In this article, we will be analyzing "static const", "#define" and "enum". These three are often confusing and choosing which one to use can sometimes be a difficult task. static const static const : "static const" is basically a combination of static(a storage specifier) and const(a type qualifier). Static : determines the lifetime and visibility
4 min read
Why can't a const variable be used to define an Array's initial size in C?
What is an array? An array is a collection of items of the same data type stored at contiguous memory locations. This makes it easier to calculate the position of each element by simply adding an offset to a base value, i.e., the memory location of the first element of the array (generally denoted by the name of the array). The base value is index
3 min read
Function overloading and const keyword
Function overloading is a feature of object-oriented programming where two or more functions can have the same name but different parameters. When a function name is overloaded with different jobs it is called Function Overloading. In Function Overloading “Function” name should be the same and the arguments should be different. Function overloading
5 min read
C++ | const keyword | Question 1
Predict the output of following program #include <iostream> using namespace std; int main() { const char* p = "12345"; const char **q = &p; *q = "abcde"; const char *s = ++p; p = "XYZWVU"; cout << *++s; return 0; } (A) Compiler Error (B) c (C) b (D) Garbage Value Answer: (B) Explanation: Output is ‘c’ c
1 min read
C++ | const keyword | Question 2
In C++, const qualifier can be applied to 1) Member functions of a class 2) Function arguments 3) To a class data member which is declared as static 4) Reference variables (A) Only 1, 2 and 3 (B) Only 1, 2 and 4 (C) All (D) Only 1, 3 and 4 Answer: (C) Explanation: When a function is declared as const, it cannot modify data members of its class. Whe
1 min read
C++ | const keyword | Question 3
Predict the output of following program. #include <iostream> using namespace std; class Point { int x, y; public: Point(int i = 0, int j =0) { x = i; y = j; } int getX() const { return x; } int getY() {return y;} }; int main() { const Point t; cout << t.getX() << " "; cout << t.getY(); return 0; } (A) Garbage Value
1 min read
C++ | const keyword | Question 5
#include <stdio.h> int main() { const int x; x = 10; printf("%d", x); return 0; } (A) Compiler Error (B) 10 (C) 0 (D) Runtime Error Answer: (A) Explanation: One cannot change the value of 'const' variable except at the time of initialization. Compiler does check this.Quiz of this Question
1 min read
C++ | const keyword | Question 5
Output of C++ program? #include <iostream> int const s=9; int main() { std::cout << s; return 0; } Contributed by Pravasi Meet (A) 9 (B) Compiler Error Answer: (A) Explanation: The above program compiles & runs fine. Const keyword can be put after the variable type or before variable type. But most programmers prefer to put const ke
1 min read