0
#define print(args) printf args print(("Hello")); 

I got output

Hello 

If I call print it works fine. Could you explain how it works?

1 Answer 1

2

This is an example of a macro.

When you compile a program, the first step is the preprocessor.

The preprocessor finds your macro:

#define print(args) printf args 

This means that if in your program you have something like

print(<some text>) 

Then the value of <some text> will be processed as args from your macro, i.e. code

print(<some text>) 

will be replaced with

printf <some text> 

Now, you have this line of code:

print(("Hello")); 

If you put <some text> = args = ("Hello"), then preprocessor will replace

print(("Hello")) 

with

printf ("Hello") 

and the whole line will be:

printf ("Hello"); 

which is legal c code to print Hello.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice detailed answer. Still, regarding the style, this: "first you run compiler. Before compiler is activated, the program finds" I feel is a bit confusing, or at least quiet unclear. What is "the program"? Only after having read through half of your answer you give a hint ("the preprocessor") what it might have been.
Imo it's misspelling. The 'program' here is probably a preprocessor. A preprocessor macros are in fact a dummy replacement of text, without any semantic/syntactic analysis (that's why macros are advised to avoid - use constexpr functions instead)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.