A
Allan M. Bruce
I just recently took a C programming test (which was a total farce - but
thats not the point). The code below is a combination of two questions that
were included in the test and I was hoping to raise a few questions about
it. First, the code:
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int increment(int *in)
{
(*in)++;
/* *in++; */ /* Doesnt work*/
return *in;
}
int main(void)
{
int i=1;
printf("%s %s\n", g(f((1,2),3)), h(f((1,2),3)));
printf("%i%i\n", increment(&i), increment(&i));
return 0;
}
The output from the above is:
f((1,2),3) (1,2)3
32
I have 3 questions...
1) Why is the output of the first line so? I have never come across # in
defines.
2) What is the difference between (*in)++ and *in++ ? obviously the latter
doesnt work as I have tested it. Is this because the ++ has higher priority
over *?
3) The output from the last printf is 32, which I suspect because the
variable arguements to printf are executed in reverse order, is this
correct?
Many Thanks
Allan
thats not the point). The code below is a combination of two questions that
were included in the test and I was hoping to raise a few questions about
it. First, the code:
#include <stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int increment(int *in)
{
(*in)++;
/* *in++; */ /* Doesnt work*/
return *in;
}
int main(void)
{
int i=1;
printf("%s %s\n", g(f((1,2),3)), h(f((1,2),3)));
printf("%i%i\n", increment(&i), increment(&i));
return 0;
}
The output from the above is:
f((1,2),3) (1,2)3
32
I have 3 questions...
1) Why is the output of the first line so? I have never come across # in
defines.
2) What is the difference between (*in)++ and *in++ ? obviously the latter
doesnt work as I have tested it. Is this because the ++ has higher priority
over *?
3) The output from the last printf is 32, which I suspect because the
variable arguements to printf are executed in reverse order, is this
correct?
Many Thanks
Allan