Is it standard compliant?

N

Nishu

Hi all,

I was under the impression that below code should give errors while
compiling, since i'm using same name for macro as well as function. but
it didnt give any errors!!
Isnt this is correct that pre-processors are processed before compiling
and wherever the "matched" label is found, pre-processor replaces it
with corresponding code snippet.
Is it defined in standard that scope of pre-processor starts only for
the code which occurs after its definition? (Now, In practice, I AVOID
using macros in small letters. Its just an example to clarify my
doubt.)

#include<stdio.h>

int min(int x, int y)
{
return (((x) < (y))? (x) : (y));
}

int main(void)
{
int i, j, k, l;
i = 1;
j =2;
k = min(i,j);

#define min(x,y) (((x) < (y))? (x) : (y))

l = min(i,j);
printf("k = %d l = %d", k, l);

return 0;
}

Thanks.
-Nishu
 
M

Michael Mair

Nishu said:
I was under the impression that below code should give errors while
compiling, since i'm using same name for macro as well as function. but
it didnt give any errors!!

Apart from library identifiers which are allowed to belong to a
function-like macro or to a function, there is no problem here.
Due to the different translation phases, there is a well-defined
albeit possibly unexpected result (which is, in this case, good
enough).
Isnt this is correct that pre-processors are processed before compiling
and wherever the "matched" label is found, pre-processor replaces it
with corresponding code snippet.

A macro is known from the place of its definition to the end
of the translation unit (or to a corresponding #undef directive),
so there is a "macro defined" and "macro undefined" part of
your translation unit. Only in the former, replacement takes
place.
Is it defined in standard that scope of pre-processor starts only for
the code which occurs after its definition? (Now, In practice, I AVOID
using macros in small letters. Its just an example to clarify my
doubt.)

Essentially, yes.

#include<stdio.h>

int min(int x, int y)
{

Mark this one with
puts("function");
return (((x) < (y))? (x) : (y));
}

int main(void)
{
int i, j, k, l;
i = 1;
j =2;
k = min(i,j);

#define min(x,y) (((x) < (y))? (x) : (y))

Mark this one with
#define min(x, y) \
(puts("macro"), \
(((x) < (y))? (x) : (y)) \
)
l = min(i,j);

FWIW, you can "protect" function calls against macro
replacement: Insert
(min)(i, j);
to see what it prints.
printf("k = %d l = %d", k, l);

return 0;
}

Cheers
Michael
 
N

Nishu

Michael Mair wrote:
--snip-
Essentially, yes.



Mark this one with
puts("function");


Mark this one with
#define min(x, y) \
(puts("macro"), \
(((x) < (y))? (x) : (y)) \
)


FWIW, you can "protect" function calls against macro
replacement: Insert
(min)(i, j);
to see what it prints.

Thanks Michael; and I understood that doing (min)(i,j); invokes the
function address call instead of macro.
-Regards
Nishu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top