473,414 Members | 1,667 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

scanf and conversion problem

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?

second question:
when i change "%f" to " %f",there is no change in result.
but when i change it to "%f " , it will stop at this line .
and when i change it to "% f",it will output a wrong num and not let
me print second time .
why?

my english is not good,if i didn't express my idea clearly ,please
forgive me.

Thank you very much.
Oct 30 '08 #1
3 2281
yxxxxy <yxx_ha...@hotmail.comwrote:
* 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
Oct 30 '08 #2
On Wed, 29 Oct 2008 21:16:36 -0700 (PDT), Peter Nilsson
<ai***@acay.com.auwrote:
>
Big Tip: C is the worst language to learn by experimentation.
Amen!
Oct 30 '08 #3
On 10ÔÂ30ÈÕ, ÏÂÎç12ʱ16·Ö, Peter Nilsson <ai...@acay.com.auwrote:
yxxxxy <yxx_ha...@hotmail.comwrote:
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!
Oct 31 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

57
by: Eric Boutin | last post by:
Hi ! I was wondering how to quickly and safely use a safe scanf( ) or gets function... I mean.. if I do : char a; scanf("%s", a); and the user input a 257 char string.. that creates a...
12
by: B Thomas | last post by:
Hi, I was reading O'Reilly's "Practical C programming" book and it warns against the use of scanf, suggesting to avoid using it completely . Instead it recomends to use using fgets and sscanf....
6
by: Pete | last post by:
Following is a fragment from a program I'm writing. I am trying to enter an 8 bit binary representation from keyboard which is then scanfed into &Ptext and then again into &Key1. This is not binary...
5
by: sreelal | last post by:
Dear all, plz see the following prograam. main() { int a,b; scanf("%d %d ",&a,&b);
11
by: Mars | last post by:
char c; int i; scanf("%d",&i); c=getchar(); I want to read a integer and a character in 2 lines. Why the getchar always get the \n character from the last line????? (Sorry for my newbie...
11
by: QQ | last post by:
Hello I am running this code int main(void) { char A,B; printf("Please input A: \n"); scanf("%2s",A); printf("Please input B: \n"); scanf("%2s",B); printf("A is %s,B is %s\n",A,B); }
33
by: Lalatendu Das | last post by:
Dear friends, I am getting a problem in the code while interacting with a nested Do-while loop It is skipping a scanf () function which it should not. I have written the whole code below. Please...
30
by: James Daughtry | last post by:
char array; scanf("%19s", &array); I know this is wrong because it's a type mismatch, where scanf expects a pointer to char and gets a pointer to an array of 20 char. I know that question 6.12...
9
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <=...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.