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
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
๐ŸŒ
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
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?
๐ŸŒ
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.
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 ...
๐ŸŒ
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*
๐ŸŒ
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); }
๐ŸŒ
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-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
๐ŸŒ
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;
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ cplusplus-program-to-convert-double-type-variables-into-int
C++ Program to convert double type Variables into int
October 19, 2022 - Integer to double datatype conversion can either be done automatically by the compiler (known as "implicit" conversion) or explicitly requested by the programmer to the compiler (known as "explicit" conversion). In the sections that follow, we go over the various conversion methods.