C Type Conversion
In this page:
target_type variable_name = (target_type) expression; // explicit cast
double result = int_value * double_value; // implicit conversion
Implicit Conversion
When an expression mixes types, C promotes the narrower operand to the wider one before evaluating -- an int added to a double is converted to double first, so the result keeps the fractional part instead of being silently truncated.
Example: Implicit Conversion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare a and set it to 5
int a = 5;
// Declare b and set it to 2.0
double b = 2.0;
// Print formatted text to the screen
printf("%.1f", a + b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Explicit Casts
Writing (int), (float), or (char) in front of a value forces a conversion the compiler wouldn't do on its own, useful when you need integer division to instead produce a fractional result or want to strip a value down to its integer part on purpose.
Example: Explicit Casts
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 7, b = 2;
// Declare result and set it to (double)a / b
double result = (double)a / b;
// Print formatted text to the screen
printf("%.1f", result);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Truncation and Data Loss
Casting a float to an int doesn't round -- it discards everything after the decimal point, so (int)9.9 becomes 9, not 10; casting a large value into a smaller type can also silently wrap around instead of raising any error.
Example: Truncation and Data Loss
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare f and set it to 9.9f
float f = 9.9f;
// Declare truncated and set it to (int)f
int truncated = (int)f;
// Print formatted text to the screen
printf("%d", truncated);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Signed and Unsigned Conversion
Assigning a negative signed value to an unsigned variable doesn't fail -- it wraps around using modular arithmetic, so -1 stored in an unsigned int becomes the type's maximum representable value, a frequent source of subtle bugs in loop counters.
Example: Signed and Unsigned Conversion
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare negative and set it to -1
int negative = -1;
// Declare wrapped and set it to negative
unsigned int wrapped = negative;
// Print formatted text to the screen
printf("%u", wrapped);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Casting Compared to C++
Plain C only has this one C-style cast syntax, (type)value, applied uniformly to every kind of conversion; C++ later split this into four distinct operators -- static_cast, dynamic_cast, const_cast, and reinterpret_cast -- specifically to make each conversion's intent and safety explicit.
Example: Casting Compared to C++
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare d and set it to 9.5
double d = 9.5;
// Declare i and set it to (int)d
int i = (int)d;
// Print formatted text to the screen
printf("%d", i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Dividing two ints and assigning to a double, such as
double d = 5 / 2;, which gives 2.0 because the division already happened as integers. - Casting a large double to
int, which truncates the fraction and may overflow. - Mixing signed and unsigned values in a comparison, where a negative number turns into a huge unsigned value.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers