C Programming - C Preprocessor - Discussion

Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 8)
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;
}
10
20
1020
12
Answer: Option
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.

Discussion:
7 comments Page 1 of 1.

Akash said:   1 decade ago
Great job indiabix!!!

Sravanireddy said:   1 decade ago
Hey first second concatenation is first second.

In the same way 10 and 2 should be 102.

Any one please explain.
(1)

Ravitheja said:   1 decade ago
@sravani. here val1##2 ==val12

Naveen Kumar Chaudhary said:   1 decade ago
## 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.
(1)

Pradeep Kumar said:   1 decade ago
#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:   1 decade ago
Isn't it error, is va1va12 is valid identifier in this program?

Atiq said:   8 years ago
You are right @Pradeep Kumar.

Post your comments here:

Your comments will be displayed after verification.