why the printf is not typecasting the int to float

Discussion in 'C Programming' started by jayapal, Dec 12, 2007.

  1. jayapal

    jayapal Guest

    #include <stdio.h>

    main ()
    {
    int k = 10;
    printf ( "%f\n", k);

    }


    o/p ................... run time error



    Why this program is showing run time error ... Is there any wrong in
    these code part.
    jayapal, Dec 12, 2007
    #1
    1. Advertising

  2. jayapal

    Guest

    jayapal wrote:
    > #include <stdio.h>
    >
    > main ()
    > {
    > int k = 10;
    > printf ( "%f\n", k);
    >
    > }
    >
    >
    > o/p ................... run time error
    >
    >
    >
    > Why this program is showing run time error ... Is there any wrong in
    > these code part.


    The printf() family uses the variadic function interface (see
    <stdarg.h>), or at least one that is functionally equivalent to it.
    When you call variadic functions, variable arguments are subject only
    to the default argument promotions. The promoted type (in this case
    'int') must match exactly the type specified in the format string
    (which in this case is 'double'). You can explicitly convert k to
    double, or you can change the format specifier to 'd', but one way or
    another you have to make them match, or the behavior is undefined.
    , Dec 12, 2007
    #2
    1. Advertising

  3. jayapal wrote:
    >
    > #include <stdio.h>
    >
    > main ()
    > {
    > int k = 10;
    > printf ( "%f\n", k);

    ^^
    The compiler sees as arguments a string and an int. The usual promotion
    rules for arguments call for no promotion.
    The "%f" is part of a string interpreted by the printf function and is
    meaningless as far as compilation is concerned except as chars in a string.
    So the printf function is passed an int, is told that it is a double,
    and promptly chokes, as can be expected.

    If you want to printf the value of k as floating-point, you must make it
    one:
    printf("%f\n", (double)k);
    Martin Ambuhl, Dec 12, 2007
    #3
  4. jayapal

    user923005 Guest

    On Dec 12, 11:12 am, jayapal <> wrote:
    > #include <stdio.h>
    >
    > main ()
    > {
    > int k = 10;
    > printf ( "%f\n", k);
    >
    > }
    >
    > o/p ................... run time error
    >
    > Why this program is showing run time error ... Is there any wrong in
    > these code part.


    There is plenty wrong with it. This should do what you want. Look at
    each difference between this program and your program. Each
    difference is important.

    #include <stdio.h>
    int main(void)
    {
    const int k = 10;
    printf("%f\n", (double) k);
    return 0;
    }
    user923005, Dec 12, 2007
    #4
  5. In article <>,
    user923005 <> wrote:
    >On Dec 12, 11:12 am, jayapal <> wrote:
    >> #include <stdio.h>


    >> main ()
    >> {
    >> int k = 10;
    >> printf ( "%f\n", k);


    >> }


    >This should do what you want. Look at
    >each difference between this program and your program. Each
    >difference is important.


    >#include <stdio.h>
    >int main(void)
    >{
    > const int k = 10;
    > printf("%f\n", (double) k);
    > return 0;
    >}


    What is the "important" difference between using int k = 10 or
    const int k = 10 ? Will the abstract behaviour of the program
    be different in -any- way by using 'const' or not using 'const'
    in that program? Is there, for example, a difference in the
    behaviour of the cast to double? Why is it important that k be const
    but that you do not cast k to (const double) ?

    --
    "Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us." -- Ecclesiastes
    Walter Roberson, Dec 12, 2007
    #5
  6. jayapal

    Amandil Guest

    On Dec 12, 2:12 pm, jayapal <> wrote:
    > #include <stdio.h>
    >
    > main ()
    > {
    > int k = 10;
    > printf ( "%f\n", k);
    >
    > }
    >
    > o/p ................... run time error
    >
    > Why this program is showing run time error ... Is there any wrong in
    > these code part.


    printf() is usually declared as something like int printf(const char
    *s, ...);
    This means that the compiler does not not check the arguments at all.
    It just performs the standard progressions (someone else will use the
    correct word), such as converting char and short to int. The function
    caller is responsible for ensuring that each argument is of the
    correct type.

    In your case, an int is being passed (on the stack) but printf(),
    because of the "%f", so internally it calls the stdarg library to
    read a float - which messes up your program. This kind of behavior can
    be warned against with some compilers that recognize printf()-like
    functions. Check your compiler documentation for details.

    What you probably want to do is:

    #include <stdio.h>

    int main()
    {
    int k = 10;
    printf("%f\n", (float)k);
    }

    using a cast, or perhaps make k a float:

    .....
    float k = 10;
    printf("%f\n", k);

    Choose your options, whichever fits better into whatever code you're
    trying to write.

    -- Martie
    Amandil, Dec 12, 2007
    #6
  7. jayapal

    Dann Corbit Guest

    "Walter Roberson" <-cnrc.gc.ca> wrote in message
    news:fjpe4i$cum$...
    > In article
    > <>,
    > user923005 <> wrote:
    >>On Dec 12, 11:12 am, jayapal <> wrote:
    >>> #include <stdio.h>

    >
    >>> main ()
    >>> {
    >>> int k = 10;
    >>> printf ( "%f\n", k);

    >
    >>> }

    >
    >>This should do what you want. Look at
    >>each difference between this program and your program. Each
    >>difference is important.

    >
    >>#include <stdio.h>
    >>int main(void)
    >>{
    >> const int k = 10;
    >> printf("%f\n", (double) k);
    >> return 0;
    >>}

    >
    > What is the "important" difference between using int k = 10 or
    > const int k = 10 ?


    The important difference is that an integer that is not modified by the
    program should be made const.

    > Will the abstract behaviour of the program
    > be different in -any- way by using 'const' or not using 'const'
    > in that program?


    The abstract behavior is not different. The difference is in maintenance of
    such a program. 80% of the cost of software is in maintenance. Anything
    you can do to reduce bugs is worthwhile. While this example is trivial, the
    general practice is what is wanted.

    > Is there, for example, a difference in the
    > behaviour of the cast to double?


    No.

    > Why is it important that k be const
    > but that you do not cast k to (const double) ?


    It is a parameter to a function call. It is only passing a copy anyway. Is
    this a troll?



    --
    Posted via a free Usenet account from http://www.teranews.com
    Dann Corbit, Dec 12, 2007
    #7
    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. bd
    Replies:
    0
    Views:
    621
  2. Nick Coghlan
    Replies:
    0
    Views:
    479
    Nick Coghlan
    Dec 6, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,843
    Smokey Grindel
    Dec 2, 2006
  4. k3n3dy
    Replies:
    15
    Views:
    990
    dan2online
    Apr 20, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,539
    James Kanze
    Oct 8, 2009
Loading...

Share This Page