V
vippstar
This question might be OT I apologise if it is.
All functions are allowed to be macros.
In a hypothetical implementation a tolower might be written as:
#define tolower(x) \
(x) == 'A' ? 'a' : \
(x) == 'B' ? 'b' : \
/* etc */
(x) == 'Z' ? 'z' : (x)
Ofcourse this has other problems (example if you pass i++ to it) but
we don't care about that now.
What I care about is 2 things:
o How does the compiler warn about incorrect arguments passed/etc?
Normally the preprocessor will change the source code, for example:
int add_one(int x);
#define add_one(x) ((x)+1)
/* ... */
char foo[2];
char * p = foo;
char * s = add_one(p); /* at this point, the compiler won't see a
'call to add_one since it would be replaced by the pp. */
o How does the macro work if I don't pass any arguments?
example:
int foo(unsigned x);
#define foo(x) (~(x))
/* ... */
int (*ptr)(void) = foo; /* how does that work? */
My tolower() macro example would not work in both cases.
thank you for your answers
All functions are allowed to be macros.
In a hypothetical implementation a tolower might be written as:
#define tolower(x) \
(x) == 'A' ? 'a' : \
(x) == 'B' ? 'b' : \
/* etc */
(x) == 'Z' ? 'z' : (x)
Ofcourse this has other problems (example if you pass i++ to it) but
we don't care about that now.
What I care about is 2 things:
o How does the compiler warn about incorrect arguments passed/etc?
Normally the preprocessor will change the source code, for example:
int add_one(int x);
#define add_one(x) ((x)+1)
/* ... */
char foo[2];
char * p = foo;
char * s = add_one(p); /* at this point, the compiler won't see a
'call to add_one since it would be replaced by the pp. */
o How does the macro work if I don't pass any arguments?
example:
int foo(unsigned x);
#define foo(x) (~(x))
/* ... */
int (*ptr)(void) = foo; /* how does that work? */
My tolower() macro example would not work in both cases.
thank you for your answers