Abinaya Perumal
Abinaya Perumal

Reputation: 33

Underatanding of #define in c

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

I got output

Hello

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

Upvotes: 0

Views: 148

Answers (1)

Anatoliy R
Anatoliy R

Reputation: 1789

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.

Upvotes: 2

Related Questions