Printf("%d")

G

geek

Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Any help would be appreciated.

Thanks,
MJ
 
W

Walter Roberson

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.
-13361016

It is undefined behaviour to have a format element for which the
parameter is missing or not of the matching type.


What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack
when you tried the %c format element, then probably your system
would attempt to convert one of the bytes as a character; exactly
which one would depend upon your internal system architecture.
Likely it is not true that it doesn't print anything: if you were
to save the output to a file and look at the file in detail,
you would probably find the character with value 255 (0xff) or
value 136 (0x88) or with value 32 (0x20). The first two of those
do not produce any visible graphics in ASCII or ISO-8859-1;
the last of those happens to correspond to the space character
in ASCII and ISO-8859-1.
 
Z

Zoran Cutura

Walter Roberson said:
It is undefined behaviour to have a format element for which the
parameter is missing or not of the matching type.


What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack

Mind you, but why would printf() convert a pointer instead of a value
here? I strongly doubt, that any implementation (though I might be
wrong) is doing it as you explain.
 
D

Dale

geek said:
Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Are you trying to print that integers as strings? See if your compiler
supports itoa() and ltoa() functions. (Look in stdlib.h.)
 
R

Richard Tobin

What your system is probably doing is converting whatever happens
to be on the stack, which probably happens to be a pointer with
value 0xff342088. If the same pointer happened to be on the stack
[/QUOTE]
Mind you, but why would printf() convert a pointer instead of a value
here?

It's converting (printing) the value on the stack, which probably *is*
a pointer, as if it were an integer.

-- Richard
 
P

pemo

Dale said:
Are you trying to print that integers as strings? See if your compiler
supports itoa() and ltoa() functions. (Look in stdlib.h.)

Oh, you wait - someone will say sprintf! Besides me of course.
 
W

Walter Roberson

Mind you, but why would printf() convert a pointer instead of a value
here? I strongly doubt, that any implementation (though I might be
wrong) is doing it as you explain.

Let me re-state with a minor wording variation for clarity:

"What your system is probably doing is converting whatever
happens to be on the top of the stack, which is a value which
is 0xff342088, which is probably a pointer."


There is some question about the endianness of the original poster's
machine, so the integer value that printed out with value 0xff342088
might have been stored internally as 0x34, 0xff, 0x88, 0x20
or 0x88, 0x20, 0x34, 0xff or 0x20, 0x88, 0xff, 0x34 or some other
byte order. This confuses the determination of the likely type
of the stray value that happened to be printed out.

The original value was unlikely to have been an IEEE float or
double, as the values implied by any of the possible orderings
are fairly far away from 0 -- on the order of 1e-52 for the largest
of them.

The value is unlikely to have been a string: 0xff and 0x88 are
unlikely as desired ISO-8859-1 characters, and the values look
unlikely for UTF-8 or for any of the UTF variations that call for
"shift codes".

The value is unlikely to have been a random int lying around
in memory -- -13361016 is just a bit too random for that.

Scrambled variations on the value look -unlikely- as virtual
memory pointers.

I have, though, seen stray values in the 0xff342088 range on the
stack, mostly as stack pointers. Well, not quite: on the architecture
I normally use, 0x7f342088 is a plausible virtual stack pointer,
which happens to be upper memory as the high address bit of
memory addresses happens to be reserved in this architecture. But
on an architecture that didn't have that reservation, 0xff342088
could be a plausible stack pointer.

The one last thing I can think of as plausible, is that the
0xff342088 could be a relative branch address to return from the
present function.

Perhaps some day the OP will mention the architecture involved
and supply a short program to reproduce the output, and someone
might get bored enough to analyze the flow to find out exactly
how that -particular- value ended up being the value sitting
on the stack at the time of the undefined behaviour.
 
P

pemo

geek said:
Hi all,

Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Any help would be appreciated.

printf is what's called a variadic function - it has one fixed arg (const
char *), which can be followed by [as far as anyone is really concerned] any
number of other args.

How does printf know how many args it's been passed then?

The first arg is used to inform printf about the further args ... %c says
there's character, %s says there's a string %d an int etc.

So, you're basically lying to printf - you say there's either an int or a
char on the stack following the format string - but they're not there
really - well, the ones you've been expected to pass aren't!
 
F

Flash Gordon

Dale said:
Are you trying to print that integers as strings? See if your compiler
supports itoa() and ltoa() functions. (Look in stdlib.h.)

All os which are completely non-standard. Since sprintf can do what
these do there is absolutely no good reason to use them, always use the
printf family of functions instead.
 
K

Keith Thompson

geek said:
Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything.

-13361016

Any help would be appreciated.

As others have said, both statements invoke undefined behavior. In
both cases, you're lying to printf(), promising to provide an
additional argument that isn't there. It then (probably) grabs
something from wherever the argument *would* have been if you had
provided it, possibly on the stack.

The solution is quite simple: Don't do that.

But it's very likely that printf("%c") *is* printing something; you're
just not seeing it. The "%c" format tells printf to grab an int value
and print it as a character. If the resulting character happens not
to be printable, you may not see any output. If you're curious,
depending on your system, you may be able to capture the output of the
program and examine it (<OT>on Unix, "./myprogram | cat -A"</OT>).

But again, the solution is simple: Don't do that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top