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
🌐
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
Discussions

Data type conversion int to double withi - C++ Forum
Both a and b are int, so it is clear that the compiler uses #1 for a / b. If you do cast both a and b to double, then the use of #2 is obvious. What if only one side is double, as in static_cast(a) / b? The compiler has two options: 1. Implicitly convert b to double and then call #2 2. More on cplusplus.com
🌐 cplusplus.com
September 29, 2016
c - Convert int to double - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... 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 sure why this is happening. I tried in ... More on stackoverflow.com
🌐 stackoverflow.com
convert int to double
You'll need to convert each of the int values to double before performing the division operation, so you don't lose the fractional portion of the result." problem. ... You mostly have it, just remove the two lines before the return. The compiler message was trying to tell you that you can't have ... More on teamtreehouse.com
🌐 teamtreehouse.com
1
March 20, 2020
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
🌐
Cplusplus
cplusplus.com › forum › beginner › 198640
Data type conversion int to double withi - C++ Forum
September 29, 2016 - The multiplication has two operands, a double (1.0) and int (b). Therefore, b converts implicitly to double, the multiplication operates on doubles and the result is a double. An explicit C-style cast to double does nothing to a double value. ... Thank you so much @keskiverto! It makes a lot more sense now that you clarified the topic. One question, I understand programmers tend to use shorthands as they are faster, so why would anyone use static_cast<double> instead of (double)?
🌐
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
That’s why it’s called hidden 1. The result is your 52-bit significand (aka. fraction). Merge the sign bit, exponent, and significand by concatenating them left-to-right like so: ... The result of such a conversion is always exact. If you need to convert a 64-bit integer to double precision, the steps are similar; however, you may need to shift right instead of left in step 5, and round the result according to the current rounding mode.
Find elsewhere
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/

🌐
DaniWeb
daniweb.com › programming › software-development › threads › 116537 › converting-integer-to-double
c++ - Converting Integer to double | DaniWeb
March 31, 2008 - int a = 7; cout << ((double) a / 2); // typecasting · The above yields 3.5, or 7.0/2 (floating point division) ... The above yields 7/2 = 3 (integer division). Put the type that you want to convert TO in parentheses before the number/variable ...
🌐
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); }
🌐
Cppreference
en.cppreference.com › w › c › language › conversion.html
Implicit conversions - cppreference.com
March 31, 2025 - double d = 0.1; // d = 0.1000000000000000055511151231257827021181583404541015625 float f = d; // f = 0.100000001490116119384765625 float x = 2 * (double)FLT_MAX; // undefined · A pointer to void can be implicitly converted to and from any pointer to object type with the following semantics: If a pointer to object is converted to a pointer 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*
🌐
Quora
quora.com › How-do-you-convert-double-to-int-in-C
How to convert double to int in C - Quora
This is all found in the “Nearest integer functions” section of the standard (e.g., 7.12.9 in C17). You can do any of these to the double, and then convert the result (which now won’t have any non-integral part to worry about, because you already took care of it) to an int.
🌐
MyCleverAI
mycleverai.com › it-questions › how-do-i-cast-an-int-to-a-double-in-c
How do I cast an int to a double in C?
In C, casting an int to a double is straightforward and can be done using a type cast. This process converts an integer value into its floating-point representation, allowing for more precise numerical operations and the storage of fractional values.
🌐
YouTube
youtube.com › watch
Convert double to integers in C - YouTube
#techlearner'sguide#cprogramminglanguagebeginners #cprogrammingtutorialforbeginners #c #cfullcourse #cprogrammingvideo #learncprogramming #linux #computersci...
Published   July 26, 2023
🌐
Quora
quora.com › How-do-I-convert-double-to-int-in-C-simply-by-a-function-and-not-by-type-casting
How to convert double to int in C simply by a function and not by type casting - Quora
This is all found in the “Nearest integer functions” section of the standard (e.g., 7.12.9 in C17). You can do any of these to the double, and then convert the result (which now won’t have any non-integral part to worry about, because you already took care of it) to an int.
🌐
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
Answer (1 of 4): How do programmers convert an integer to float or double precision? 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 ...
🌐
YouTube
youtube.com › shorts › MveD_bQX77g
How To Convert Int To Double In C++ - YouTube
November 27, 2024 - This video demonstrates how to convert an int to a double in C++#cpp #learntocode #codingtutorial