News for hackers. Stuff that matters.
Re:C Programming Guidelines by caf on Tuesday September 01, 2015 @10:50PM (#10157171)
Use signed char for strings...

What? No. Use plain "char" for strings (which may be signed or unsigned, depending on the environment).

If you use "signed char" in an environment where plain "char" is unsigned then a comparison like:

  string[i] == '\xff'
will be always false.
Re:C Programming Guidelines by metromint on Tuesday September 01, 2015 @10:56PM (#10157183)
It won't make a difference for strings. But in C you can use a char to do math, when it will make a difference.

In fact, when working in constrained memory environments, like embedded 8 bit applications a char will often be used to do math, and then it makes a big difference. This is because there is no byte type by default in C.

Re:C Programming Guidelines by nitrogen on Wednesday September 02, 2015 @01:15AM (#10157550)
This is because there is no byte type by default in C.

There is in C99 and newer: int8_t or uint8_t.

Re:C Programming Guidelines by analognoise on Wednesday September 02, 2015 @01:40AM (#10157606)
This! C99 is old enough to drive; I don't know why there's so much cruft that's been fixed in C that gets perpetuated.
Re:C Programming Guidelines by 2bluesc on Thursday September 03, 2015 @09:52PM (#10168456)

  C99 is old enough to drive
I'm using that from now on.
Re:C Programming Guidelines by Tomte on Wednesday September 02, 2015 @02:46AM (#10157759)
No, char (not signed char, unsigned char, uint8_t or int8_t is the "byte type". By definition.
Re:C Programming Guidelines by nitrogen on Wednesday September 02, 2015 @03:12AM (#10157823)
CHAR_BIT must be at least 8 bits, but is not a "byte" as used by the original comment. The C standard defines "byte" differently from current common usage. To get exactly 8 bits of the desired signedness, which may not be possible on systems with CHAR_BIT greater than 8, one must use [u]int8_t.

----

From a draft version of the C standard:

The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two’s complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.

The typedef name uintN_t designates an unsigned integer type with width N and no padding bits. Thus, uint24_t denotes such an unsigned integer type with a width of exactly 24 bits.

These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.

Re:C Programming Guidelines by Tomte on Wednesday September 02, 2015 @03:39AM (#10157899)
Yes, exactly. Why are you telling me this?

If we're talking about C, we should use the technical definition of "byte" as used by C.

Everywhere else, sure, let's view byte as a 8 bit entity. That's so predominant that everything else would be nit-picking.

But not in the context of the C language.

Re:C Programming Guidelines by nitrogen on Wednesday September 02, 2015 @12:20PM (#10160286)
I was commenting more for the benefit of the original commenter who said there is no "byte" type.
Re:C Programming Guidelines by caf on Wednesday September 02, 2015 @03:30AM (#10157875)
int8_t and uint8_t are definitely not possible on systems with CHAR_BIT greater than 8.

(From 6.2.6.1: Values stored in non-bit-field objects of any other object type consist of n × CHAR_BIT bits, where n is the size of an object of that type, in bytes.)

Re:C Programming Guidelines by heinrich5991 on Wednesday September 02, 2015 @03:19AM (#10157838)
I believe unsigned char is the byte type, as you're allowed to convert each type into a unsigned char array and read it out (by some standard, was it C99 or C11, I don't know).
Re:C Programming Guidelines by btrask on Wednesday September 02, 2015 @06:25PM (#10162313)
You're right, and that statement was poorly worded. If you check my project, I use plain char, which is signed on most platforms. The benefit of using unsigned char for binary data is the same, but the compiler warnings won't help on certain platforms.