Printf question

Discussion in 'C Programming' started by anonymous@coolgroups.com, Jan 19, 2004.

  1. Guest

    Hi CLCers,
    The program below prints some number and i dont
    understand why it is behaving like this. I am not supplying
    the necessary arguments to the printf function and
    according to my understanding the program shiould throw
    some error. Please clear my doubts.

    #include<stdio.h>
    int main()
    {
    printf("%d");
    return 0;
    }

    Thanks in advance.

    Sha
     
    , Jan 19, 2004
    #1
    1. Advertising

  2. scribbled the following:
    > Hi CLCers,
    > The program below prints some number and i dont
    > understand why it is behaving like this. I am not supplying
    > the necessary arguments to the printf function and
    > according to my understanding the program shiould throw
    > some error. Please clear my doubts.


    > #include<stdio.h>
    > int main()
    > {
    > printf("%d");
    > return 0;
    > }


    You are creating undefined behaviour. printf() expects an argument,
    because of the format string, but you didn't supply one. This, by
    itself, creates undefined behaviour, so anything might happen,
    including printing random numbers. Most probably it's reading a
    value from the top of the stack, but whatever value that is is
    implementation-defined.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "Roses are red, violets are blue, I'm a schitzophrenic and so am I."
    - Bob Wiley
     
    Joona I Palaste, Jan 19, 2004
    #2
    1. Advertising

  3. pete Guest

    Joona I Palaste wrote:
    >
    > scribbled the following:
    > > Hi CLCers,
    > > The program below prints some number and i dont
    > > understand why it is behaving like this. I am not supplying
    > > the necessary arguments to the printf function and
    > > according to my understanding the program shiould throw
    > > some error. Please clear my doubts.

    >
    > > #include<stdio.h>
    > > int main()
    > > {
    > > printf("%d");
    > > return 0;
    > > }

    >
    > You are creating undefined behaviour. printf() expects an argument,
    > because of the format string, but you didn't supply one. This, by
    > itself, creates undefined behaviour, so anything might happen,
    > including printing random numbers. Most probably it's reading a
    > value from the top of the stack, but whatever value that is is
    > implementation-defined.


    It's undefined.
    It need not be documented and it need not be repeatable.

    --
    pete
     
    pete, Jan 19, 2004
    #3
  4. pete <> scribbled the following:
    > Joona I Palaste wrote:
    >> scribbled the following:
    >> > Hi CLCers,
    >> > The program below prints some number and i dont
    >> > understand why it is behaving like this. I am not supplying
    >> > the necessary arguments to the printf function and
    >> > according to my understanding the program shiould throw
    >> > some error. Please clear my doubts.

    >>
    >> > #include<stdio.h>
    >> > int main()
    >> > {
    >> > printf("%d");
    >> > return 0;
    >> > }

    >>
    >> You are creating undefined behaviour. printf() expects an argument,
    >> because of the format string, but you didn't supply one. This, by
    >> itself, creates undefined behaviour, so anything might happen,
    >> including printing random numbers. Most probably it's reading a
    >> value from the top of the stack, but whatever value that is is
    >> implementation-defined.


    > It's undefined.
    > It need not be documented and it need not be repeatable.


    That's why I said "you are creating undefined behaviour", and that's
    also why I said "most probably" instead of "certainly".

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "'I' is the most beautiful word in the world."
    - John Nordberg
     
    Joona I Palaste, Jan 19, 2004
    #4
  5. spoke thus:

    > int main()
    > {
    > printf("%d");
    > return 0;
    > }


    Your program produces undefined behavior in two ways - one, by failing
    to specify an argument for the %d conversion specifier (as others
    noted), and two, by failing to supply a newline at the end of your
    program's output. Invoking undefined behavior means that the
    implementation is free to do whatever it wants, up to and including
    perpetrating terrorist acts.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 19, 2004
    #5
  6. Christopher Benson-Manica <> scribbled the following:
    > spoke thus:
    >> int main()
    >> {
    >> printf("%d");
    >> return 0;
    >> }


    > Your program produces undefined behavior in two ways - one, by failing
    > to specify an argument for the %d conversion specifier (as others
    > noted), and two, by failing to supply a newline at the end of your
    > program's output. Invoking undefined behavior means that the
    > implementation is free to do whatever it wants, up to and including
    > perpetrating terrorist acts.


    Failing to supply a newline at the end of the program's output causes
    undefined behaviour? Chapter and verse, please?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    "You will be given the plague."
    - Montgomery Burns
     
    Joona I Palaste, Jan 19, 2004
    #6
  7. Joona I Palaste <> spoke thus:

    > Failing to supply a newline at the end of the program's output causes
    > undefined behaviour? Chapter and verse, please?


    Um... can I quote from the book of Revelation? ;( Apparently I
    (badly!) mis-recalled previous discussion on the topic. Thanks for
    the cluon...

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Jan 19, 2004
    #7
  8. pete Guest

    Joona I Palaste wrote:
    >
    > pete <> scribbled the following:
    > > Joona I Palaste wrote:
    > >> scribbled the following:
    > >> > Hi CLCers,
    > >> > The program below prints some number and i dont
    > >> > understand why it is behaving like this. I am not supplying
    > >> > the necessary arguments to the printf function and
    > >> > according to my understanding the program shiould throw
    > >> > some error. Please clear my doubts.
    > >>
    > >> > #include<stdio.h>
    > >> > int main()
    > >> > {
    > >> > printf("%d");
    > >> > return 0;
    > >> > }
    > >>
    > >> You are creating undefined behaviour. printf() expects an argument,
    > >> because of the format string, but you didn't supply one. This, by
    > >> itself, creates undefined behaviour, so anything might happen,
    > >> including printing random numbers. Most probably it's reading a
    > >> value from the top of the stack, but whatever value that is is
    > >> implementation-defined.

    >
    > > It's undefined.
    > > It need not be documented and it need not be repeatable.

    >
    > That's why I said "you are creating undefined behaviour",
    > and that's also why I said "most probably" instead of "certainly".


    There is nothing about the value at the top of the stack
    (assuming a stack)
    which suggests that it is an implementation defined value.

    --
    pete
     
    pete, Jan 19, 2004
    #8
  9. pete Guest

    Joona I Palaste wrote:
    >
    > Christopher Benson-Manica <> scribbled the following:
    > > spoke thus:
    > >> int main()
    > >> {
    > >> printf("%d");
    > >> return 0;
    > >> }

    >
    > > Your program produces undefined behavior in two ways
    > > - one, by failing
    > > to specify an argument for the %d conversion specifier (as others
    > > noted), and two, by failing to supply a newline at the end of your
    > > program's output. Invoking undefined behavior means that the
    > > implementation is free to do whatever it wants, up to and including
    > > perpetrating terrorist acts.

    >
    > Failing to supply a newline at the end of the program's output causes
    > undefined behaviour? Chapter and verse, please?


    It's from the definition of "stream".

    Whether or not the last line of a text stream needs to be
    newline terminated is implementation defined.

    If it isn't newline terminated, then it isn't a portable stream.
    If it isn't a stream, then what is it ?

    --
    pete
     
    pete, Jan 19, 2004
    #9
  10. Dan Pop Guest

    In <bugqbd$2f$> Joona I Palaste <> writes:

    > scribbled the following:
    >> Hi CLCers,
    >> The program below prints some number and i dont
    >> understand why it is behaving like this. I am not supplying
    >> the necessary arguments to the printf function and
    >> according to my understanding the program shiould throw
    >> some error. Please clear my doubts.

    >
    >> #include<stdio.h>
    >> int main()
    >> {
    >> printf("%d");
    >> return 0;
    >> }

    >
    >You are creating undefined behaviour. printf() expects an argument,
    >because of the format string, but you didn't supply one. This, by
    >itself, creates undefined behaviour, so anything might happen,
    >including printing random numbers. Most probably it's reading a
    >value from the top of the stack, but whatever value that is is
    >implementation-defined. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

    ^^^^^^^^^^^^^^^^^^^^^^
    Nope, it ain't. It's *unspecified*, but this doesn't matter, because
    undefined behaviour has *already* been invoked.

    Be *very* careful when using terms like "implementation-defined", because
    their meaning is precisely defined by the C standard (and the definition
    is not always what one would intuitively expect).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 19, 2004
    #10
  11. Dan Pop Guest

    In <bugrkj$10j$> Joona I Palaste <> writes:

    >Christopher Benson-Manica <> scribbled the following:
    >> spoke thus:
    >>> int main()
    >>> {
    >>> printf("%d");
    >>> return 0;
    >>> }

    >
    >> Your program produces undefined behavior in two ways - one, by failing
    >> to specify an argument for the %d conversion specifier (as others
    >> noted), and two, by failing to supply a newline at the end of your
    >> program's output. Invoking undefined behavior means that the
    >> implementation is free to do whatever it wants, up to and including
    >> perpetrating terrorist acts.

    >
    >Failing to supply a newline at the end of the program's output causes
    >undefined behaviour? Chapter and verse, please?


    2 A text stream is an ordered sequence of characters composed into
    lines, each line consisting of zero or more characters
    plus a terminating new-line character. Whether the last line
    requires a terminating new-line character is implementation-defined.

    So, if the implementation requires it, but the program doesn't supply it
    you have undefined behaviour by lack of specification of behaviour.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Jan 19, 2004
    #11
  12. Groovy hepcat Christopher Benson-Manica was jivin' on Mon, 19 Jan 2004
    14:54:48 +0000 (UTC) in comp.lang.c.
    Re: Printf question's a cool scene! Dig it!

    > spoke thus:
    >
    >> int main()
    >> {
    >> printf("%d");
    >> return 0;
    >> }

    >
    >Your program produces undefined behavior in two ways - one, by failing

    ^^^
    Would you believe three ways?

    >to specify an argument for the %d conversion specifier (as others
    >noted), and two, by failing to supply a newline at the end of your


    Three, failing to provide a prototype for printf().

    --

    Dig the even newer still, yet more improved, sig!

    http://alphalink.com.au/~phaywood/
    "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
    I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
     
    Peter Shaggy Haywood, Jan 23, 2004
    #12
    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. Edith Gross
    Replies:
    2
    Views:
    343
    =?iso-8859-1?Q?Juli=E1n?= Albo
    Nov 2, 2003
  2. ben
    Replies:
    4
    Views:
    627
    Martin Ambuhl
    Jun 26, 2004
  3. whatluo

    (void) printf vs printf

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

    printf affects following printf/s

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

Share This Page