Scanf

main()
{
float a;
printf("enter a number =");
scanf("%f",&a);
printf("%d", a);
}

it is returning 0. my question is it is reading the value of a as float but what happens then? how is it executing 0?
Hi,

Google "C++ printf example" and carefully read what the % arguments mean. This should help you work out what went wrong.

The scanf function returns a value so you should make use of that to see that it worked. This isn't related to your current problem, but is a good practice.

If you are going to use any library function, always google it to see how it works.

Hope this helps
A program returns an error code to the operating system.
A value of 0 usually means "all OK", and is the default in C99 and C++98 even if your main() is missing a return 0;.

In Windows you can manually check the last error code from the Console with:
echo %errorlevel%


In Linux, from the Shell:
echo $?


If neither my reply nor TheIdeasMan's reply helps, please rephrase your question.

Edit: grammar.
Last edited on
It is an error to call printf with arguments whose types do not match the format specifiers.

Assuming that by "returning" you mean "displaying", I guess your compiler passes floats and ints in different CPU registers (gcc does that, for example) so that when it looks for an int as instructed by %d, it finds whatever was in that register earlier, in this case,zero.
Last edited on
Topic archived. No new replies allowed.