problems with indirect width specification

Discussion in 'C++' started by mastermind, Aug 7, 2012.

  1. mastermind

    mastermind Guest

    Hi,
    I have some doubts related to indirect width specification.

    Here is the sample code:

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    main()
    {
    float f=20, width_f=3;
    clrscr();
    printf("%*.2f",width_f,f);
    getch();
    }
    
    
    The output of the above code is 0.00; while if I change the data type of width_f from float to int, then the output comes out to be 20.00. I know thatthe default data type for width specification needs to be 'int'. But I am still not clear about the output when the data type is of float type.

    Can anyone help me out with this ?? Thanks in advance.
    mastermind, Aug 7, 2012
    #1
    1. Advertising

  2. On 8/7/2012 10:50 AM, mastermind wrote:
    > I have some doubts related to indirect width specification.
    >
    > Here is the sample code:
    >
    >
    Code:
    > #include<stdio.h>
    > #include<conio.h>
    >
    > main()
    > {
    >    float f=20, width_f=3;
    >    clrscr();
    >    printf("%*.2f",width_f,f);
    >    getch();
    > }
    >
    > 
    >
    > The output [...]
    >
    > Can anyone help me out with this ?? Thanks in advance.


    Somebody in comp.lang.c can help you, probably. C++ relies on the C
    library, but its behavior is actually specified in the C Standard. Talk
    to those guys, down the hall to the left.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 7, 2012
    #2
    1. Advertising

  3. mastermind <> wrote:
    > float f=20, width_f=3;
    > clrscr();
    > printf("%*.2f",width_f,f);
    > getch();
    >
    > The output of the above code is 0.00; while if I change the data
    > type of width_f from float to int, then the output comes out to be
    > 20.00. I know that the default data type for width specification
    > needs to be 'int'. But I am still not clear about the output when
    > the data type is of float type.


    What makes you think that you can supply either a float or an int as the
    field with parameter?

    printf() is completely type-unsafe. It won't do any checks. (Your compiler
    *might* perform some checks if it's smart enough.)
    Juha Nieminen, Aug 7, 2012
    #3
  4. mastermind

    terminator Guest

    On Tuesday, August 7, 2012 7:31:31 PM UTC+4:30, Juha Nieminen wrote:
    > printf() is completely type-unsafe. It won't do any checks. (Your compiler
    >

    agreed.

    the declaration of printf begins with ' extern "C" ' and contains ellipsis to become a C-style variadic function. integral inputs are sent as 'long' and real inputs are cast to double , with C-style variadic functions.

    the above code is an example of UB.

    > *might* perform some checks if it's smart enough.)

    not more than a warning.

    regards,
    FM.
    terminator, Aug 7, 2012
    #4
  5. mastermind

    Paul N Guest

    On Aug 7, 3:50 pm, mastermind <> wrote:
    > Hi,
    > I have some doubts related to indirect width specification.
    >
    > Here is the sample code:
    >
    >
    Code:
    > #include<stdio.h>
    > #include<conio.h>
    >
    > main()
    > {
    >   float f=20, width_f=3;
    >   clrscr();
    >   printf("%*.2f",width_f,f);
    >   getch();
    >
    > }
    >
    > 
    >
    > The output of the above code is 0.00; while if I change the data type of width_f from float to int, then the output comes out to be 20.00. I know that the default data type for width specification needs to be 'int'. But I am still not clear about the output when the data type is of float type.
    >
    > Can anyone help me out with this ?? Thanks in advance.


    I'm guessing to some extent, but I think that your problem is that the
    width needs to be an int, and that printf isn't clever enough to know
    to convert it to one. So it may actually only find and use part of the
    value of the float width_f, and then finds the end of width_f where it
    is expecting to find the beginning of f. So it is trying to print the
    wrong value.

    To fix it, try printf("%*.2f",(int) width_f,f);

    And does "main", rather than "int main", work in C++? Or are you
    actually using a C compiler?
    Paul N, Aug 7, 2012
    #5
  6. mastermind

    James Kanze Guest

    On Tuesday, August 7, 2012 4:23:38 PM UTC+1, terminator wrote:
    > On Tuesday, August 7, 2012 7:31:31 PM UTC+4:30, Juha Nieminen wrote:


    > > printf() is completely type-unsafe. It won't do any checks. (Your compiler


    > agreed.


    > the declaration of printf begins with ' extern "C" ' and
    > contains ellipsis to become a C-style variadic function.
    > integral inputs are sent as 'long' and real inputs are cast to
    > double , with C-style variadic functions.


    There's certainly no requirement that the declaration of printf
    begin with `extern "C"' (although this is frequently the case).
    And the type actually sent is the result of integral promotion:
    int for char, short and int, long for long and long long for
    long long. (For the unsigned integral types, it's even more
    complex, and partially implementation defined.)

    > the above code is an example of UB.


    > > *might* perform some checks if it's smart enough.)


    > not more than a warning.


    And only then if the format string is a string literal. Which
    is almost never the case in internationalized software. (IIRC,
    g++ is capable of checking through gettext as well, against the
    default string. But that still doesn't protect you against the
    translator who converts %d into %s.)

    --
    James
    James Kanze, Aug 12, 2012
    #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. George Ter-Saakov

    Indirect use of COM

    George Ter-Saakov, Aug 26, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    337
    Natty Gur
    Aug 27, 2003
  2. plaztik via DotNetMonster.com

    Enterprise Library - Indirect/Inherited Configuration??

    plaztik via DotNetMonster.com, May 6, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    501
    Scott Allen
    May 6, 2005
  3. AndrewF
    Replies:
    1
    Views:
    741
    Bruce Barker
    Oct 10, 2005
  4. Yu Lianqing
    Replies:
    1
    Views:
    363
    John Harrison
    Jul 31, 2003
  5. Steven T. Hatton

    Indirect recursion and templates?

    Steven T. Hatton, Apr 22, 2004, in forum: C++
    Replies:
    2
    Views:
    433
    Steven T. Hatton
    Apr 22, 2004
Loading...

Share This Page