Difference between #define and const in C Last Updated : 02 Jan, 2025 Comments Improve Suggest changes Like Article Like Report In C, both #define and const define constant values, but these constants differ greatly in their behaviors and implementation. #define is a preprocessor directive used to define constants that are replaced by their value during preprocessing, before actual compilation begins. while const defines a typed constant with type safety.The below table lists the primary differences between the constants defined using #define and using const keyword:Aspect#defineconstType SafetyNo type checking is performed and the constant defined acts as a literal.Enforces type checking on the constant.MemoryDoes not allocate memory for constant.Allocates memory for storage of constant.ScopeIts scope is global so it is available throughout the program.Follows block or function scope.DebuggingHard to debug as it is replaced during preprocessing.Easier to debug as it retains variable properties.ModifiersCannot use modifiers with it like staticCan use modifiers with it (e.g., static)EvaluationEvaluated at preprocessing.Evaluated at runtime.UsageIt is preferred for macros or constants without type.It is preferred for typed constants#define in C#define in C is a preprocessor directive that replaces a name with a specific value before compilation. It works like a shortcut, where the preprocessor substitutes the defined name with the value, and it doesn't have a type like regular variables.Example: C #include <stdio.h> // Define constant PI #define PI 3.14 int main() { printf("PI = %f\n", PI); return 0; } OutputPI = 3.140000 Explanation: Here, #define is used to define a constant PI. The preprocessor replaces PI with 3.14 before compilation, and the program behaves as if the literal value 3.14 was written directly in place of PI.When to Use #define?The use of #define is prefferend in the following cases:Constants that do not require a specific type.Simple text replacement or macros.Avoiding additional memory allocation.const in CThe const keyword is used to define constants with a specific type. Unlike #define, which is handled by the preprocessor, const is part of the C language and the compiler checks for type safety. A const variable behaves like any other variable, except its value cannot be modified after initialization. C #include <stdio.h> // Define constant PI const float PI = 3.14; int main() { printf("PI = %f\n", PI); return 0; } OutputPI = 3.140000 Explanation: In this case, const is used to define a constant PI with type float. The compiler ensures type safety, and the value of PI cannot be changed after it is initialized.When to Use const?Use const for:Typed constants that benefit from type checking.Constants with a defined scope.Improved readability and debugging. Comment More infoAdvertise with us Next Article C Preprocessor Directives K kartik Improve Article Tags : C Language cpp-macros C-Macro & Preprocessor Similar Reads C Preprocessors Preprocessors are programs that process the source code before the actual compilation begins. They are not part of the compilation process but operate separately, allowing programmers to modify the code before compilation. It is the first step that the C source code goes through when being converted 8 min read C Preprocessor Directives In C programming, the preprocessor is a program that process the source code before the actual compilation begins. It uses preprocessor directives are commands that instruct the preprocessor to perform specific actions. These directives start with the # symbol.List of Preprocessor DirectivesThe foll 6 min read How a Preprocessor works in C? Compiling a C program - Behind the Scene A Preprocessor is a system software (a computer program that is designed to run on computer's hardware and application programs). It performs preprocessing of the High Level Language(HLL). Preprocessing is the first step of the language processing system. Lan 3 min read Header Files in C In C programming, a header file is a file that ends with the .h extension and contains features like functions, data types, macros, etc that can be used by any other C program by including that particular header file using "#include" preprocessor.C language uses header files to provide the standard 5 min read Whatâs difference between header files "stdio.h" and "stdlib.h" ? In C programming, standard header files provide various inbuilt functionalities and two of the most commonly used standard header files are stdio.h and stdlib.h. The <stdio.h> provides Standard Input Output tools such as printf(), scanf(), etc while <stdlib.h> provides some commonly used 4 min read How to write your own header file in C? As we all know that files with .h extension are called header files in C. These header files generally contain function declarations which we can be used in our main C program, like for e.g. there is need to include stdio.h in our C program to use function printf() in the program. So the question ar 4 min read Macros and its types in C In C programming, a macro is a symbolic name or constant that represents a value, expression, or code snippet. They are defined using the #define directive, and when encountered, the preprocessor substitutes it with its defined content.ExampleC#include <stdio.h> // Macro definition #define LIM 4 min read Interesting Facts about Macros and Preprocessors in C In a C program, all lines that start with # are processed by preprocessor which is a special program invoked by the compiler. by this we mean to say that the â#â symbol is used to process the functionality prior than other statements in the program, that is, which means it processes some code before 5 min read # and ## Operators in C In C, # and ## operators are preprocessor operators using in macros for token manipulation. They are known as stringizing and token pasting operators and are used in macro definition with #define preprocessor. In this article, we will learn about these operators and how to use them in C programs.Str 3 min read How to print a variable name in C? Printing a variable name means printing the identifier that is assigned to the variable. To print it, it should be in the form of string. This can be done by using stringification.Stringification in C is the method to convert the argument of a function like macro to a string. This can be done with t 1 min read Like