Difference between #define and const in C


#define and const both are used in C Programming Language for defining constants.

const vs #define in C

What is #define in C?

The #define directive is a preprocessor directive; the preprocessor directive replaces the macros by their body before the compilation is started.

Example of #define in C

#define SQR(x) (x)*(x)

Now in your c program if you use


This code would get converted into


What is const in C?

A const variable declaration actually declares an actual variable in the language, which you can use like a normal variable but its value cannot be changed once assigned. You can use const normally like another variable to pass it around to another variable, cast it to some another type, convert it, etc.

C const Example

int main() { const int width = 100; /*int constant*/ const float pi = 3.14; /*Real constant*/ const char some_letter = 'A'; /*char constant*/ const char letter_sequence[10] = "ABC"; /*string constant*/ const char backslash_char = '\?'; /*special char cnst*/ ... ... return 0; }

Difference between #define vs const

#define directive is a preprocessor directive. const variable is just like a normal variable in C language.
#define can only define simple constant. const can define complex constants too.
Syntax check will not be done for #define until the error macro is used in code block. Example:
#define SUM 10+ int main() { int a = SUM; // macro syntax error
Syntax check will be done for const
#define is not type-safe. const is type safe


Get Answers to your Programming Questions

Recommended Deals ends in

Quiz For You:

Search Tags

    const vs define in c

    What is the difference between const variable and define directive