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 C Programming Language Puzzle?
Here we will see one C programming language 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
How Token Pasting Works
The ## operator concatenates the tokens during preprocessing. When we write y##x, the preprocessor literally joins the tokens to form a new token. In our case, 96##48 becomes the integer literal 9648.
Example
#include <stdio.h>
#define MERGE(x, y) y##x
int main() {
printf("%d<br>", MERGE(48, 96));
return 0;
}
Output
9648
Additional Examples
Here are more examples showing different uses of the token pasting operator −
#include <stdio.h>
#define CONCAT(a, b) a##b
#define REVERSE_CONCAT(x, y) y##x
int main() {
printf("Normal concatenation: %d<br>", CONCAT(123, 456));
printf("Reverse concatenation: %d<br>", REVERSE_CONCAT(789, 321));
printf("With single digits: %d<br>", CONCAT(7, 8));
return 0;
}
Normal concatenation: 123456 Reverse concatenation: 321789 With single digits: 78
Key Points
- The
##operator works only in macro definitions during preprocessing. - It creates a new token by joining adjacent tokens literally.
- No arithmetic operations are involved − it's pure token concatenation.
- The result must form a valid token in C.
Conclusion
The token pasting operator provides an elegant solution to concatenate numbers without using arithmetic or string operations. It works at the preprocessing level, making it perfect for this type of puzzle.
