Reputation: 122
I have read that the type of string literal is char[n+1], where n is the length.The storage of string literals is an implementation issue.But still it must be unique at an instant.
printf
("%u\t %s\t %d\t %c\t %f\t %e\t %x\t %p\t",
&"XY",&"XY",&"XY",&"XY",&"XY",&"XY",&"XY",&"XY");
The output of this code is
4206628 XY 4206628 $ 0.000000 1.800980e-307 7ffde000 00000032
Why %f
gives zero, %s
gives XY (no effect of &
?), and %p
gives a totally different value?
Upvotes: 2
Views: 280
Reputation: 122383
Why
%f
gives zero?
Because %f
expects a double
while it's not, this leads to undefined behavior.
%s
givesXY
(no effect of&
?)
Possibly because for an array arr
: arr
and &arr
have the same value. However, the type is different, which means you are passing the unexpected type to printf
, again, undefined behavior.
and
%p
gives a totally different value?
That's the pointer value you are looking for.
Upvotes: 1
Reputation: 399793
You cannot pass values of the wrong type (a type that doesn't match what the formatting specifier says is expected) and not get undefined behavior.
For instance, it's quite possible that a double
which is what %f
expects is bigger than a pointer (which is what you're actually) passing, thus leading to a mis-match between the passed values and the values consumed by printf()
, and more or less mayhem as a result.
Upvotes: 2