0Pricing
C Academy · Lesson

Explicit Casts

Manual conversions.

What Is a Cast

A cast explicitly converts a value to another type using (type)value. It tells the compiler exactly what you want.

#include <stdio.h>
int main(void) {
    double d = (double)5;
    printf("%.1f\n", d);
    return 0;
}

Forcing Real Division

Cast one operand to double so division keeps the fractional part.

#include <stdio.h>
int main(void) {
    int a = 7, b = 2;
    double r = (double)a / b;
    printf("%.2f\n", r);
    return 0;
}

All lessons in this course

  1. Implicit Conversions
  2. Explicit Casts
  3. Integer and Float Conversions
  4. Conversion Pitfalls
← Back to C Academy