By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,264 Members | 1,725 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,264 IT Pros & Developers. It's quick & easy.

Printf("%d")

P: n/a
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

Nov 15 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In article <11**********************@g14g2000cwa.googlegroups .com>,
geek <me********@gmail.com> wrote:
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.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 15 '05 #2

P: n/a
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
In article <11**********************@g14g2000cwa.googlegroups .com>,
geek <me********@gmail.com> wrote:
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


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.

--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 15 '05 #3

P: n/a
"geek" <me********@gmail.com> wrote in news:1128916418.735484.182050
@g14g2000cwa.googlegroups.com:

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.)
Nov 15 '05 #4

P: n/a
In article <ne********************@news.daimlerchrysler.com >,
Zoran Cutura <zo**********@web.de> wrote:
Why does a printf("%d") statement prints this as output and when I do
printf("%c") it doesn't print anything. -13361016
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?


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

-- Richard
Nov 15 '05 #5

P: n/a

"Dale" <da***@gas.orange> wrote in message
news:Xn*************************@204.153.244.156.. .
"geek" <me********@gmail.com> wrote in news:1128916418.735484.182050
@g14g2000cwa.googlegroups.com:

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


Oh, you wait - someone will say sprintf! Besides me of course.
Nov 15 '05 #6

P: n/a
In article <ne********************@news.daimlerchrysler.com >,
Zoran Cutura <zo**********@web.de> wrote:
Walter Roberson <ro******@ibd.nrc-cnrc.gc.ca> wrote:
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.


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.
--
Many food scientists have reported chocolate to be the single most
craved food. -- Northwestern University, 2001
Nov 15 '05 #7

P: n/a

"geek" <me********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
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!

Nov 15 '05 #8

P: n/a
Dale wrote:
"geek" <me********@gmail.com> wrote in news:1128916418.735484.182050
@g14g2000cwa.googlegroups.com:
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.)


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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 15 '05 #9

P: n/a
"geek" <me********@gmail.com> writes:
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.