Printf("%d")

Discussion in 'C Programming' started by geek, Oct 10, 2005.

  1. geek

    geek Guest

    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
     
    geek, Oct 10, 2005
    #1
    1. Advertising

  2. In article <>,
    geek <> 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
     
    Walter Roberson, Oct 10, 2005
    #2
    1. Advertising

  3. geek

    Zoran Cutura Guest

    Walter Roberson <-cnrc.gc.ca> wrote:
    > In article <>,
    > geek <> 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 ()
    "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
     
    Zoran Cutura, Oct 10, 2005
    #3
  4. geek

    Dale Guest

    "geek" <> 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.)
     
    Dale, Oct 10, 2005
    #4
  5. In article <newscache$zo85oi$g63$>,
    Zoran Cutura <> 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
     
    Richard Tobin, Oct 10, 2005
    #5
  6. geek

    pemo Guest

    "Dale" <> wrote in message
    news:Xns96EB57FB64FA210384893247@204.153.244.156...
    > "geek" <> 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.
     
    pemo, Oct 10, 2005
    #6
  7. In article <newscache$zo85oi$g63$>,
    Zoran Cutura <> wrote:
    >Walter Roberson <-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
     
    Walter Roberson, Oct 10, 2005
    #7
  8. geek

    pemo Guest

    "geek" <> wrote in message
    news:...
    > 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!
     
    pemo, Oct 10, 2005
    #8
  9. geek

    Flash Gordon Guest

    Dale wrote:
    > "geek" <> 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.
     
    Flash Gordon, Oct 10, 2005
    #9
  10. "geek" <> 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) <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.
     
    Keith Thompson, Oct 10, 2005
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Polite

    java printf

    Alex Polite, Jun 5, 2004, in forum: Java
    Replies:
    18
    Views:
    1,238
    Dale King
    Apr 15, 2006
  2. ben
    Replies:
    4
    Views:
    657
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,325
  4. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    455
  5. guru
    Replies:
    8
    Views:
    301
Loading...

Share This Page