aayush pal

October 26, 2023

Notes about the C language

Working through Beej's guide to C.

Notes taken while reading Beej’s guide to C.

About

Language

Variable

specifiertype
%dinteger
%sstring
%ffloat
%zusize_t
%ppointer
if (x > 10)
    y += 17;
else
    y += 37;
while (1) {
    printf("1 is always true, so this repeats forever\n");
}

Functions

Pointers

int *p;
p = NULL;

Array

Strings

Structs

void set_price(struct car *c, float new_price) {
    c.price = new_price;  // ERROR!!
}

That won’t work because the dot operator only works on structs — it doesn’t work on pointers to structs.

void set_price(struct car *c, float new_price) {
    (*c).price = new_price;  // Works, but is ugly and non-idiomatic :(
}
void set_price(struct car *c, float new_price) {
    c->price = new_price;  // This looks so much better!
}

File I/O