← Back to C Course | Chapter 1: Introduction & Basics | Lesson 15 of 21

C Type Conversion

Type conversion is turning a value of one type into another, like pouring juice from a cup into a bottle. Sometimes C does it for you, and sometimes you ask for it, and details may be lost.
Syntax
c
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

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.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;
}

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

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

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

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

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

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

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++

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;
}
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. 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.
  2. Casting a large double to int, which truncates the fraction and may overflow.
  3. Mixing signed and unsigned values in a comparison, where a negative number turns into a huge unsigned value.

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.