C Tutorial

C Type Conversion

C converts types in expressions. Casts make the conversion explicit. Integer division truncates.

int divided by int truncates

When both operands of / are integers, C performs integer division. The fraction is discarded toward zero. 7 / 2 is 3, not 3.5. That is not rounding. The remainder is what % reports.

Example

#include <stdio.h>

int main(void) {
  int a = 7;
  int b = 2;
  printf("int / int: %d\n", a / b);
  printf("remainder: %d\n", a % b);
  return 0;
}

Output is 3 and 1. If you wanted 3.5, one operand must be floating-point before the division happens.

(double)a / b

A cast converts a value to another type. (double)a is 7.0. Dividing that by bpromotes b to double as well. The result is 3.5. Casting the quotient afterward is too late: (double)(a / b) is still 3.0, because the truncation already happened.

Example

#include <stdio.h>

int main(void) {
  int a = 7;
  int b = 2;
  printf("cast first: %.1f\n", (double)a / b);
  printf("cast after: %.1f\n", (double)(a / b));
  printf("3.0 / 2: %.1f\n", 3.0 / 2);
  return 0;
}

Open this in /c/try. gcc compiles C. Swap in(double)(a / b) in your own print if you want to see the truncated path again.

Casts drop the fraction the other way too

Casting a double to int truncates toward zero. (int)3.9 is3. (int)-1.9 is -1. There is no rounding in the cast. Use a math library function if you need to round.

Example

#include <stdio.h>

int main(void) {
  double x = 3.9;
  double y = -1.9;
  printf("(int)3.9 = %d\n", (int)x);
  printf("(int)-1.9 = %d\n", (int)y);
  return 0;
}

Usual arithmetic conversions

In a mixed expression, C converts the narrower type toward the wider one. An int plus adouble becomes a double addition. A char in arithmetic is promoted toint first. You do not have to write a cast for those cases, but a cast makes the intent obvious.

ExpressionResult kind
7 / 2int 3
(double)7 / 2double 3.5
7 / 2.0double 3.5
(int)3.9int 3

Casts are not magic

A cast does not change the original variable. (double)a produces a converted copy for that expression. a is still an int. Casting a pointer type is a different, sharper tool and is easy to get wrong; this chapter stays with numbers. Next: bitwise operators, which work on the bits inside those integers.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the C editor at /c/try. Change one measurement and check whether the result still has the right unit.

Physics

Milliamps to amps

20 mA is 0.020 A. Integer 20 / 1000 is 0 in C. Dividing by 1000.0 promotes the expression. Circuit formulas want amps. Milli- on a meter is a display choice.

I(A) = I(mA) / 1000

Example

#include <stdio.h>

int main(void) {
  int milliamps = 20;
  double amps = milliamps / 1000.0;
  printf("%.3f A\n", amps);
  return 0;
}

Maths

Mean without truncation

8, 11, and 5 sum to 24. 24 / 3 is 8 as int or as a real. Change the list to 8, 11, 6 and the integer mean becomes 8 while the real mean is 8.33. That is why the listing prints both.

Example

#include <stdio.h>

int main(void) {
  int sum = 8 + 11 + 5;
  printf("truncated = %d\n", sum / 3);
  printf("real = %.2f\n", sum / 3.0);
  return 0;
}

FAQ: C Type Conversion

Common questions about this page.

What is the StudyGrid C tutorial?

The StudyGrid C tutorial is a full beginner-to-advanced track: syntax, types, input, loops, functions, pointers, structs, files, and the standard library. Each chapter has copy-and-run examples.

Should I run c type conversion examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn c type conversion in this C C lesson (C Type Conversion).

Is the C editor the same as Try Python, Try HTML, or Try C++?

No. Try C compiles with gcc at /c/try and shows stdout plus compiler messages. Try Python stays at /try. Try HTML stays at /html/try. Try C++ stays at /cpp/try. C lessons never open those editors.

Do I need to install a compiler to learn C?

No. Open a chapter, click Try it in C, and compile in the browser. You can also download a .c file and compile locally with gcc.

Where should I start the C tutorial?

Start at C Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After pointers, open C Examples, then files and bitwise. Use Next at the bottom of each chapter.

Is the C tutorial free?

Yes. The C workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.