problems with indirect width specification

M

mastermind

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.
 
V

Victor Bazarov

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
 
J

Juha Nieminen

mastermind said:
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.)
 
T

terminator

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.
 
P

Paul N

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?
 
J

James Kanze

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.
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top