C Tutorial

C Operators

Arithmetic, comparison, and assignment operators work on values. Precedence decides what runs first.

Arithmetic

+, -, *, and / add, subtract, multiply, and divide.% is remainder after integer division. Both sides of % should be integers in this tutorial.

Example

#include <stdio.h>

int main(void) {
  printf("%d\n", 10 + 3);
  printf("%d\n", 10 - 3);
  printf("%d\n", 10 * 3);
  printf("%d\n", 10 / 3);
  printf("%d\n", 10 % 3);
  return 0;
}

10 / 3 is 3 because both operands are int. The fractional part is dropped, not rounded. 10 % 3 is 1: three goes into ten three times, remainder one.

Run the listings in /c/try. gcc compiles them there. Do not use the Python editor at/try, the HTML preview at /html/try, or C++ at /cpp/try.

Increment and decrement

++ adds one. -- subtracts one. Written as a statement, n++ and++n both change n by one. This tutorial uses them on their own line so prefix and postfix do not matter yet.

After int n = 5;, the statement n++; leaves n equal to 6.n--; would take it back to 5.

Comparison

Comparisons produce 0 or 1. C treats 0 as false and any non-zero value as true. Use == to test equality. A single = assigns; it does not compare.

OperatorMeaning
==equal
!=not equal
<less than
>greater than
<=less than or equal
>=greater than or equal

Example

#include <stdio.h>

int main(void) {
  int a = 7;
  int b = 4;
  printf("%d\n", a == b);
  printf("%d\n", a != b);
  printf("%d\n", a > b);
  printf("%d\n", a < b);
  return 0;
}

Logic

&& is and: both sides must be true. || is or: at least one side is true.! is not: it flips true and false. Use parentheses when you mix them so the intended grouping is obvious.

Example

#include <stdio.h>

int main(void) {
  int n = 8;
  printf("%d\n", n > 0 && n < 10);
  printf("%d\n", n < 0 || n > 100);
  printf("%d\n", !(n == 8));
  return 0;
}

Compound assignment

+= adds and stores. -=, *=, /=, and %= work the same way. n += 2 means n = n + 2.

Example

#include <stdio.h>

int main(void) {
  int n = 10;
  n++;
  n += 5;
  n *= 2;
  printf("%d\n", n);
  return 0;
}

Precedence

Multiplication and division run before addition and subtraction. 2 + 3 * 4 is 14, not20. Parentheses override the default: (2 + 3) * 4 is 20. When a line looks busy, add parentheses even if the default would match what you meant.

Next: C strings — char arrays that end with a null byte, not a C++ string.

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

Ohm’s law

For many metals at constant temperature, current is proportional to potential difference. V = I R. 20 mA through 220 Ω is 4.4 V.

Keep I in amps, not milliamps, unless you convert. 20 times 220 is 4400, which looks like a voltage until you remember the 20 was milliamps.

V = I R

Ohm’s law is a model for ohmic components. A filament lamp’s resistance rises as it heats.

Example

#include <stdio.h>

int main(void) {
  double I = 0.020;
  double R = 220.0;
  double V = I * R;
  printf("V = %.2f V\n", V);
  return 0;
}

Maths

Hours and leftover minutes

Integer division throws away the remainder. 125 / 60 is 2 hours. The leftover 5 minutes is 125 % 60. Together they reconstruct 125.

This is how you convert a total in a small unit into a mixed measurement. The same pair of operators splits a number of days into weeks and leftover days.

h = total / 60, min = total % 60

Example

#include <stdio.h>

int main(void) {
  int total = 125;
  printf("%d h %d min\n", total / 60, total % 60);
  return 0;
}

Biology

Body mass index

BMI is mass in kilograms divided by height in metres squared. 68 kg at 1.72 m is about 23, which tables call a typical adult range. The index does not know muscle from fat.

Comparisons in C produce 0 or 1. Printing bmi > 25.0 is a crude flag, not a diagnosis.

BMI = m / h²

BMI was built for populations, not for one athlete. Treat the cutoff as a teaching threshold.

Example

#include <stdio.h>

int main(void) {
  double mass = 68.0;
  double height = 1.72;
  double bmi = mass / (height * height);
  printf("BMI = %.1f\n", bmi);
  printf("over 25? %d\n", bmi > 25.0);
  return 0;
}

FAQ: C Operators

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 operators 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 operators in this C C lesson (C Operators).

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.