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 3 of 4.

Krishz said:   1 decade ago
Ya you are right vasuuma:-).

Prasad Shinde said:   1 decade ago
# means a concatenation operator in C.

It concats 2 strings.

Nikhil said:   1 decade ago
## concatenates not a single #

Rohit said:   1 decade ago
To concatenate we use dobule hash but single can also do this

Vijendra said:   1 decade ago
What will be the answer the S1, S2?

Wikiok said:   1 decade ago
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);
after preprocessing:
printf("%s=%s %s=%s \n", "str1", str1, "str2", str2);
so:
parameter -> parameter
#parameter -> "parameter"
param1##param2 -> param1param2
Or not? :-)

Vishal said:   1 decade ago
I am not getting it. Any one can explain it properly please.

Sachin Kumar said:   1 decade ago
Can anyone explain me that why semi-colon(;) is used because we can't use semi-colon in macros ?

Pooja said:   1 decade ago
Yup.its true..firstly it is concate the 2 string nd than declared with the join s1,s2..
so,#s1 gives output as str1,#s2 gives output as str2

so.str1=india,str2=bix

Shekar said:   1 decade ago
# operator stringrises the operand. For example #str1 is replaced by "str1" during compilation.


Post your comments here:

Your comments will be displayed after verification.