You're using the wrong format specifier to printf.

The %d format specifier expects an int argument, but you're passing a double. Using the wrong format specifier invokes undefined behavior.

To print a double, use %f.

printf("average: %f\n", average);
Answer from dbush on Stack Overflow
🌐
W3Schools
w3schools.com › c › c_type_conversion.php
C Data Type Conversion
As another example, if you divide two integers: 5 by 2, you know that the sum is 2.5. And as you know from the beginning of this page, if you store the sum as an integer, the result will only display the number 2. Therefore, it would be better to store the sum as a float or a double, right? float sum = 5 / 2; printf("%f", sum); // 2.000000 Try it Yourself » · Why is the result 2.00000 and not 2.5? Well, it is because 5 and 2 are still integers in the division. In this case, you need to manually convert the integer values to floating-point values.
Discussions

How to Convert double to int in C? - Stack Overflow
double a; a = 3669.0; int b; b = a; I am getting 3668 in b, instead of 3669. How do I fix This problem? And if have 3559.8 like that also I want like 3559 not 3560. More on stackoverflow.com
🌐 stackoverflow.com
c - Convert int to double - Stack Overflow
I ran this simple program, but when I convert from int to double, the result is zero. The sqrt of the zeros then displays negative values. This is an example from an online tutorial so I'm not su... More on stackoverflow.com
🌐 stackoverflow.com
C convert int to double in a function - Stack Overflow
Im having a problem getting the same result from math equation when I use a function. Here is my code. #include int main () { int a=5, b=2; double c; c = (double) a/b;... More on stackoverflow.com
🌐 stackoverflow.com
April 4, 2016
convert int to double
aryn weatherly is having issues with: I've tried working this several different ways and keep making things worse and worse. please help More on teamtreehouse.com
🌐 teamtreehouse.com
1
March 20, 2020
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › how-can-i-cast-an-int-to-a-double-c-435814
How can I cast an int to a double (C)?
How can I cast an int, stored in a variable, to a double? I am trying to do this: Code: double d; d = (double)strlen(str) \ (double)cols Its obviously
🌐
W3Schools Blog
w3schools.blog › home › how to convert double to int in c
How to Convert double to int in C - W3schools.blog
June 30, 2022 - [ad_1] How to Convert double to int in C double a = 12.34; int b = a ; //b = 12 not 12.34 int to double c int vIn = 0; double vOut = (double)vIn; [ad_2] Please follow and like us:
Top answer
1 of 5
74

I suspect you don't actually have that problem - I suspect you've really got:

double a = callSomeFunction();
// Examine a in the debugger or via logging, and decide it's 3669.0

// Now cast
int b = (int) a;
// Now a is 3668

What makes me say that is that although it's true that many decimal values cannot be stored exactly in float or double, that doesn't hold for integers of this kind of magnitude. They can very easily be exactly represented in binary floating point form. (Very large integers can't always be exactly represented, but we're not dealing with a very large integer here.)

I strongly suspect that your double value is actually slightly less than 3669.0, but it's being displayed to you as 3669.0 by whatever diagnostic device you're using. The conversion to an integer value just performs truncation, not rounding - hence the issue.

Assuming your double type is an IEEE-754 64-bit type, the largest value which is less than 3669.0 is exactly

3668.99999999999954525264911353588104248046875

So if you're using any diagnostic approach where that value would be shown as 3669.0, then it's quite possible (probable, I'd say) that this is what's happening.

2 of 5
7
main() {
    double a;
    a=3669.0;
    int b;
    b=a;
    printf("b is %d",b);
  
}

output is :b is 3669

when you write b=a; then its automatically converted in int

see on-line compiler result : 

http://ideone.com/60T5b


This is called Implicit Type Conversion Read more here https://www.geeksforgeeks.org/implicit-type-conversion-in-c-with-examples/

🌐
YouTube
youtube.com › watch
How to Convert an int to a double in C Without Precision Issues - YouTube
Learn how to convert integers that represent decimal values to double in C. This guide helps maintain precision during JSON API integration.---This video is ...
Published   March 28, 2025
Views   11
🌐
Quora
quora.com › In-C-can-you-type-cast-a-double-into-an-int-How-is-it-treated
In C, can you type cast a double into an int? How is it treated? - Quora
Check range before converting when inputs may be large, NaN, or infinities. Use functions like lrint(), lround(), llround() (from wzxhzdk:0) when you want well-defined rounding behavior and error handling. For portable safety, compare against INT_MIN, INT_MAX (from wzxhzdk:1) and handle exceptional values explicitly. ... Behavior described is based on the C11 and C17 standards; implementations may add specifics. ... RelatedWhen you cast an int to a double in C++, how is it done at the bit level, since integer and floating point numbers have different bit-level representations?
Find elsewhere
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 67369-how-convert-integer-double.html
How to convert an integer to double?
August 6, 2005 - No need for a cast at all, the conversion is implicit. Just assign the int to the double. Who said he was assigning it to something?
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_type_casting.htm
Type Casting in C
You can use the typecasting operator to explicitly convert the values from one type to another − ... #include <stdio.h> int main() { int sum = 17, count = 5; double mean; mean = sum / count; printf("Value of mean: %f\n", mean); }
🌐
Quora
quora.com › How-do-you-convert-double-to-int-in-C
How to convert double to int in C - Quora
And of course the standard doesn’t guarantee even that much for undefined behavior; a compiler is allowed to emit code that steals your car and runs over your grandma instead. If you specifically want, say, the value mod 1<<32L, or pegged at INT_MAX, or whatever, you need to explicit write code that does that to the double, not just notice that it seems to do that on one compiler and therefore assume that’s what will always happen.
🌐
Cppreference
en.cppreference.com › w › c › language › conversion.html
Implicit conversions - cppreference.com
March 31, 2025 - double d = 0.1; // d = ... to void and back, its value compares equal to the original pointer. No other guarantees are offered. int* p = malloc(10 * sizeof(int)); // malloc returns void*...
🌐
Reddit
reddit.com › r/learnprogramming › how to concert two integers into one double in c
r/learnprogramming on Reddit: How to concert two integers into one double in C
December 2, 2020 -

Hey,

i have two int variables (hours, minutes) and one double variable (driven miles/km).

I want to calculate the average speed (distancy / time).

I thought about: averagespeed = miles/km / (hours + (minutes / 60));

but that gives me the warning: different types in assignment (possible loss of date)

Is it possible to convert the two int variable into one double hours.minutes?

🌐
Quora
quora.com › How-do-programmers-convert-an-integer-to-float-or-double-precision
How do programmers convert an integer to float or double precision? - Quora
The answer is, as always, it depends! If one is programming in a higher level language like Java or C, one simply calls the appropriate subroutine, whose name will undoubtedly contain the word “float”. Even if ...
🌐
TutorialsPoint
tutorialspoint.com › article › cplusplus-program-to-convert-int-variables-into-double
C++ Program to convert int Variables into double
October 19, 2022 - One is the C-styled version and another one is the function-styled casting. We mention the resultant datatype before the source variable or the value enclosed within brackets. int input = <integer value>; double output = (double) input;