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.

Saurabh Tiwari said:   1 decade ago
Yup.... arun and puja both of you are right because

#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);

Here

1) #s1 is equal to "variable name" i.e (str1) so it will print same as it is.

2) s1 that will print the "value of str1" i.e (India along with =)
and so on.

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? :-)

Sruthi said:   9 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.
(2)

Kundan said:   1 decade ago
I agree with Natasa. For better understanding purpose I give a example code.

#include<stdio.h>
#define res(s1,s2) printf("%s=%d %s=%d",#s1,s1,#s2,s2);

void main()
{
int a=4,b=3;
res(a,b);
}

Output of this code is
a=4 b=3

Arun said:   1 decade ago
#s1,#s2 - this will print the variable name which has been declared with the join
s1,s2 - which will display the characters

str1 and str2 are the variables and *str1 and * str2 are the pointers which has declred as char.

Natasha said:   1 decade ago
# is a stringizing operator. it will convert the argument into string.
eg #define df(a) printf(#a)
main()
{
int b=7;
df(b);
}

This will give result
b
as the statement df(b) was converted to printf("b");
(1)

Sunil Kothari said:   1 decade ago
#include<stdio.h>
#define cube(x) (x*x*x)

void main()

{
int a,b =3;
a= cube(++b);
printf("%d \n %d", a, b);
}

Output:
-------
150
6

Please explain above program?

Gaurav Kumar Garg said:   1 decade ago
@Sachin Kumar.

Its your choice you can also remove semicolon. without semi colon it will also work.

With semi colon in macro
You can also call JOIN(str1, str2) <--- no semicolon.

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

Jayesh said:   8 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)


Post your comments here:

Your comments will be displayed after verification.