Print argument (i.e. variable name, value) using Macro in C

Here, we will learn how to print the given argument like variable name, value using Macro in C programming language?
By IncludeHelp Last updated : March 10, 2024

To print argument (which is passed as Macro argument), we use # operator in C programming language. # prints the passed argument as it is, it may be a variable name, value etc.

Example

#include <stdio.h>

#define SQUARE(N)	printf("Square of " #N " is = %d\n",(N*N))
#define CUBE(N)		printf("Cube   of " #N " is = %d\n",(N*N*N))

int main()
{
	int number = 10;
	
	//passing variable name 
	SQUARE(number);
	CUBE(number);
	
	//passing values
	SQUARE(10);
	CUBE(10);

	return 0;
}

Output

Square of number is = 100
Cube   of number is = 1000
Square of 10 is = 100
Cube   of 10 is = 1000

C Preprocessors Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.