By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,019 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

Is it standard compliant?

P: n/a
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

Sep 22 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Nishu schrieb:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 22 '06 #2

P: n/a

Michael Mair wrote:
--snip-
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.
Thanks Michael; and I understood that doing (min)(i,j); invokes the
function address call instead of macro.
-Regards
Nishu

Sep 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.