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;
}
Discussion:
35 comments Page 2 of 4.
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
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.
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");
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)
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
#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:
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.
#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:
1 decade ago
# defines that name of the variable.
Durga dutt said:
1 decade ago
isn't an error if we use ";" at the end of macro expansion?
Sparsh610 said:
1 decade ago
It only works in macros ?
Or we can print them in main function too ?
Or we can print them in main function too ?
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 ?
Nandini said:
1 decade ago
#is name of the variable. So, if #str1 gives str1 content.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers