Trouble with formats in printf

Discussion in 'C Programming' started by Sanjay Kulkarni, Mar 21, 2007.

  1. I am facing some problems while trying to explore the printf
    statement:

    Expected Actual
    Result Statement Result

    1 printf("x = %d", 18.0 / 14); 18725
    5.0 printf("\nx = %f", 5); 0.000000
    5 printf("\nx = %d", 5.0); 0

    What am I missing? Is there some issue with floats?
    I have included conio.h, stdio.h, float.h and math.h.

    - Sanjay Kulkarni
     
    Sanjay Kulkarni, Mar 21, 2007
    #1
    1. Advertising

  2. Sanjay Kulkarni said:

    > I am facing some problems while trying to explore the printf
    > statement:
    >
    > Expected Actual
    > Result Statement Result
    >
    > 1 printf("x = %d", 18.0 / 14); 18725
    > 5.0 printf("\nx = %f", 5); 0.000000
    > 5 printf("\nx = %d", 5.0); 0
    >
    > What am I missing?


    18.0 / 14 is a double, not an int, but you promised printf an int. Since
    you lied, printf is allowed to sulk.

    5 is an int, not a double, but you promised printf a double. Since you
    lied, printf is allowed to sulk.

    5.0 is a double, not an int, but you promised printf an int. Since you
    lied, printf is allowed to sulk.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Mar 21, 2007
    #2
    1. Advertising

  3. Sanjay Kulkarni wrote:
    > I am facing some problems while trying to explore the printf
    > statement:
    >
    > Expected Actual
    > Result Statement Result
    >
    > 1 printf("x = %d", 18.0 / 14); 18725


    18.0 / 14 is a floating point value. It makes no sense to print it
    with "%d".
    18 / 14 is an integral number. Try that.

    > 5.0 printf("\nx = %f", 5); 0.000000


    5 is an integer. It makes no sense to print it with "%f".

    > 5 printf("\nx = %d", 5.0); 0


    5.0 is a floating point value. It makes no sense to print it with "%d".

    > What am I missing? Is there some issue with floats?


    In all these cases you are producing undefined behavior. The issue is
    with knowing the difference between a floating point number and an
    integer. Further, there are no floats at all in the above, so it can
    hardly be "some issue with floats".

    > I have included conio.h, stdio.h, float.h and math.h.


    It's a good thing you didn't post that code with the non-standard <conio.h>.
     
    Martin Ambuhl, Mar 21, 2007
    #3
  4. Sanjay Kulkarni <> wrote:
    > I am facing some problems while trying to explore the printf
    > statement:


    > Expected Actual
    > Result Statement Result


    > 1 printf("x = %d", 18.0 / 14); 18725


    Here you tell printf() via the '%d' format specifier that the second
    variable you're going to pass it wil be an integer. But you're lying
    and pass it a floating point variable instead.

    > 5.0 printf("\nx = %f", 5); 0.000000


    Here you tell it to expect a float (or double) but you pass it an int.

    > 5 printf("\nx = %d", 5.0); 0


    And here, like in the first case you promise it an int but pass it
    a double.

    > What am I missing? Is there some issue with floats?


    There's no issue with floats, there's an issue with your use of
    printf(). You _must_ pass printf() a variable of the type you
    promised it you would send via the format specifiers or all bets
    are off. You have to be very careful about this with all functions
    that take variable number of arguments since the compiler can't
    correct any mistakes you make.

    > I have included conio.h, stdio.h, float.h and math.h.


    <conio.h> isn't a standard C header file, avoid if if you can.
    And of all the rest you only need <stdio.h> - at least for the
    three lines you have shown here.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Mar 21, 2007
    #4
  5. Sanjay Kulkarni <> wrote:
    >What am I missing?


    That printf() won't automatically convert your parameters to the correct
    type. You have to make sure they are the correct type.

    -Beej
     
    Beej Jorgensen, Mar 21, 2007
    #5
  6. Sanjay Kulkarni

    Army1987 Guest


    > 1 printf("x = %d", 18.0 / 14); 18725

    printf("x = %d", (int)18.0/14);
    (Why not simply use 18/14, of course?)

    > 5.0 printf("\nx = %f", 5); 0.000000

    printf("\nx = %d", (double)5);
    (Why not simply use 5.0, of course?)

    > 5 printf("\nx = %d", 5.0); 0

    printf("\nx = %d", (int)5.0);
    (Why not simply use 5, of course?)

    Sometimes, values aren't automatically casted to the right type.
    Use (appropriate_type)expression to do that.

    BTW, why do you put newlines at the beginning of strings rather than at the
    end of the previous ones?
    This way you'll need an extra \n at the end of the program to prevent the
    prompt from showing on the same line of the last output.
    Using strings like "x = %d\n" everywhere needed will be more consistent.
     
    Army1987, Mar 21, 2007
    #6
    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. ben
    Replies:
    4
    Views:
    623
    Martin Ambuhl
    Jun 26, 2004
  2. David Mathog

    printf formats for size_t?

    David Mathog, May 12, 2005, in forum: C Programming
    Replies:
    6
    Views:
    4,831
    Clark S. Cox III
    May 13, 2005
  3. whatluo

    (void) printf vs printf

    whatluo, May 26, 2005, in forum: C Programming
    Replies:
    29
    Views:
    1,249
  4. Guenther Sohler

    Problem with printf formats

    Guenther Sohler, Dec 20, 2005, in forum: C Programming
    Replies:
    16
    Views:
    508
    Joe Wright
    Dec 21, 2005
  5. azza

    printf affects following printf/s

    azza, Oct 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    433
Loading...

Share This Page