Token Pasting Operator (##) in C/C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

Operators in C/C++ are essential tools for performing different data operations. Developers may design effective and concise code by knowing their unique categories - arithmetic, relational, logical, bitwise, assignment, and more. Understanding operator precedence ensures that expressions are evaluated correctly. Continuous practice and application result in a thorough understanding of these core programming components.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Token-pasting Operator

Preprocessor directives in C and C++ are commands that guide the compiler in manipulating the source code before actual compilation. They enhance code efficiency, readability, and customization by enabling transformations, conditionals, and macro expansions.

Introduction to Preprocessor

The C/C++ preprocessor is a crucial initial phase of compilation. It processes preprocessor directives before the code enters the main compilation phase. Its role includes including header files, conditionally compiling code, and expanding macros.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Macro Expansion

One of the preprocessor's essential functions is macro expansion. Macros are user-defined symbolic names that represent a sequence of code. The preprocessor replaces macro instances with their corresponding code snippets, enhancing code readability and reducing redundancy.

Token Passing Operator

Efficiency and flexibility are critical in C/C++ development. This is where the token-pasting operator, commonly indicated as ##, comes into play. This modest pair of hash symbols may not catch your eye initially, but they serve an important function in macro expansion, a procedure that simplifies and streamlines code.

Simply put, the token-pasting operator joins two tokens (variables, keywords, etc.) to form a new token. Consider the following scenario: You have a macro that defines a variable name based on a prefix and a suffix. Instead of coding many macros for different scenarios, you may use the token-pasting operator to construct variable names dynamically.

For instance:

Here, if you use CREATE_VAR(data, 1), the token-pasting operator combines data and 1 to form data1, creating the variable int data1 = 0;.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Token-pasting Operator's Role:

The token-pasting operator ## is a powerful tool within the macro expansion. It enables the dynamic merging of tokens, such as variables and literals, to form new identifiers. This facilitates the creation of flexible and reusable macros that adapt to different contexts.

Syntax

The token-pasting operator ## in C/C++ programming merges two independent tokens into a single token. It is used to generate new identifiers or symbols within macros. If X and Y are tokens, for example, X ## Y becomes XY. This aids in code optimization by creating dynamic names for variables and functions.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Example

Consider establishing numerous variables or functions with identical names that differ only in their suffixes. Instead of typing them all out by hand, the token-pasting operator can help. Let's look at an example:

Code

Output:

Explanation:

This code's CREATE_VARIABLE macro employs the token-pasting operator to combine the provided identifier with a number. This yields the variables value1 and number2. Remembering token copying occurs before compilation and during the preprocessing stage is vital.

Application of Token-pasting Operator

The token-pasting operator is often used in the generation of macro names. Developers may construct dynamic and adaptable macro names that adapt to various conditions by combining distinct identifiers. Consider the following example:

This approach is extremely useful for automating repetitive activities, easing code maintenance, and improving code readability. As you start writing, use the token-pasting operator to open up a world of possibilities, making your codebase more resilient and versatile.

Conclusion

  • During preprocessing, the token-pasting operator merges two tokens into one. This facilitates the dynamic creation of variable names. It is most commonly used in macros, where inputs can produce variable names.
  • It increases code reuse and decreases redundancy.
  • Errors are found at build time since it runs during preprocessing. It aids in the early discovery of bugs and speeds up debugging.
  • As a result, by permitting dynamic naming, the token-pasting operator simplifies code production and increases efficiency. Use caution to keep the code clear.