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?