Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is it standard compliant?

Reply
Thread Tools

Is it standard compliant?

 
 
Nishu
Guest
Posts: n/a
 
      09-22-2006
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

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      09-22-2006
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.
 
Reply With Quote
 
 
 
 
Nishu
Guest
Posts: n/a
 
      09-22-2006

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
standard libraries don't behave like standard 'libraries' Sriram Srinivasan Python 13 11-12-2009 06:05 PM
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
How to redirect a "system" standard output and standard error to avariable (Linux) Venks Ruby 5 12-06-2007 12:21 AM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM



Advertisments