Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problems with indirect width specification

Reply
Thread Tools

problems with indirect width specification

 
 
mastermind
Guest
Posts: n/a
 
      08-07-2012
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.
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-07-2012
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
 
Reply With Quote
 
 
 
 
Juha Nieminen
Guest
Posts: n/a
 
      08-07-2012
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.)
 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      08-07-2012
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.
 
Reply With Quote
 
Paul N
Guest
Posts: n/a
 
      08-07-2012
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?
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-12-2012
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Textbox width scaling to width of data not width of page? AndrewF ASP .Net 1 10-10-2005 04:38 PM
"indirect" ipsec dt1649651@yahoo.com Cisco 3 05-19-2005 08:17 PM
Enterprise Library - Indirect/Inherited Configuration?? plaztik via DotNetMonster.com ASP .Net 1 05-06-2005 03:44 PM
Indirect use of COM George Ter-Saakov ASP .Net 2 08-27-2003 03:03 PM
Can a class call its indirect base's member function? Yu Lianqing C++ 1 07-31-2003 02:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57