Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
A Puzzle using C Program
Here we will see one C puzzle question. Suppose we have two numbers 48 and 96. We have to add the first number after the second one. So final result will be like 9648. But we cannot use any logical, arithmetic, string related operations, also cannot use any pre-defined functions. So how can we do that?
This is easy. We can do by using Token Pasting operator (##) in C. The Token Pasting operator is a preprocessor operator. It sends commands to compiler to add or concatenate two tokens into one string. We use this operator at the macro definition.
Syntax
#define MACRO_NAME(param1, param2) param2##param1
Example
The following program demonstrates how to concatenate two numbers using the token pasting operator −
#include <stdio.h>
#define MERGE(x, y) y##x
int main() {
printf("%d", MERGE(48, 96));
return 0;
}
Output
9648
How It Works
The token pasting operator (##) works at the preprocessor level. When the compiler encounters MERGE(48, 96), it expands the macro by concatenating the tokens. The macro y##x becomes 96##48, which results in the single token 9648.
Conclusion
The token pasting operator provides an elegant solution to concatenate numbers without using arithmetic or string operations. It works entirely at the preprocessor level, making it a powerful tool for macro definitions in C programming.
