#include <stdio.h>
main ()
{
int k = 10;
printf ( "%f\n", k);
}
o/p ................... run time error
Why this program is showing run time error ... Is there any wrong in
these code part.
printf() is usually declared as something like int printf(const char
*s, ...);
This means that the compiler does not not check the arguments at all.
It just performs the standard progressions (someone else will use the
correct word), such as converting char and short to int. The function
caller is responsible for ensuring that each argument is of the
correct type.
In your case, an int is being passed (on the stack) but printf(),
because of the "%f", so internally it calls the stdarg library to
read a float - which messes up your program. This kind of behavior can
be warned against with some compilers that recognize printf()-like
functions. Check your compiler documentation for details.
What you probably want to do is:
#include <stdio.h>
int main()
{
int k = 10;
printf("%f\n", (float)k);
}
using a cast, or perhaps make k a float:
.....
float k = 10;
printf("%f\n", k);
Choose your options, whichever fits better into whatever code you're
trying to write.
-- Martie