Explicit Casts
Manual conversions.
Explicit Casts is a free C Academy lesson on CoddyKit. This is lesson 2 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, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 4 lessons in total.
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;
}Truncating on Purpose
Casting a double to int discards decimals. Sometimes that is exactly what you want.
#include <stdio.h>
int main(void) {
double price = 19.95;
int dollars = (int)price;
printf("dollars = %d\n", dollars);
return 0;
}Cast vs Implicit
An implicit conversion happens automatically; a cast documents intent and silences warnings.
#include <stdio.h>
int main(void) {
long big = 100000L;
int n = (int)big;
printf("n = %d\n", n);
return 0;
}Casting in Expressions
A cast applies only to the value next to it, so order and parentheses matter.
#include <stdio.h>
int main(void) {
int total = 5, count = 2;
double avg = (double)total / count;
printf("avg = %.2f\n", avg);
return 0;
}Char and Int Casts
You can cast between char and int to move between a character and its code.
#include <stdio.h>
int main(void) {
int code = 66;
char c = (char)code;
printf("c = %c\n", c);
return 0;
}Rounding With a Cast
Add 0.5 before casting to round a positive number to the nearest integer.
#include <stdio.h>
int main(void) {
double d = 2.7;
int rounded = (int)(d + 0.5);
printf("rounded = %d\n", rounded);
return 0;
}Casting to unsigned
You can cast to unsigned, but a negative value wraps around to a large positive number.
#include <stdio.h>
int main(void) {
int x = 10;
unsigned int u = (unsigned int)x;
printf("u = %u\n", u);
return 0;
}Avoid Unneeded Casts
Casting when the conversion is already safe and implicit just adds noise. Use casts where they clarify or are required.
#include <stdio.h>
int main(void) {
int a = 4;
double d = a;
printf("%.1f\n", d);
return 0;
}Casting in a Formula
A cast inside a percentage calculation keeps the result precise.
#include <stdio.h>
int main(void) {
int done = 3, total = 8;
double pct = (double)done / total * 100.0;
printf("%.1f%%\n", pct);
return 0;
}Casting With Math Functions
Casts help when feeding integer values into floating-point math.
#include <stdio.h>
int main(void) {
int n = 9;
double root = (double)n / 3.0;
printf("%.2f\n", root);
return 0;
}Quick Check
Test your understanding of explicit casts.
Recap: Explicit Casts
You learned to convert types on purpose.
- Syntax is
(type)value - Cast to force real division
- Cast to truncate intentionally
- Casts document intent and silence warnings
- Cast affects only the adjacent value, so use parentheses
Learn C with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 39
- Lessons
- 144
Frequently Asked Questions
Is the “Explicit Casts” lesson free?
Yes — the full text of “Explicit Casts” is free to read here on the web. 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. The C Academy course includes 4 lessons in total.
What will I learn in “Explicit Casts”?
Manual conversions. 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, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Explicit Casts” 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.