in Programming in C retagged by
454 views
1 vote
1 vote
consider the following program

main()

{

int a=5;

int*b=&a;

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 in C retagged by
by
454 views

1 Answer

0 votes
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).

Supplementary:

Source: https://www.le.ac.uk/users/rjm1/cotter/page_30.htm

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

3 Comments

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
0
0
when I executed this code, it gave the same and expected result.
0
0
it may be but i think question is about for finding correct location in memory always.
0
0