Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > why the printf is not typecasting the int to float

Reply
Thread Tools

why the printf is not typecasting the int to float

 
 
jayapal
Guest
Posts: n/a
 
      12-12-2007


#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.
 
Reply With Quote
 
 
 
 
jameskuyper@verizon.net
Guest
Posts: n/a
 
      12-12-2007
jayapal wrote:
> #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.

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-12-2007
jayapal wrote:
>
> #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);
 
Reply With Quote
 
user923005
Guest
Posts: n/a
 
      12-12-2007
On Dec 12, 11:12 am, jayapal <jayapal...@gmail.com> wrote:
> #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;
}

 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      12-12-2007
In article <a1c0f3d0-8065-47c5-bd8c->,
user923005 <> wrote:
>On Dec 12, 11:12 am, jayapal <jayapal...@gmail.com> wrote:
>> #include <stdio.h>


>> main ()
>> {
>> int k = 10;
>> printf ( "%f\n", k);


>> }


>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) ?

--
"Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us." -- Ecclesiastes
 
Reply With Quote
 
Amandil
Guest
Posts: n/a
 
      12-12-2007
On Dec 12, 2:12 pm, jayapal <jayapal...@gmail.com> wrote:
> #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
 
Reply With Quote
 
Dann Corbit
Guest
Posts: n/a
 
      12-12-2007

"Walter Roberson" <> wrote in message
news:fjpe4i$cum$...
> In article
> <a1c0f3d0-8065-47c5-bd8c->,
> user923005 <> wrote:
>>On Dec 12, 11:12 am, jayapal <jayapal...@gmail.com> wrote:
>>> #include <stdio.h>

>
>>> main ()
>>> {
>>> int k = 10;
>>> printf ( "%f\n", k);

>
>>> }

>
>>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 ?


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?



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Float to int conversion by using two int variables for representation of the float variable k3n3dy C++ 15 04-20-2006 06:53 PM
Float to int conversions (was: int(float(sys.maxint)) buglet) Nick Coghlan Python 0 12-06-2004 12:12 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57