C Type Conversion

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Type Conversion?

Type conversion changes a value from one type to another. C converts implicitly in mixed expressions (smaller types promote to int, int converts to double) and lets you force a conversion with a cast like (double)x.

Remember
double avg = (double)sum / count;   /* explicit cast */
int    n   = 3.99;               /* implicit: becomes 3 */
double r   = 7 / 2;              /* int division first -> 3.0 */

Cheat Sheet

Keep these facts handy while you work with Type Conversion in C.

TopicDetail
Promotionchar and short become int in expressions
Usual arithmetic conversionsOperands convert to the wider type
Cast(type)expression
TruncationFloat to int drops the fraction

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Input and Output.

Example 1 — Integer division versus a cast

Convert before dividing to keep the fractional part.

C
#include <stdio.h>

int main(void) {
    int sum = 7;
    int count = 2;

    double wrong = sum / count;
    double right = (double)sum / count;

    printf("wrong: %.1f\n", wrong);
    printf("right: %.1f\n", right);
    return 0;
}

How It Works

  1. sum / count divides two ints and yields 3, then converts to 3.0.
  2. (double)sum converts first, so the division is floating point.
  3. Casts bind tighter than /.

Edge Cases & Pitfalls

Check these before relying on Type Conversion in real programs.

sign

Signed/unsigned comparison

Comparing -1 < 1u is false because -1 converts to a huge unsigned value. Keep signedness consistent.

trunc

Silent narrowing

int n = 3000000000L; may overflow. Enable -Wconversion to find these.

Key Takeaways

  • Promote: smaller types widen automatically.
  • Cast: convert before the operation, not after.
  • Truncate: float to int discards the fraction.

One line: Type conversion changes a value from one type to another.

Frequently Asked Questions

No. A cast produces a converted value; the variable keeps its type and value.
Both operands are int, so C performs integer division and discards the remainder. Cast one operand to double for 3.5.
Continue with "C Input and Output" next. Print with printf and read input with scanf, getchar, and fgets. The C Fundamentals hub lists every topic in order.

Next: Input and Output

Print with printf and read input with scanf, getchar, and fgets.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful