Re: scanf and conversion problem



On 10月30日, 下午12时16分, Peter Nilsson <ai...@xxxxxxxxxxx> wrote:
yxxxxy <yxx_ha...@xxxxxxxxxxx> wrote:
Hi, this is a part of my program code.
i want to ask two questions.

int time;
float rate;
float salary;

printf("Enter # of hours worked (-1 to end):");
scanf("%d",&time);

while (time!=-1) {
printf("Enter hourly rate of the worker ($00.00):");
1 scanf("%f",&rate);
if (time>=40)
2 salary=(time-40)*3/2*rate+40*rate;
else
salary=time*rate;

printf("Salary is $%.2f\n",salary);

printf("Enter # of hours worked (-1 to end):");
scanf("%d",&time);
}

first question:
there is no error and warning.
but
when i change 3/2 to 1.5 (line 1), it will reveals a
warning :conversion from 'double' to 'float', possible
loss of data why?

Because multiplying an integer by 3 and dividing by 2
produces an integer. Multiplying an integer by 1.5 will
produce a double (since 1.5 is of type double). As
doubles usually have more precision that floats, the
compiler has decided to warn that you are storing a
double value into a float. [It doesn't have to, but
yours did.]

second question:
when i change "%f" to " %f",there is no change in result.

Read the spec for scanf. All you've done is asked scanf
to ignore optional leading whitespace twice.

but when i change it to "%f " , it will stop at
this line .

Because a space is a conversion directive in its own right.
Find out what it is by reading the specs.

and when i change it to "% f",

That is not a valid conversion directive. Undefined
behaviour is allowed to do anything.

Big Tip: C is the worst language to learn by experimentation.

--
Peter- 隐藏被引用文字 -

- 显示引用的文字 -

Thank you very much!
And thank your tip!
.



Relevant Pages

  • Re: scanf and conversion problem
    ... there is no error and warning. ... double value into a float. ... Because a space is a conversion directive in its own right. ...
    (comp.lang.c)
  • Re: pointer and array
    ... impossible to compare the values, ... int 3 and the float 3.14, that are equal when we convert them both ... the "double" 33554433.0 becomes 33554432.0 after conversion to ...
    (comp.lang.c)
  • RE: multiple repeatable "could not bulk copy out of table" errors
    ... in Query Analyzer or your favorite query execution tool. ... implicitly converted to float values thereby causing the conversion error. ... > new Agent action and object ID/Error from snapshot output: ...
    (microsoft.public.sqlserver.replication)
  • Re: IBM. PL/I and DPF
    ... "assigned" to an IEEE 754r decimal floating point receiving item, ... form of FLOAT, maybe FLOAT DEC, which is done using ... that when squared yields 2 ... no automatic conversion for generic functions. ...
    (comp.lang.pl1)
  • Re: Type casting
    ... I have a small mess with the conversion of numeric types in the ... the outcome of E / 2 cannot be of type float. ... trying to truncate a float to an integer, ... Float'Truncation truncates F to an integer, truncating toward zero, ...
    (comp.lang.ada)