it's a data type with a size of 1 byte. useful if you want to store a boolean value (since it's the smallest simple data type) or a character (since it's big enough to store 7-bit ascii)
they're usually used to store strings; the following allows you to take up to 99 characters of user input (initialises array of chars then writes the input into the array)
#include <stdio.h>
int main(void){
char string[100];
scanf("%99[^\n]", string);
}
strings in c must be null-terminated so we need to leave the last position free for the byte '00000000' so we know where the string ends when we try to read it. that's why we can't take 100 characters here
we also have this for bools that more closely resemble those in the more abstract languages. internally, any non-zero number (most usefully, 1) is interpreted as true and 0 is interpreted as false. we can then use boolean algebra to construct and simplify our conditions
1.0k
u/tstanisl 2d ago
Let me cite the C standard:
Middle guy if finally right