C Programming - C Preprocessor - Discussion

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;
}

[A]. str1=IndiaBIX str2=BIX
[B]. str1=India str2=BIX
[C]. str1=India str2=IndiaBIX
[D]. Error: in macro substitution

Answer: Option B

Explanation:

No answer description available for this question.

Nachiyappan said: (Jun 27, 2010)  
Is it possible to print value through pre-processor ?

Vasuuma said: (Jul 15, 2010)  
# mean to variable name. so #s1 gives output as str1

Krishz said: (Oct 4, 2010)  
Ya you are right vasuuma:-).

Prasad Shinde said: (Oct 15, 2010)  
# means a concatenation operator in C.

It concats 2 strings.

Nikhil said: (Nov 13, 2010)  
## concatenates not a single #

Rohit said: (Dec 5, 2010)  
To concatenate we use dobule hash but single can also do this

Vijendra said: (Dec 17, 2010)  
What will be the answer the S1, S2?

Wikiok said: (Mar 22, 2011)  
#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: (Mar 28, 2011)  
I am not getting it. Any one can explain it properly please.

Arun said: (Apr 5, 2011)  
#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.

Pooja said: (May 6, 2011)  
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: (Jul 12, 2011)  
# operator stringrises the operand. For example #str1 is replaced by "str1" during compilation.

Natasha said: (Jul 30, 2011)  
# 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");

Kundan said: (Aug 30, 2011)  
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

Saurabh Tiwari said: (Dec 13, 2011)  
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.

Vimala said: (Jan 10, 2012)  
# defines that name of the variable.

Durga Dutt said: (Sep 29, 2012)  
isn't an error if we use ";" at the end of macro expansion?

Sparsh610 said: (Mar 12, 2013)  
It only works in macros ?

Or we can print them in main function too ?

Sachin Kumar said: (Jul 17, 2013)  
Can anyone explain me that why semi-colon(;) is used because we can't use semi-colon in macros ?

Nandini said: (Aug 24, 2013)  
#is name of the variable. So, if #str1 gives str1 content.

Gaurav Kumar Garg said: (Jan 20, 2014)  
@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.

Avinash said: (Mar 13, 2014)  
# symbol is used to print the actual arguments that are passed to function.

Sunil Kothari said: (Aug 9, 2014)  
#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?

Shashwat said: (Sep 21, 2014)  
We have to add semicolon either at the macro declaration or while calling macro.

Neeraja said: (Sep 21, 2014)  
Please give clarity what #s1 #s2 means?

Omkar said: (Feb 17, 2015)  
Can anyone explain @Sunil's program ?

Bhushan said: (May 17, 2015)  
@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.

Nimisha said: (May 18, 2015)  
#s is used to print string value.

Kaviyakarthi said: (Feb 10, 2016)  
@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: (Feb 10, 2016)  
@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.

Sruthi said: (Jan 31, 2017)  
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.

Jayesh said: (May 30, 2017)  
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.

Saravana said: (Jun 16, 2017)  
# is called Stringizing operator, whatever you pass (here variable name ) is converted to String, so result come with the variable name.

Ashwini said: (Nov 10, 2017)  
#define JOIN(s1, s2) printf("%s=%s %s=%s \n", #s1, s1, #s2, s2);

How ?
please help me to understand it?

Poovarasan said: (Sep 19, 2019)  
Not getting it. Please explain me.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.