Live data from Hacker News

Sizeof(char) is 1

drj11.wordpress.com

51–55 of 55 posts

Re: Sizeof(char) is 1

#51
post #18

If a char is to be defined as number from 0 to 255 that and its storage space is exactly 1 byte - that is correct. But it is rather a legacy nowadays. No one should use it.

I think you miss the point. A byte is always 8 bits, and therefore contains unsigned numbers 0 to 255 inclusive. A char is always defined to be the base size of the machine. By definition, therefore, sizeof(char) is always 1. However, a char is not always 1 byte. On some machines a char can hold the values 0..511, and on others 0..65535. (for example)

I stand corrected and apologise - I genuinely mis-spoke (mis-typed?) myself. I do know that a byte is not (historically) necessarily 8-bits - indeed, I regularly used to program machines on which the basic memory unit was 12 bits.

The other reply to my comment (http://news.ycombinator.com/item?id=1666345) gets this right.

Again, apologies.

Re: Sizeof(char) is 1

#52

Earlier quoted context omitted.

The only reason to do that is if you want the compiler to check for typos: HTPP is caught, "htpp" isn't. It makes no sense for single letter constants of course.

Isn't that what unit testing is for?

Do all your projects get 100% branch coverage? If not you'll probably want the compiler to help you where ever it can.

Re: Sizeof(char) is 1

#53

Personally I don't agree with the post, starting from his analysis, of the first snippet of code: group->text = (char *) malloc(sizeof(char) * strlen(params->text)); if (group->text == NULL) { res = MP_MEM; } strcpy(group->text, params->text); I don't think that the sizeof(char) or the casting of the pointer are of any danger and increase readability (we are not coding to show how well we know the standard). The post…

gets ... at least you theoretically can use strcpy securely.

Re: Sizeof(char) is 1

#54
post #34

Earlier quoted context omitted.

Which is actually pretty useful and readable, imho (if formatted correctly): return a ? b : c ? d : e ? f : g; Side note: this does not work in PHP, as the operator is left-associative instead of right-associative there. The above in PHP would be evaluated as: return ((a ? b : c) ? d : e) ? f : g; Which is almost never what you want. I can't even format this properly to convey intent.

The only sane thing is to use if statements, imho. No ninja cowboy engineer nonsense.

This is why more languages should adopt the every-form-returns-a-value paradigm.

Re: Sizeof(char) is 1

#55

Earlier quoted context omitted.

The only sane thing is to use if statements, imho. No ninja cowboy engineer nonsense.

Sometimes, if-statements unnecessarily add lines, and the lines become harder to read. I would say it's a case-by-case statement. Well, okay, more specifically, I would say that the use of a single ternary if is a case-by-case statement.

Absolutely. I use ternary if when appropriate. But nesting ternary ifs is horrible, and nesting ternary ifs while discarding parentheses is malevolent.
Post reply on HN