0% found this document useful (0 votes)
2 views

05. Preprocessor

Uploaded by

yevob11108
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

05. Preprocessor

Uploaded by

yevob11108
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Interesting Facts about Macros and Preprocessors in

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:

Feature # Operator ## Operator

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.

Syntax #token token1 ## token2

Example #define STRINGIZE(x) #x #define CONCATENATE(x, y) x ## y

Used in macros to convert macro


Used in macros to concatenate or join
Usage arguments or identifiers into string
tokens to create new identifiers or symbols.
literals.

Example
```cpp ```cpp
Use

#define PRINT_VAR_NAME(var) #define MAKE_IDENTIFIER(x) identifier ##


printf(#var) x

int main() { int main() {

int num = 10; MAKE_IDENTIFIER(1) = 100;

PRINT_VAR_NAME(num); printf("%d\n", identifier1);

return 0; return 0;

} }

When called with num , it prints It creates a new identifier identifier1 ,


Output
"num" . which holds the value 100 .

Debugging, logging, and


Creating unique identifiers or symbols
Use Cases generating error messages with
dynamically based on existing tokens.
macro arguments.
How to print and store a variable name in string variable?

#include <stdio.h>
#define getName(var) #var

int main()
{
int myVar;
printf("%s", getName(myVar));
return 0;
}

typedef versus #define in C


typedef is limited to giving symbolic names to types only, whereas #define can be used to define
an alias for values as well, e.g., you can define 1 as ONE, 3.14 as PI, etc.
typedef interpretation is performed by the compiler where #define statements are performed by
preprocessor.
#define should not be terminated with a semicolon, but typedef should be terminated with
semicolon.
#define will just copy-paste the definition values at the point of use, while typedef is the actual
definition of a new type.
typedef follows the scope rule which means if a new type is defined in a scope (inside a function),
then the new type name will only be visible till the scope is there. In case of #define, when
preprocessor encounters #define, it replaces all the occurrences, after that (No scope rule is
followed).

You might also like