why the printf is not typecasting the int to float

J

jayapal

#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.
 
J

jameskuyper

jayapal said:
#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.

The printf() family uses the variadic function interface (see
<stdarg.h>), or at least one that is functionally equivalent to it.
When you call variadic functions, variable arguments are subject only
to the default argument promotions. The promoted type (in this case
'int') must match exactly the type specified in the format string
(which in this case is 'double'). You can explicitly convert k to
double, or you can change the format specifier to 'd', but one way or
another you have to make them match, or the behavior is undefined.
 
M

Martin Ambuhl

jayapal said:
#include <stdio.h>

main ()
{
int k = 10;
printf ( "%f\n", k);
^^
The compiler sees as arguments a string and an int. The usual promotion
rules for arguments call for no promotion.
The "%f" is part of a string interpreted by the printf function and is
meaningless as far as compilation is concerned except as chars in a string.
So the printf function is passed an int, is told that it is a double,
and promptly chokes, as can be expected.

If you want to printf the value of k as floating-point, you must make it
one:
printf("%f\n", (double)k);
 
U

user923005

#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.

There is plenty wrong with it. This should do what you want. Look at
each difference between this program and your program. Each
difference is important.

#include <stdio.h>
int main(void)
{
const int k = 10;
printf("%f\n", (double) k);
return 0;
}
 
W

Walter Roberson

This should do what you want. Look at
each difference between this program and your program. Each
difference is important.
#include <stdio.h>
int main(void)
{
const int k = 10;
printf("%f\n", (double) k);
return 0;
}

What is the "important" difference between using int k = 10 or
const int k = 10 ? Will the abstract behaviour of the program
be different in -any- way by using 'const' or not using 'const'
in that program? Is there, for example, a difference in the
behaviour of the cast to double? Why is it important that k be const
but that you do not cast k to (const double) ?
 
A

Amandil

#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
 
D

Dann Corbit

Walter Roberson said:
What is the "important" difference between using int k = 10 or
const int k = 10 ?

The important difference is that an integer that is not modified by the
program should be made const.
Will the abstract behaviour of the program
be different in -any- way by using 'const' or not using 'const'
in that program?

The abstract behavior is not different. The difference is in maintenance of
such a program. 80% of the cost of software is in maintenance. Anything
you can do to reduce bugs is worthwhile. While this example is trivial, the
general practice is what is wanted.
Is there, for example, a difference in the
behaviour of the cast to double?
No.

Why is it important that k be const
but that you do not cast k to (const double) ?

It is a parameter to a function call. It is only passing a copy anyway. Is
this a troll?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top