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
C/C++ Macro for string concatenation
In C programming, macros provide a powerful way to concatenate strings at compile-time using the preprocessor. This technique allows for efficient string manipulation without runtime overhead, making it particularly useful for creating constants, debug messages, and code generation.
Syntax
/* String literal concatenation */ #define STRING1 "First part" #define STRING2 "Second part" #define CONCATENATED STRING1 STRING2 /* Token concatenation using ## operator */ #define CONCAT_TOKENS(a, b) a##b
Understanding Macros in C
In C, macros are preprocessor directives defined using #define. They are expanded before compilation, replacing every occurrence with the defined value. Macros are commonly used for constants, simple functions, and code generation −
#include <stdio.h>
#define PI 3.14159
int main() {
float radius = 5.0;
float area = PI * radius * radius;
printf("Area: %.2f\n", area);
return 0;
}
Area: 78.54
Method 1: String Literal Concatenation
C preprocessor automatically concatenates adjacent string literals without any operator. This happens during preprocessing, creating a single string constant −
#include <stdio.h>
#define GREETING "Hello, "
#define NAME "TutorialsPoint"
#define FULL_MESSAGE GREETING NAME
int main() {
printf("%s\n", FULL_MESSAGE);
printf("Direct concatenation: %s\n", "Welcome " "to " "C Programming");
return 0;
}
Hello, TutorialsPoint Direct concatenation: Welcome to C Programming
Method 2: Token Concatenation Using ## Operator
The ## operator concatenates tokens during macro expansion, useful for creating variable names, function names, or other identifiers dynamically −
#include <stdio.h>
#define MAKE_VARIABLE(name) int var_##name
#define PRINT_VAR(name) printf(#name ": %d\n", var_##name)
int main() {
MAKE_VARIABLE(age) = 25; /* Creates: int var_age = 25; */
MAKE_VARIABLE(count) = 100; /* Creates: int var_count = 100; */
PRINT_VAR(age); /* Prints var_age value */
PRINT_VAR(count); /* Prints var_count value */
return 0;
}
age: 25 count: 100
Practical Example: Debug Message System
Here's a practical example combining both techniques to create a debug message system −
#include <stdio.h>
#define DEBUG_PREFIX "[DEBUG] "
#define ERROR_PREFIX "[ERROR] "
#define LOG_MESSAGE(type, msg) printf(type##_PREFIX msg "\n")
int main() {
LOG_MESSAGE(DEBUG, "Program started successfully");
LOG_MESSAGE(ERROR, "Failed to open file");
/* Direct string concatenation for file paths */
#define BASE_PATH "/home/user/"
#define CONFIG_FILE "config.txt"
printf("Config path: %s\n", BASE_PATH CONFIG_FILE);
return 0;
}
[DEBUG] Program started successfully [ERROR] Failed to open file Config path: /home/user/config.txt
Key Points
- String literal concatenation happens automatically for adjacent string literals during preprocessing.
- The ## operator concatenates tokens, creating new identifiers or symbols.
- Macro concatenation occurs at compile-time, resulting in zero runtime overhead.
- Use
#operator to stringify macro arguments when needed.
Conclusion
C macro string concatenation provides efficient compile-time string manipulation through automatic literal concatenation and the ## token-pasting operator. These techniques are essential for creating flexible, maintainable code with preprocessor-based string operations.
