C Programming - C Preprocessor - Discussion

8. 

What will be the output of the program?

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int va1=10;
    int va12=20;
    printf("%d\n", FUN(va1, 2));
    return 0;
}

[A]. 10
[B]. 20
[C]. 1020
[D]. 12

Answer: Option B

Explanation:

The following program will make you understand about ## (macro concatenation) operator clearly.

#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
    int First  	= 10;
    int Second  = 20;

    char FirstSecond[] = "IndiaBIX";

    printf("%s\n", FUN(First, Second) );

    return 0;
}

Output:
-------
IndiaBIX

The preprocessor will replace FUN(First, Second) as FirstSecond.

Therefore, the printf("%s\n", FUN(First, Second) ); statement will become as printf("%s\n", FirstSecond );

Hence it prints IndiaBIX as output.

Like the same, the line printf("%d\n", FUN(va1, 2)); given in the above question will become as printf("%d\n", va12 );.

Therefore, it prints 20 as output.


Akash said: (Jul 30, 2011)  
Great job indiabix!!!

Sravanireddy said: (Aug 17, 2011)  
Hey first second concatenation is first second.

In the same way 10 and 2 should be 102.

Any one please explain.

Ravitheja said: (Jan 17, 2012)  
@sravani. here val1##2 ==val12

Naveen Kumar Chaudhary said: (Sep 21, 2013)  
## is the token concatenation or token pasting operator.

You can even use it to form longer numbers like 1.5##e3 will give you 1.5x10^3 or it can be used for string concatenation.

Here val##2 -->val2.

Pradeep Kumar said: (Jul 7, 2014)  
#include<stdio.h>
#define FUN(i, j) i##j

int main()
{
int va1=10;
int va12=20;
printf("%d\n", FUN(va1, 2));
return 0;
}

Here ## is the token concatenation operator.
FUN(val,2)is
val##2 its give val2
So printf("%d\n",val2) give 20.

A.S Bhullar said: (Jul 12, 2014)  
Isn't it error, is va1va12 is valid identifier in this program?

Atiq said: (Oct 10, 2016)  
You are right @Pradeep Kumar.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.