05. Preprocessor
05. Preprocessor
C
1. When we use include directive, the contents of included header file (after preprocessing) are
copied to the current file.
Angular brackets < and > instruct the preprocessor to look in the standard folder where all header
files are held. Double quotes “ and “ instruct the preprocessor to look into the current folder
(current directory).
2. When we use define for a constant, the preprocessor produces a C program where the defined
constant is searched and matching tokens are replaced with the given expression.
3. The macros can take function like arguments, the arguments are not checked for data type. For
example, the following macro INCREMENT(x) can be used for x of any data type.
#include <stdio.h>
#define INCREMENT(x) ++x
int main()
{
char* ptr = "GeeksQuiz";
int x = 10;
printf("%s ", INCREMENT(ptr));
printf("%d", INCREMENT(x));
return 0;
}
Output:
eeksQuiz 11
4. The macro arguments are not evaluated before macro expansion. For example, consider the
following program
#include <stdio.h>
#define MULTIPLY(a, b) a* b
int main()
{
// The macro is expanded as 2 + 3 * 3 + 5, not as 5*8
printf("%d", MULTIPLY(2 + 3, 3 + 5));
return 0;
}
// Output: 16
5. The tokens passed to macros can be concatenated using operator ## called Token-Pasting
operator.
#include <stdio.h>
#define merge(a, b) a##b
int main() { printf("%d ", merge(12, 34)); }
Output:
1234
6. A token passed to macro can be converted to a string literal by using # before it.
#include <stdio.h>
#define get(a) #a
int main()
{
// GeeksQuiz is changed to "GeeksQuiz"
printf("%s", get(GeeksQuiz));
}
Output:
GeeksQuiz
7. The macros can be written in multiple lines using ‘\’. The last line doesn’t need to have ‘\’.
#include <stdio.h>
#define PRINT(i, limit) \
while (i < limit) { \
printf("GeeksQuiz "); \
i++;
}
int main()
{
int i = 0;
PRINT(i, 3);
return 0;
}
Output:
GeeksQuiz GeeksQuiz GeeksQuiz
8. There are some standard macros which can be used to print program file (FILE), Date of
compilation (DATE), Time of compilation (TIME) and Line Number in C code (LINE)
# vs ## Operators in C
Certainly! Here's a comparison of the # and ## operators in C, explained in simple language:
The # operator, also known as The ## operator, also known as the token
the stringizing or stringify pasting or concatenation operator,
Description
operator, converts tokens into concatenates two tokens into a single
strings. token.
Example
```cpp ```cpp
Use
return 0; return 0;
} }
#include <stdio.h>
#define getName(var) #var
int main()
{
int myVar;
printf("%s", getName(myVar));
return 0;
}