~/ learn/ comp-325/ cards/ What happens when you call read()
1 of 9

The only acceptable shape for a system call

The only acceptable shape for a system call

Answer

int fd = open("data.txt", O_RDONLY); if (fd == -1) { perror("open"); return 1; }

Test for -1, name the call in the perror label so the message is findable, then decide. Skipping this is the single most common way a C program on UNIX fails silently.

S&K 3e ch18 §18.3–18.4, §18.7.4; system-call return convention and errno per POSIX.1-2024 §2.3

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/5a7833c8-fff8-4ef9-b42d-acbcad733ef6/flashcard utf-8 LF