473,385 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1918
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...@gmail.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**********************************@q77g2000hsh. googlegroups.com>,
user923005 <dc*****@connx.comwrote:
>On Dec 12, 11:12 am, jayapal <jayapal...@gmail.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
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
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...
2
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...
3
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
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
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
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
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
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;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.