C Type Casting
In this page:
target_type variable_name = (target_type) expression; // explicit cast
double result = int_value * double_value; // implicit conversion
What is Type Casting?
Type casting converts a value from one data type to another so an expression evaluates with the precision or range you actually intend, rather than whatever the compiler would pick by default.
It matters most whenever mixed types meet in the same expression, such as combining an int with a double.
Example: What is Type Casting?
// 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 (double)a
double b = (double)a;
// Print formatted text to the screen
printf("%.1f", b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Implicit Type Conversion
Implicit conversion happens automatically whenever the compiler needs to reconcile mismatched operand types in an expression, silently promoting the narrower type (like char or int) to the wider one (like double) so no precision is lost during the calculation.
This is convenient, but it can hide bugs if you don't expect the promotion.
Example: Implicit Type 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.5
double b = 2.5;
// 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 Type Casting
Explicit casting means writing the target type in parentheses directly before the value, such as (double)x, to force a conversion the compiler wouldn't otherwise perform on its own.
Reach for it whenever you need to override C's default promotion rules deliberately, not just to silence a compiler warning.
Example: Explicit Type Casting
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 9.7
double x = 9.7;
// Print formatted text to the screen
printf("%d", (int)x);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Preventing Integer Division Loss
Because integer division in C truncates toward zero and drops any remainder, dividing two int variables like 7/2 always yields 3, not 3.5.
Casting at least one operand to float or double before the division forces C to use floating-point arithmetic and keep the fractional part.
Example: Preventing Integer Division Loss
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
int a = 7, b = 2;
// Print formatted text to the screen
printf("%.1f", (float)a / b);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Float to Integer Truncation
Converting a float or double to an int truncates the value toward zero rather than rounding it, so 9.99 becomes 9 and -9.99 becomes -9, not -10.
If you actually want rounding, call round() from math.h before casting, since the cast alone will always chop off the decimal part.
Example: Float to Integer Truncation
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%d %d", (int)9.99, (int)-9.99);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Casting after the division, as in
(float)(a / b), when the cast should be applied before it. - Casting a large value to a smaller type and losing data.
- Assuming a cast rounds the value, when it truncates.
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions