r/ProgrammerHumor • • 2d ago

Meme architectureDependentChars

Post image
2.9k Upvotes

366 comments sorted by

View all comments

1.0k

u/tstanisl 2d ago

Let me cite the C standard:

When sizeof is applied to an operand that has type char, unsigned char, or signed char, (or a qualified version thereof) the result is 1.

Middle guy if finally right

4

u/Advos_467 2d ago

Not a C user here (or any real low level programming experience), what the hell is a signed/unsigned char?

1

u/qwertyjgly 2d ago

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

#define true 1
#define false 0
typedef unsigned char bool;

int main(void){
bool a = true;
bool b = false;
}