Stringizing Operator in C | How to print a variable name in C?

Stringizing Operator: '#' in preprocessor directive is known as Stringizing Operator it is used to convert an argument into string format.

Print variable name using C program

Defining Macro:

#define macro_function(argument)    #argument

Consider the example

/*
C program to demonstrate example of
Stringizing Operator
*/

#include <stdio.h>

#define getVariableName(x) #x

int main()
{
    int student_age = 21;

    printf("value of %s is = %d\n", getVariableName(student_age), student_age);

    return 0;
}

Output:

value of student_age is = 21

Write a code for Debugging – print variable name with their values

#include <stdio.h>

#define printDebug(x) printf("\nvalue of \"%s\" is: %d\n", #x, x);

int main()
{
    int value1;
    int value2;

    value1 = 10;
    value2 = 20;

    printDebug(value1);
    printDebug(value2);

    return 0;
}

Output:


value of "value1" is: 10

value of "value2" is: 20

printDebug

In this macro variable is passing in x, The statement printf("\nvalue of \"%s\" is: %d\n",#x,x); #x will convert the given argument in string and x will return the value.

C Advance Programs »



Related Programs

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.