C Tutorial

C Data Types

int, double, char, and _Bool store different kinds of values. Pick the type that matches the data.

The type is a contract

Every variable has a type. The type says what kind of value fits and how printf and operators treat it. Choose int for whole counts, double for measurements, char for one glyph, and _Bool (or bool from #include <stdbool.h>) for yes or no.

Example

#include <stdio.h>
#include <stdbool.h>

int main(void) {
  int seats = 24;
  double length_m = 1.75;
  char row = 'C';
  bool open = true;
  printf("%d\n", seats);
  printf("%f\n", length_m);
  printf("%c\n", row);
  printf("%d\n", open);
  return 0;
}

bool prints with %d as 1 or 0. That is normal forprintf.

Compile this at /c/try with gcc. That editor is only for C. Python lives at /try. HTML lives at /html/try. C++ lives at /cpp/try.

A table of common types

TypeHoldsExampleHeader
intWhole numbers42, -3built in
doubleFloating-point numbers3.14, -0.5built in
charOne character'A', '7'built in
_Bool / bool0 or 1, false or true0, truestdbool.h for bool
char[]Text ending in a null byte"hello"built in; string.h to measure

C has no string type like C++. Text is a char array with a '\0' at the end. The strings chapter covers that in full.

sizeof reports bytes

sizeof tells you how many bytes a type (or a variable) uses. The number can differ between compilers and machines. On the StudyGrid gcc setup you will typically see int as 4 bytes anddouble as 8.

Example

#include <stdio.h>

int main(void) {
  printf("char %zu\n", sizeof(char));
  printf("int %zu\n", sizeof(int));
  printf("double %zu\n", sizeof(double));
  printf("_Bool %zu\n", sizeof(_Bool));
  return 0;
}

You almost never pick a type because of sizeof at this stage. You pick it because of the kind of value. sizeof is here so you know the tool exists. %zu is the specifier forsizeof’s result.

int versus double

Integer division truncates toward zero: 7 / 2 is 3. If either side is adouble, you get a fractional result: 7.0 / 2 is 3.5. Useint for counts. Use double for measurements in this tutorial.

Example

#include <stdio.h>

int main(void) {
  printf("%d\n", 7 / 2);
  printf("%f\n", 7.0 / 2);
  return 0;
}

char is not a string

A char holds one character in single quotes: 'A'. A string literal in double quotes — "ABC" — is an array of characters plus a null terminator. Mixing them up is a type error.

C also has float, long, and unsigned. This track stays withint, double, char, and booleans until you have a reason to switch. Prefer double over float unless you are told otherwise.

Next: constants, so some of these values can be named and locked.

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.

Chemistry

Atomic number versus pH

Atomic number is a count of protons. Oxygen has 8. That value is an integer; you never have 8.3 protons in a nucleus.

pH is −log10 of hydrogen-ion activity. Rainwater around 5.6 is weakly acidic because of dissolved CO2, not because the rain is “polluted” by default. It needs a double.

pH = −log₁₀[H⁺]

Example

#include <stdio.h>

int main(void) {
  int protons = 8;      /* oxygen */
  double ph = 5.6;      /* rainwater, roughly */
  printf("O has %d protons\n", protons);
  printf("pH = %.1f\n", ph);
  return 0;
}

Physics

A kelvin reading and its unit

Absolute temperature is kelvin. 293.15 K is 20 °C. The number and the scale are different kinds of data: one is a measurement, the other is a label.

char holds a single character. Using K instead of a string is enough when the scale cannot be “kelvin” spelled out.

T(K) = t(°C) + 273.15

Example

#include <stdio.h>

int main(void) {
  double temp = 293.15;
  char scale = 'K';
  printf("%.2f %c\n", temp, scale);
  return 0;
}

FAQ: C Data Types

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 data types 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 data types in this C C lesson (C Data Types).

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.