simple scanf question

xqwertyx's Avatar, Join Date: Feb 2010
Newbie Member
main()
{
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
0
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
Try a space between your "%d" s. Also, scanf uses the address of the variables so you need to prepend them with "&":

Code:
  scanf("%d %d %d" , &lower, &upper, &step);
0
murugaperumal's Avatar, Join Date: Feb 2010
Go4Expert Member
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

Code:
   scanf("%d%d%d",&lower, &upper, &step);
0
vivekraj's Avatar, Join Date: Feb 2010
Light Poster
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.