##### C Program

Explain the output of the program in the attached file.

Arul
13 Jan 2015 03:41 pm

looks like a)

Ranita Biswas
13 Jan 2015 03:51 pm

Only if the program is modified like the following, the answer will be (a)

#include<stdio.h>
#define PRINT(format,x) printf(#x " = %" #format "\n", x)
int integer = 5;
char character = '5';
char *string = "5";
int main()
{
PRINT(d,string);
PRINT(d,character);
PRINT(d,integer);
}

Otherwise, the macro written within printf quotes will not take the proper values of x and format, so will lead to answer (d) None of these.

Aparajita Mehta
13 Jan 2015 06:12 pm

Please explain how it will be (a) and why we need to put #??

Ranita Biswas
13 Jan 2015 07:37 pm

In a macro definition, to turn a token into a string containing the literal text of the token we need to prefix the token with a # sign. You can simply run the given code and verify.

Aparajita Mehta
13 Jan 2015 11:59 pm

Is it necessary that if we use %d format specifier with a string than it would give its address, and how is character giving output as 53?? Please explain!

K Ankith Kumar
16 Jun 2017 12:46 pm

string contains starting address of string constant "5". So, %d format specifier with a string gives its address.
Whereas character contains the constant '5'. Here, %c with character will give '5' and %d with character gives the ASCII value of '5'.
Note: Integer value of a character is equal to it's ASCII value.

Arul
13 Jan 2015 04:15 pm

yes. you are right.

thanks!

sanjay
15 Jul 2017 08:28 pm

When u declare string as a character pointer,it'll hold the base address of the character string,so when u print it with %d as format specifier,it's base address will be printed as an integer

When %d is passed as a format specifier and if we use it to print a character,it's ascii value will be printed and so as the ascii value of '5' is 53,so 53 is printed

and when integer is printed it will output the integer value

Macros can be defined using #define and u can also specify functions as a part of macro.