0Pricing
C Academy · Lesson

Integer and Float Conversions

Precision concerns.

Integer and Float Conversions is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Worlds of Numbers

C has integers (exact, no fraction) and floating point (approximate, with fraction). Converting between them changes how a value is stored.

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

Widening int to double

Most int values fit exactly in a double, so widening is safe.

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

Narrowing double to int

Going from double to int truncates toward zero and can lose information.

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

Negative Truncation

Truncation toward zero means -2.9 becomes -2, not -3.

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

float vs double Precision

float holds about 7 significant digits; double about 15. Converting double to float loses precision.

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

Large int to float

A very large int may not be representable exactly in a float, giving a rounded result.

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

Rounding vs Truncating

Casting truncates. To round, add 0.5 first (for positive values) or use library functions.

#include <stdio.h>
int main(void) {
    double d = 4.6;
    int truncated = (int)d;
    int rounded = (int)(d + 0.5);
    printf("%d %d\n", truncated, rounded);
    return 0;
}

Average Calculation

Averaging integers needs a float division to keep the fraction.

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

Float Equality Pitfall

Because floats are approximate, comparing them with == is risky. Compare within a small tolerance.

#include <stdio.h>
#include <math.h>
int main(void) {
    double a = 0.1 + 0.2;
    if (fabs(a - 0.3) < 1e-9)
        printf("close enough\n");
    return 0;
}

Mixing in Conditions

Comparing an int with a double promotes the int, so the comparison happens in floating point.

#include <stdio.h>
int main(void) {
    int n = 3;
    double d = 3.5;
    if (n < d)
        printf("3 < 3.5\n");
    return 0;
}

Choosing the Right Type

Use integers for counts and indices; use floating point for measurements and ratios.

#include <stdio.h>
int main(void) {
    int items = 4;
    double pricePer = 2.5;
    printf("total = %.2f\n", items * pricePer);
    return 0;
}

Quick Check

Test your understanding of integer and float conversions.

Recap: Integer and Float Conversions

You learned how numbers change across types.

  • int to double usually widens safely
  • double to int truncates toward zero
  • float keeps fewer digits than double
  • Round by adding 0.5 or using library calls
  • Avoid == on floats; use a tolerance

Frequently asked questions

Is the “Integer and Float Conversions” lesson free?

Yes — the full text of “Integer and Float Conversions” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Integer and Float Conversions”?

Precision concerns. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Integer and Float Conversions” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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