Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   problems with indirect width specification (http://www.velocityreviews.com/forums/t949221-problems-with-indirect-width-specification.html)

mastermind 08-07-2012 02:50 PM

problems with indirect width specification
 
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.

Victor Bazarov 08-07-2012 02:53 PM

Re: problems with indirect width specification
 
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

Juha Nieminen 08-07-2012 03:01 PM

Re: problems with indirect width specification
 
mastermind <fzdudit@gmail.com> 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.)

terminator 08-07-2012 03:23 PM

Re: problems with indirect width specification
 
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.

Paul N 08-07-2012 09:38 PM

Re: problems with indirect width specification
 
On Aug 7, 3:50*pm, mastermind <fzdu...@gmail.com> 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?

James Kanze 08-12-2012 06:27 PM

Re: problems with indirect width specification
 
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


All times are GMT. The time now is 08:49 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.