Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   simple scanf question (http://www.go4expert.com/forums/simple-scanf-question-t20969/)

xqwertyx 13Feb2010 02:26

simple scanf question
int fahr, celsius;
int lower, upper, step;

scanf("%d%d%d" , lower, upper, step);
fahr = lower;

while (fahr <= upper) {
celsius = 5 / 9 * (fahr-32) ;
printf("%d\t%d\n" , fahr, celsius);
fahr = fahr +step;

I just started learning C and wanted to play with scanf,,
When i run the above code and type in numbers, the command prompt crashes..
is this a problem with my code or computer?
thank you

Gene Poole 13Feb2010 03:30

Re: simple scanf question
Try a space between your "%d" s. Also, scanf uses the address of the variables so you need to prepend them with "&":


  scanf("%d %d %d" , &lower, &upper, &step);

murugaperumal 1Mar2010 09:44

Re: simple scanf question
Dear Friend,

In the code
scanf("%d%d%d",lower, upper, step);

Here space between the "%d" is not must. You can give or no need. But "& " is must for each variable,which is used in the scanf.

The correct code is


  scanf("%d%d%d",&lower, &upper, &step);

vivekraj 1Mar2010 10:37

Re: simple scanf question
As muruga said,there is no rule to leave space between the format specifiers such as %d in your example.According to your program,you should use & before the lower,upper and step variables.Because,scanf expects the pointer as its argument after the format specifier.

scanf will read the input and store the given input in the specified location pointed to by the pointer specified.

If you use array,then there is no need to use &.Because,array generally points to the memory location directly.

All times are GMT +5.5. The time now is 14:04.