Stringizing operator (#) in C

In this blog post, you will learn about the stringizing operator (#)  and it uses in C programming. Also, you will see some C programming examples of stringizing operators (#). So let’s first understand what is stringizing operator (#) in C?

 

What is the “stringizing” operator (#)?

The  “stringizing” operator (#) converts macro parameters to string literals without expanding the parameter definition. It is used only with function-like macro (macros that take arguments). Let’s first understand the syntax of function-like macro it will help you to understand the concept and use of the number-sign or “stringizing” operator (#).

#define identifier( parameters ) replacement-list

The above declaration is a simple syntax of a function-like macro with replacement. Let’s see an example.

// Function-like Macro definition 

#define MAX(a, b) (((a) > (b)) ? (a) : (b))

 

If a parameter is immediately preceded by a # (preprocessing token) in the replacement list of a function-like macro, the parameter is enclosed within quotation marks and treated as a literal string.

Example C program with Stringizing operator (#):

The following examples show a function-like macro definition that includes the stringizing operator, and the main function that invokes the macro.

Example 1:

#include <stdio.h>

#define PRINT_MSG( x ) printf(#x)

int main()
{
    PRINT_MSG(LOVE TO READ ATICLEWORLD);

    return 0;
}

Output: LOVE TO READ ATICLEWORLD

 

Explanation of the code:

The PRINT_MSG macros are expanded during preprocessing, producing the following code.

int main()
{
    printf( "LOVE TO READ ATICLEWORLD");
    
    return 0;
}

 

Example 2:

#include <stdio.h>

#define PRINT_MSG( x ) printf(#x)

int main()
{
    PRINT_MSG(12345);

    return 0;
}

Output: 12345

Explanation of the code:

The PRINT_MSG macros are expanded during preprocessing, producing the following code.

#include <stdio.h>

int main()
{
    printf("12345");

    return 0;
}

 

Example 3:

#include <stdio.h>

#define PRINT_MSG( x ) printf(#x "\n")

int main()
{
    PRINT_MSG(LOVE TO READ ATICLEWORLD);

    PRINT_MSG(12456);

    return 0;
}

Output:

LOVE TO READ ATICLEWORLD
12456

Explanation of the code:

The PRINT_MSG macros are expanded during preprocessing, producing the following code.

int main()
{
    printf("LOVE TO READ ATICLEWORLD" "\n");

    printf( "123456" "\n");

    return 0;
}

 

Note: If the replacement that results is not a valid character string literal, the behavior is undefined.

 

White space that precedes the first token and follows the last token of the actual argument is ignored. Any white space between the tokens in the actual argument is reduced to a single white space in the resulting string literal.

#include <stdio.h>

#define PRINT_MSG(x) printf( #x )

int main()
{
    PRINT_MSG( ab c );

    return 0;
}

Output:

Stringizing operator (#) in C

 

 

 

 

 

Explanation of the code:

The PRINT_MSG macros are expanded during preprocessing, producing the following code.

int main()
{
    printf("ab c");
    
    return 0;
}

 

A \ character is inserted before each " and \ character of a string literal (including the delimiting “characters), except that it is implementation-defined whether a \ character is inserted before the \ character beginning a universal character name.

#include <stdio.h>

#define PRINT_MSG( x ) printf(#x "\n")

int main()
{
    PRINT_MSG(LOVE TO READ ATICLEWORLD);

    PRINT_MSG("LOVE TO READ ATICLEWORLD");

    PRINT_MSG("Wow:\" LOVE TO READ ATICLEWORLD");

    return 0;
}

Output:

Stringizing operator (#) in C special case

 

 

 

 

 

 

 

Explanation of the code:

The PRINT_MSG macros are expanded during preprocessing, producing the following code.

int main()
{
    printf("LOVE TO READ ATICLEWORLD" "\n");

    printf("\"LOVE TO READ ATICLEWORLD\"" "\n");

    printf("\"Wow:\\\" LOVE TO READ ATICLEWORLD\"" "\n");

    return 0;
}

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *