News for hackers. Stuff that matters.
Re:What is your favourite C programming trick? by snorkel on Friday October 14, 2011 @10:24AM (#3111263)
So many to choose from ...

Convert single char c into an integer:

    int value = '0' - c;
Get the length of a static string you can use sizeof() instead of strlen()

    int len1 = sizeof("hello");   // compile-time string length
A fast and simple ring buffer (borrowed from Quake code)

    UPDATE_MASK = ARRAY_SIZE - 1;
    array[i++ & UPDATE_MASK] = data;
Init all bits of a mask to 1 on any architecture:

    unsigned int flags = -1;
Re:What is your favourite C programming trick? by mansr on Friday October 14, 2011 @10:39AM (#3111321)
I think you meant

    int value = c - '0';
Also, remember that sizeof("string") includes the null terminator while strlen() does not.
Re:What is your favourite C programming trick? by matthavener on Friday October 14, 2011 @10:29AM (#3111276)
The ring buffer is a nice trick, but it only works on array sizes that are a multiple of 2. The generalized version would be "array[i++ % ARRAY_SIZE] = data". If ARRAY_SIZE happens to be a multiple of two, I'm sure most compilers would optimize to a bitmask anyways..
Re:What is your favourite C programming trick? by mansr on Friday October 14, 2011 @10:42AM (#3111338)
And if it is not a power of 2, the % operation will be anything but fast. If a size not a power of 2 is strictly required, a simple increment and compare is almost certainly faster.
Re:What is your favourite C programming trick? by JoachimSchipper on Saturday October 15, 2011 @08:34AM (#3114690)
Signed integer overflow is undefined, so make sure you're using an unsigned type in your ringbuffer example. (And an array with some-power-of-2 elements; you should probably use sizeof(array)/sizeof(<star> array) instead of ARRAY_SIZE too.)
Re:What is your favourite C programming trick? by tptacek on Friday October 14, 2011 @10:45AM (#3111354)
If you want all-ones, shouldn't you just use ~0?
Re:What is your favourite C programming trick? by mansr on Friday October 14, 2011 @11:14AM (#3111471)
No. ~0 has type signed int, which on a one's complement machine is a negative zero. Converting this to an unsigned type yields zero. On a sign-magnitude machine, ~0 is -0x7fffffff (assuming 32-bit for simplicity). Conversion to unsigned 32-bit is done by adding 0x100000000, yielding 0x80000001, again not what was desired. If the variable being assigned to has a width different from that of int, things get even more interesting.
Re:What is your favourite C programming trick? by tptacek on Friday October 14, 2011 @11:20AM (#3111497)
Ah. Thanks! Is there a ones-complement or sign-magnitude architecture still in production anywhere? (I'm curious; I have no idea.)
Re:What is your favourite C programming trick? by joel4039 on Friday October 14, 2011 @12:34PM (#3111817)
As far as I know analog devices still makes some ones-complement Analog to Digital Converters. But I don't know of any modern processor that uses them.
Re:What is your favourite C programming trick? by wglb on Friday October 14, 2011 @07:35PM (#3113402)
How about ~0U?
Re:What is your favourite C programming trick? by mansr on Friday October 14, 2011 @11:01PM (#3113833)
That will only set as many bits as are in an int, which is wrong if the type of the variable you are setting is wider than int. For a long, you could of course use ~0ul, and so on for other types, but then you'd have to find and update every such assignment if the variable were ever changed to a wider type.
Re:What is your favourite C programming trick? by xtacy on Friday October 14, 2011 @10:37AM (#3111311)
I think you meant:

    int value = c - '0';
Otherwise, value will be negative.