DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 20 of 64
←PreviousPrevNextNext→

Type Conversion in C

What is Type Conversion?

Type conversion is the process of changing a variable from one data type to another.

This is important because operations in C often involve variables of different types, and the compiler must standardize them before performing calculations.

Implicit Conversion (Type Promotion)

C performs implicit conversions to a common type in expressions. First, the *integer promotions* apply (e.g., `char`/`short` → `int` or `unsigned int`). Then the *usual arithmetic conversions* choose a common real type: `long double` > `double` > `float` (note: in expressions, `float` operands are promoted to `double`).

Example: if you add an `int` and a `double`, the `int` is converted to `double` before the operation.

Example
#include <stdio.h>
int main(void) {
    int x = 5;
    double y = 2.5;
    double result = x + y; // x converted to double
    printf("%.1f\n", result);
    return 0;
}
Output
7.5

Explicit Conversion (Casting)

You can manually convert one type to another using casting. This is done with the syntax `(type) value`.

Casting is useful when you want to control precision or discard fractions.

Example
#include <stdio.h>
int main(void) {
    double pi = 3.14;
    int x = (int)pi; // explicitly cast to int
    printf("%d\n", x);
    return 0;
}
Output
3

Type Conversion in Expressions

When multiple data types appear in an expression, C converts operands to a common type: apply integer promotions, then use the usual arithmetic conversions. For floating types, `float` is promoted to `double` in expressions; if one operand is `double`, the other is converted to `double`, etc.

Example: `int a = 3; float b = 2.5f; double c = a + b;` → `a` and `b` are both converted to `double`, and the result is `double`.

Example
#include <stdio.h>
int main(void) {
    int a = 3; float b = 2.5f; double c = a + b;
    printf("c = %.1f\n", c);
    return 0;
}
Output
c = 5.5

Common Pitfall: Integer Division

If both operands of `/` are integers, the result is integer division (fraction is discarded) before any assignment or further conversion. Cast at least one operand to get floating-point division.

Example
#include <stdio.h>
int main(void) {
    int m = 7, n = 2;
    double r1 = m / n;        // integer division → 3, then converted to 3.0
    double r2 = (double)m / n; // 7.0 / 2 → 3.5
    printf("r1=%.1f r2=%.1f\n", r1, r2);
    return 0;
}
Output
r1=3.0 r2=3.5

Signed vs Unsigned Mixing

When a signed and an unsigned integer are combined, the signed value may be converted to unsigned, which can yield surprising results if the signed value is negative.

⚠️ Warning: Avoid mixing signed and unsigned types in comparisons unless you intend the conversion.
Example
#include <stdio.h>
int main(void) {
    int a = -1;
    unsigned int b = 1u;
    printf("a < b? %d\n", a < b); // often prints 0 because a converts to a large unsigned value
    return 0;
}
Output
a < b? 0

Best Practices

• Avoid unnecessary casts — let the compiler handle safe conversions.

• Cast explicitly to control behavior (e.g., to prevent integer division or to narrow intentionally).

• Remember that casting does not change the variable’s actual type, only the value used in that expression.

• Be careful mixing signed and unsigned integers.

• Prefer matching types in APIs and expressions to minimize implicit conversions.

ℹ️ Note: Improper type conversion can lead to data loss (e.g., casting double to int removes decimals) or logic bugs (e.g., signed→unsigned in comparisons).
Test your knowledge: Type Conversion in C
Quiz Configuration
4 of 8 questions
Random
Previous allowed
Review enabled
Early close allowed
Estimated time: 8 min
C BasicsTopic 20 of 64
←PreviousPrevNextNext→