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 program to print a string without any quote in the program
This is another tricky problem. In this program, we will see how to print a string using C where no quotation marks are used in the source code.
Here we are using a macro function with the stringizing operator. We define a macro function that converts its argument into a string literal at compile time.
Syntax
#define getString(x) #x
The getString() is a macro function that returns x by converting it into a string. The # before x is the stringizing operator that converts the macro argument into a string literal.
Example
Here's how to print a string without using quotes directly in the printf statement −
#include <stdio.h>
#define getString(x) #x
int main() {
printf(getString(Hello World));
printf("
");
printf(getString(TutorialsPoint is awesome!));
return 0;
}
Hello World TutorialsPoint is awesome!
How It Works
- The preprocessor replaces
getString(Hello World)with"Hello World" - The
#operator converts the tokens after the macro name into a string literal - Spaces and special characters are preserved in the resulting string
Key Points
- This technique uses the C preprocessor's stringizing operator (
#) - The macro argument becomes a string literal at compile time
- No quotes are needed in the function call, but quotes are generated by the preprocessor
Conclusion
Using the stringizing operator with macros allows printing strings without directly writing quotes in the code. This demonstrates the power of C preprocessor directives for code manipulation.
