← Zurück zum Kurs: C programming from scratch

Variables, types and week 1 review

Video folgt in Kürze

A variable is a name for a piece of memory that holds a value. In C you must declare the type of each variable before using it, and that type decides how much memory it takes and which values it can store. This contrasts with languages where the type is decided automatically: in C the compiler needs to know everything in advance, which lets the program be very fast.

The basic types

  • char: one byte, usually holds a character or a small number
  • short, int, long, long long: integers of growing size
  • unsigned: variant without sign (only values from 0 up)
  • float and double: real numbers; double has more precision and is the usual choice
  • _Bool (or bool with <stdbool.h>): true or false
  • size_t: unsigned size, used for lengths and sizes

The standard does not fix the exact size of each type, only minimums: an int has at least 16 bits, although on current computers it is almost always 32. To find out, use the sizeof operator and the limits in <limits.h>. If you need a guaranteed size, include <stdint.h> and use int32_t, uint8_t or int64_t.

c
#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>

int main(void) {
    printf("char: %zu byte(s)\n", sizeof(char));
    printf("int: %zu bytes, max %d\n", sizeof(int), INT_MAX);
    printf("long long: %zu bytes\n", sizeof(long long));
    printf("double: %zu bytes\n", sizeof(double));
    printf("int32_t: %zu bytes\n", sizeof(int32_t));

    bool ready = true;
    printf("ready = %d\n", ready);
    return 0;
}

Declaring, initializing and naming

Declaring means giving the type and the name; initializing means giving it a first value. An uninitialized local variable holds garbage: whatever value was in that memory before. Reading it is undefined behavior, one of the most treacherous errors in C because sometimes the program seems to work. Always initialize when you declare. Names use letters, digits and underscore, cannot start with a digit, are case sensitive and cannot be reserved words such as int or return.

c
#include <stdio.h>

int main(void) {
    int apples = 5;          /* declared and initialized */
    int oranges = 3, pears = 0;
    int total;               /* declared only: do NOT read yet */

    total = apples + oranges + pears;
    printf("Total fruit: %d\n", total);

    double price = 1.20;
    printf("Cost: %.2f\n", total * price);
    return 0;
}

Overflow: the limit of the types

Each type has a finite range. If a signed int goes beyond INT_MAX, the result is undefined behavior: you cannot trust that it wraps around. For unsigned types it is defined: arithmetic is modulo 2^n, so 0 minus 1 gives the maximum value. Many famous security failures come from overflows that nobody checked, so it is wise to choose a type large enough for your data.

c
#include <limits.h>
#include <stdio.h>

int main(void) {
    unsigned int u = 0;
    u = u - 1;                      /* defined: wraps to UINT_MAX */
    printf("u = %u\n", u);
    printf("UINT_MAX = %u\n", UINT_MAX);

    long long big = 3000000000LL;   /* does not fit in a 32-bit int */
    printf("big = %lld\n", big);
    return 0;
}

Summary of the week: you can install a compiler, compile with warnings enabled, print with printf and declare variables of the basic types, knowing that the size depends on the platform. These foundations are used in every following week. Before the quiz, review the format specifiers and recompile your programs with -Wall -Wextra: if there are no warnings, you are on the right track.

Teste dein Wissen

Frage 1 von 6

Which specifier prints a double with two decimals?

✎ Meine Notizen

Nur in diesem Browser gespeichert. Melde dich an, um es im Konto zu behalten.0/5000 Zeichen

← ZurückWeiter →