Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C language important programs ( Advance Programs )

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to find class of an IP Address... >>
<< Function Pointer example program in C programming...