C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 4)
4.
What will be the output of the program?
#include<stdio.h>
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
int main()
{
    char *str1="India";
    char *str2="BIX";
    JOIN(str1, str2);
    return 0;
}
str1=IndiaBIX str2=BIX
str1=India str2=BIX
str1=India str2=IndiaBIX
Error: in macro substitution
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
35 comments Page 1 of 4.

Poovarasan said:   4 years ago
Not getting it. Please explain me.

Ashwini said:   6 years ago
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);

How ?
please help me to understand it?

Saravana said:   6 years ago
# is called Stringizing operator, whatever you pass (here variable name ) is converted to String, so result come with the variable name.
(1)

Jayesh said:   6 years ago
This because there we use the JOIN STRING FUNCTION in which the str1 print& then next str2join the str1.

Because we have to use join string function.
(1)

Sruthi said:   6 years ago
The rule is if the parameter name is preceded by a # in the macro expansion, the combination (of # and parameter) will be expanded into a quoted string with the parameter replaced by the actual argument. This can be combined with string concatenation to print desired the output.

Kaviyakarthi said:   7 years ago
@Sunil:

I thought the solution is,

a = ++b*++b*++b.
a = ++3*++b*++b.
a = 4*++4*++b.
a = 4*5*++5.
a = 4*5*6.
a = 120.
b = 6.

Kaviyakarthi said:   7 years ago
@Sunil:

I thought the solution is,

a = ++b*++b*++b.
a = ++3*++b*++b.
a = 4*++4*++b.
a = 4*5*++5.
a = 4*5*6.
a = 120.
b = 6.

Nimisha said:   8 years ago
#s is used to print string value.

Bhushan said:   8 years ago
@Sunil : It should be:

216.
6.

Value would be overwrite.

a = (++b * ++b * ++b).
a = (4 * ++b * ++b).

a = (5 * 5 * ++b).
a = (6 * 6 * 6).
a = 216.

Omkar said:   8 years ago
Can anyone explain @Sunil's program ?


Post your comments here:

Your comments will be displayed after verification.