The Gateway to Computer Science Excellence
+1 vote
consider the following program



int a=5;


printf("%p",b);//  .................LINE 1

printf("%d",b);//...................LINE 2

printf("%u",b);//...................LINE 3


which of the following statement is true.

A) both Line 1 and line 2  will always print correct address.

B)only line 3 will  always print correct address.

C)only line 1 and 3 will always print correct address.

D)all the lines i.e line 1 line 2 line 3  will always print  correct address.
in Programming by Loyal (6.2k points)
retagged by | 64 views

1 Answer

0 votes

Answer is (d).

All the lines will print the correct address that is the value of variable b(which is equal to address of  a since its a  pointer), just with different formats. Line one will print the b as a pointer (a hexadecimal value) and line two will print the address represented as integers (%d for decimal representation of integer number and %u for unsigned integer).



The % Format Specifiers

The % specifiers that you can use in ANSI C are:

Usual variable type Display

%c char single character

%d (%i) int signed integer

%e (%E) float or double exponential format

%f float or double signed decimal

%g (%G) float or double use %f or %e as required

%o int unsigned octal value

%p pointer address stored in pointer

%s array of char sequence of characters

%u int unsigned decimal

%x (%X) int unsigned hex value

by Junior (941 points)
but what i read from one solution is that due to last bit reservation in signed formate(%d) sometime gives wrong address means we cant except correct address from %d
when I executed this code, it gave the same and expected result.
it may be but i think question is about for finding correct location in memory always.
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,391 answers
105,442 users