← Back to C Course | Chapter 12: Advanced Topics | Lesson 2 of 20

C Type Casting

Type casting turns a value of one type into another, like pouring juice from a cup into a bottle. Going to a smaller type can lose information.
Syntax
c
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?

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Casting after the division, as in (float)(a / b), when the cast should be applied before it.
  2. Casting a large value to a smaller type and losing data.
  3. Assuming a cast rounds the value, when it truncates.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.