Open In App
Related Articles

Difference between #define and const in C?

Improve Article
Save Article
Like Article

#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.

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

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 13 Jun, 2022
Like Article
Save Article
Similar Reads