473,772 Members | 2,564 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why the printf is not typecasting the int to float



#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.
Dec 12 '07 #1
4 1932
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.

Dec 12 '07 #2
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);
Dec 12 '07 #3
On Dec 12, 11:12 am, jayapal <jayapal...@gma il.comwrote:
#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;
}

Dec 12 '07 #4
In article <a1************ *************** *******@q77g200 0hsh.googlegrou ps.com>,
user923005 <dc*****@connx. comwrote:
>On Dec 12, 11:12 am, jayapal <jayapal...@gma il.comwrote:
>#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
Dec 12 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
3913
by: Yogesh Khanolkar | last post by:
Hi, I am getting incorrect o/p from the code below: #include<stdio.h> #include<float.h> #include<limits.h> main() { double val,val1;
4
2811
by: Niklaus | last post by:
Hi, I would like to know more about casts.What exactly happens when casts are applied to a variable.I know that if an object of type int is applied an cast of float the result would be of type float. 1) What i would like to know is the about the internals when a cast is applied ? Say we have
2
2179
by: dis | last post by:
The following code introduces a 'generic function pointer' p. When calling the pointed-to function via this p, one has to typecast p towards a pointer to the type of the pointed-to function. My question is how to do this if the pointed-to function is a K&R style declared function, like f. The best I could come up with is a typecast towards a pointer to a function with unspecified number and type of parameters (and appropriate return type),...
3
2021
by: nandh_dp | last post by:
When the below program is compiled and executed, #include <stdio.h> #define MASK 0xFFFFFFULL main() { unsigned long long a; unsigned long b, c;
7
2187
by: Raghu | last post by:
Hello All, I need some help regarding overloading operation. Is there any way to overload typecasting? I mean if i have a piece of code as below. int a = 2: float b; b = (float)a;
2
2311
by: Jude | last post by:
here is the source code: #include<stdio.h> int main() { float f; scanf("%d%f",&f); printf("The float is %10.5f\n",f); return 0; } when I input 12345.11111,the output is 12345.11133.
34
15993
by: Old Wolf | last post by:
Is there any possible situation for printf where %hd causes a different result to %d, and the corresponding argument was of type 'short int' ?
8
2671
by: quarkLore | last post by:
Following program is erroneous as I am passing a float as int. The output is --- | V Int f is 0 float f is 0.000000 float f is 3.000000 int f is 0
29
11116
by: candy_init | last post by:
Hi all, I just came across the following program: #include <stdio.h> int main() { float a = 12.5; printf("%d\n", a); printf("%d\n", *(int *)&a); return 0;
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3610
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.