Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

C

// Guess the number game
#include <stdio.h>
#include <stdlib.h>

int generate_secret_number(int min, int max) {
    return (rand() % (max - min + 1)) + min;
}

int main() {
    printf("Guess the number!\n");

    int secret_number = generate_secret_number(1, 100);

    while (1) {
        printf("Please input your guess: ");

        int guess;
        scanf("%d", &guess);

        printf("You guessed: %d\n", guess);

        if (guess < secret_number) {
            printf("Too small!\n");
        } else if (guess > secret_number) {
            printf("Too big!\n");
        } else {
            printf("You win!\n");
            break;
        }
    }

    return 0;
}

Types

  • Primitives: int, char, float, double
  • Constants: const
  • Modifiers: signed, unsigned, short, long
  • Casting: (int) 3.14
  • Array: int arr[5];

Operators

  • Arithmetic: +, -, *, /, %
  • Relational: ==, !=, <, <=, >, >=
  • Logical: &&, ||, !
  • Bitwise: &, |, ^, <<, >>
  • Assignment: =, +=, -=, *=, /=, %=
  • Increment/Decrement: ++, --
  • Ternary: condition ? expr1 : expr2

Flow Control

// if-else
if (condition) { /* ... */ }
else if (condition) { /* ... */ }
else { /* ... */ }

// switch-case
switch (enumerable) {
    case variant1: /* ... */ break;
    case variant2: /* ... */ break;
    default: /* ... */
}

// while loop
while (condition) { /* ... */ }

// do-while loop
do { /* ... */ } while (condition);

// for loop
for (int i = 0; i < 10; i++) { /* ... */ }

// break, continue
break;    // exits the loop
continue; // skips the current iteration.

// label & goto
label: /* statement */
goto label; // jumps to the label.

Compound Types

// struct (used to group related data)
struct Point {
    int x;
    int y;
};
struct Point p1 = {0, 0};
p1.x = 3;
p1.y = 4;

// union (only one member can be accessed at a time)
union Data {
    int i;
    float f;
};
union Data data;
// note that accessing `i` after assigned `f` is undefined behavior.
data.i = 10;    // only `i` is accessible now
data.f = 220.5; // only `f` is accessible now

// enum (used to define a set of named integer constants)
enum Color { RED, GREEN, BLUE };
enum Color c = BLUE;
if (c == RED) { /* ... */ }