For working professionals
For fresh graduates
More
Explore C Tutorials: From Begi…
1. Introduction to C Tutorial
2. Addition of Two Numbers in C
3. Anagram Program in C
4. Armstrong Number in C
5. Array in C
6. Array of Pointers in C
7. Array of Structure in C
8. C Program to Find ASCII Value of a Character
9. Assignment Operator in C
10. Binary Search in C
11. Binary to Decimal in C
12. Bitwise Operators in C
13. Boolean in C
14. C Compiler for Mac
15. C Compiler for Windows
16. C Function Call Stack
17. C Language Download
18. Operators in C
19. C/C++ Preprocessors
20. C Program for Bubble Sort
21. C Program for Factorial
22. C Program for Prime Numbers
23. C Program for String Palindrome
24. C Program to Reverse a Number
25. Reverse a String in C
26. C string declaration
27. String Input Output Functions in C
28. Calculator Program in C
29. Call by Value and Call by Reference in C
30. Ceil Function in C
31. Coding Vs. Programming
32. Command Line Arguments in C/C++
33. Comments in C
34. Compilation process in C
35. Conditional Statements in C
36. Conditional operator in the C
37. Constant Pointer in C
38. Constants in C
39. Dangling Pointer in C
40. Data Structures in C
41. Data Types in C
42. Debugging C Program
43. Convert Decimal to Binary in C
44. Define And include in C
Now Reading
21 mins read
45. Difference Between Arguments And Parameters
46. Difference Between Compiler and Interpreter
47. Difference Between If Else and Switch
48. Do While Loop In C
49. Double In C
50. Dynamic Array in C
51. Dynamic Memory Allocation in C
52. Enumeration (or enum) in C
53. Evaluation of Arithmetic Expression
54. Factorial of A Number in C
55. Features of C Language
56. Fibonacci Series Program in C Using Recursion
57. File Handling in C
58. For Loop in C
59. Format Specifiers in C
60. Functions in C
61. Function Pointer in C
62. goto statement in C
63. C Hello World Program
64. Header Files in C
65. Heap Sort in C Program
66. Hello World Program in C
67. History of C Language
68. How to compile a C program in Linux
69. How to Find a Leap Year Using C Programming
70. Identifiers in C
71. If Else Statement in C
72. If Statement in C
73. Implementation of Queue Using Linked List
74. Increment and decrement operators in c
75. Input and Output Functions in C
76. How To Install C Language In Mac
77. Jump Statements in C
78. Lcm of Two Numbers in C
79. Length of an Array in C
80. Library Function in C
81. Linked list in C
82. Logical Operators in C
83. Macros in C
84. Matrix multiplication in C
85. Nested if else statement in C
86. Nested Loop in C
87. One Dimensional Array in C
88. Operator Precedence and Associativity in C
89. Overflow And Underflow in C
90. Palindrome Program in C
91. Pattern Programs in C
92. Pointer to Pointer in C
93. Pointers in C: A Comprehensive Tutorial
94. Pre-increment And Post-increment
95. Prime Number Program in C
96. Program for Linear Search in C
97. Pseudo-Code In C
98. Random Access Files in C
99. Random Number Generator in C
100. Recursion in C
101. Relational Operators in C
102. Simple interest program in C
103. Square Root in C
104. Stack in C
105. Stack Using Linked List in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
112. String Comparison in C
113. String Functions in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
118. Structure of C Program
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
122. Toupper Function in C
123. Transpose of a Matrix in C
124. Two Dimensional Array in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
129. User Defined Functions in C
130. What is Variables in C
131. Is C language case sensitive
132. Fibonacci Series in C
C is a statically typed, high-level, general-purpose language that provides a broad range of features, such as low-level access to memory, a simple set of keywords, and a clean style. It is known for its efficiency, versatility, and flexibility, which is why it's widely used in the development of software applications. Among its core features are the #define and #include preprocessor directives.
This article will explore these two directives, their roles, and how they function.
In order to simplify programming tasks and improve readability, the C programming language incorporates various tools and features. Two of these tools are the preprocessor directives: #define and #include.
The preprocessor is a program invoked by the compiler that manipulates the source code before the actual compilation starts. It operates under a set of directives, indicated by the # symbol. Among these directives are #define and #include, which perform unique roles.
The #define directive is used for creating symbolic constants and macros. Symbolic constants make programs more modifiable and comprehensible using representative names instead of hard-coded values. For example, we can define the constant PI as #define PI 3.14159. Conversely, macros are like mini-functions that expand through the preprocessor and can take arguments and return values, differing from regular functions.
The #include directive instructs the preprocessor to incorporate the content of another file into the current file. This other file is typically a header file (with a .h extension) containing function prototypes, macro definitions, and other declarations. With the #include directive, code reuse, modularity, and organisation are facilitated, as frequently used functions and macros can be defined in one place (in a header file) and included wherever required.
To sum up, #define and #include fundamental aspects of the C programming language that enhance code readability, maintainability, and organisation. They allow programmers to define symbolic names for hard-coded values, create macro functions for repetitive tasks, and incorporate the contents of other files, thereby promoting code reuse and modularity.
Further, conditional compilation is also made possible using the above-mentioned concepts.
Conditional compilation is a feature provided by the C preprocessor, allowing portions of the code to be compiled conditionally. This can be incredibly useful in various scenarios, such as platform-specific programming, debugging, and writing header files.
Conditional compilation is controlled by the preprocessor directives #if, #ifdef, #ifndef, #else, #elif, and #endif.
Here's an example demonstrating the use of conditional compilation:
#define DEBUG |
In this example, the printf statement within the #ifdef...#endif block will only be compiled and executed if DEBUG is defined. If you comment out the #define DEBUG line, the debugging information will not be printed.
#define is a preprocessor directive in C that defines symbolic constants or macros. It's used to make the program more readable and maintainable.
The general syntax of #define in C function is as follows:
#define identifier replacement |
Here’s an example code in C that uses #define:
#define PI 3.14159 |
The #define directive in C is primarily used for defining symbolic constants and creating macro functions.
1. Defining Constants: Using #define to assign a name to a constant value is a common practice that improves code readability. When a value is used frequently in a program, it is recommended to #define it as a symbolic constant.
For instance, the mathematical constant pi is frequently used in circle calculations. Instead of typing out 3.14159 every time we need to use pi, we can define it as a symbolic constant at the beginning of our program:
#define PI 3.14159 |
Now we can use PI anywhere in our program where we'd use 3.14159:
double circumference = 2 * PI * radius; |
2. Creating Macro Functions: Another powerful use of #define is to create macro functions. These are like regular functions, but they're expanded by the preprocessor instead of being called at runtime.
This can lead to more efficient code, but macro functions can also be tricky to use correctly because they're expanded textually. This means you must be careful with operator precedence and side effects.
Here's an example of a define macro in C function that squares a number:
#define SQUARE(x) ((x)*(x)) |
In this example, SQUARE(x) is a macro function that squares x. Because macros are expanded textually, putting parentheses around x in the macro definition is important to ensure correct operator precedence. This is a common pitfall with macro functions.
3. Conditional Compilation: The #define directive can also be used in conjunction with #ifdef, #ifndef, #if, #else, and #elif to control which parts of the program are compiled. This can be useful for creating debug builds, platform-specific code, and more.
For example, we could create a debug build of our program by defining a DEBUG symbol and then using #ifdef to conditionally compile debugging information:
#define DEBUG |
Overall, #define is a flexible tool that can help make your code more readable, efficient, and configurable.
#include is another preprocessor directive in C. It is used to include the contents of another file in the current file. While writing a C program, you are by default supposed to include some important header files as per your use – especially stdio.h.
The general syntax of #include is as follows:
#include <file> |
Or
#include "file" |
Here’s an example code in C that makes use of #include:
#include <stdio.h> |
In this example, #include <stdio.h> tells the preprocessor to include the standard I/O library (stdio.h), which contains functions such as printf() and scanf().
The #include directive in C is used for inserting the contents of one file into another during the preprocessing phase. This is incredibly useful for various reasons:
1. Including Standard Libraries: Perhaps the most common use of #include is to include standard library header files in your program. These files, which have the .h extension, contain function declarations and macro definitions that are part of the C standard library.
For example, if you want to use the printf() function to print to the console, you would need to include the stdio.h file:
#include <stdio.h> |
In this case, #include <stdio.h> tells the preprocessor to include the contents of stdio.h in your program. This file contains the declaration of printf(), among other things.
2. Including User-Defined Header Files: #include can be used to include your own header files. This is very useful for larger programs, as it allows you to separate your program into multiple files.
For instance, suppose you have a header file myfunctions.h, which contains the declaration of a function void myFunction();. You can include this file in your program and then use myFunction():
#include "myfunctions.h" |
3. Modularity and Code Reusability: By placing commonly used function declarations, macro definitions, and other code in a header file, you can #include it in multiple source files. This not only makes your code more organised but also allows you to reuse code across multiple projects.
Remember that the #include directive simply copies the contents of the specified file into the current file. The file to be included is specified using either angle brackets <> (for system header files) or double quotes "" (for user-defined header files).
Though both #define and #include in C are preprocessor directives, they serve different purposes and have different uses.
#define is a preprocessor directive for defining symbolic constants and macros, improving code readability and reusability.
On the other hand, #include is a preprocessor directive used to insert file contents, typically header files, into another file, allowing access to functions and macros defined in those files.
In essence, #define is about creating symbolic constants and macros, whereas #include is about bringing in external code (in the form of header files) to use in your program.
Here is a comparison table summarising the key differences:
#define | #include |
Used for defining symbolic constants and macros | Used for including header files |
Replaces defined identifiers with their corresponding replacement text | Inserts the entire contents of one file into another |
Does not involve any sort of file handling | Involves file handling |
Can be used to conditionally compile sections of code | Cannot be used for conditional compilation |
Does not increase the executable size, as the preprocessor deals with it | Including unnecessary files can increase the executable size |
Syntax: #define identifier replacement_text | Syntax: #include <file> or #include "file" |
The #define and #include in C programming are invaluable tools, and understanding their purposes and differences is crucial for writing efficient and maintainable code. They allow you to define symbolic names for constant values, create reusable macros, and incorporate code from external files, enhancing modularity and reusability.
While this article provided a solid foundation, mastering the concepts of C programming and advancing your skills requires practice and in-depth study. If you're looking to enhance your knowledge to the next level, check out upGrad's Master of Science in Computer Science program, delivered in collaboration with Liverpool John Moores University. This course offers in-depth coverage of topics like software engineering, data science, AI, and machine learning, providing the skills you need to advance in the fast-paced tech industry.
Q1: Can I use a #define macro in a #include statement?
Yes, it's possible to use a #define macro within an #include statement. For instance, consider the following:
#define HEADER "my_header.h" |
Q2: How are #define constants different from const in C?
#define constants are processed by the preprocessor and do not have a memory location, while const variables do have a memory location. const variables also have a type, whereas #define constants don't.
Q3: Can #define be used for function declaration?
#define can be used to create macro functions, but it should not be used for actual function declarations. Macros are expanded by the preprocessor, and this can lead to unexpected behaviour if not used carefully.
Q4: What is the difference between #include "file" and #include <file>?
The #include "file" form looks for the specified file in the current directory first, then the standard library directories. In contrast, #include <file> looks for the file only in the standard library directories.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.