Coding tips they don't teach you in school
Here are some C coding tips, because I have been unable to post anything for a while.
The nested ? : trick
The switch statement is very efficient, and the compiler will often implement it as a table lookup so it doesn't have to do any comparisons. But it sure can tire your fingers in a hurry:switch( number ) { case 1: str = "one"; break; case 2: str = "two"; break; case 3: str = "three"; break; case 4: str = "four"; break; case 5: str = "five"; break; default: str = "unknown number"; break; }
If getting the code out is more important than its speed, you can use a nested conditional operator to save typing:
str = number == 1 ? "one" : number == 2 ? "two" : number == 3 ? "three" : number == 4 || rand() == 42 ? "four" : number == 5 ? "five" : "unknown number";
You can freak out your coworkers if you do it all on one line.
DeMorgan's theorem of negativity
// if blah blah blah blah blah blah.... if ( !(A && B) )
is the same as
// if blah blah if ( !A || !B )
Pick the one that is easier to understand and read out loud. It is usually the second.
Rounding numbers using integer math
Suppose you have 2001 boolean variables, so you want to keep them in a bitmap of bytes, 8 at a time. How do you declare this array?
#define NUMBER_OF_BITS 2001 unsigned char bitmap[ NUMBER_OF_BITS / 8 ]; // FAIL!
The computer uses integer math, so 2001 / 8 is 250 and there is one bit left over. When you store bit 2001, you will corrupt memory. You can round numbers up like this:
unsigned char bitmap[ (NUMBER_OF_BITS + 7) / 8 ];
This works because integer division always rounds down. However, if you add the divisor less one, then you will force it to always round up. It will also work for 32 bit integers:
unsigned int bitmap[ (NUMBER_OF_BITS + 31) / 32 ];
Real rounding via integer division
Adding something before dividing is a general technique. This code rounds a and b to the nearest 10.int a = 12; int b = 16; printf("Round a: %d", (a + 5) / 10 * 10 ); printf("Round b: %d", (b + 5) / 10 * 10 );
Multiply by arbitrary numbers using shift
Sometimes, for good reasons, you need to hand optimize your code. But most of the time, somebody is just showing off, and you will see code like this:
b = ( a << 1 );
This is the same as multiplying by 2. Because of the way binary numbers are represented, you can multiply by any power of two by shifting by that power. You can also multiply by other numbers.
// b = 10*a, which is 8*a + 2*a b = ( a << 3 ) + ( a << 1 );
Division isn't as nice looking.
Getting array sizes
In C, you can get the total size of an array in bytes using the sizeof operator. Dividing it by the size of each element will give you the number of elements in the array.MyHugeStructure array[100]; int i; for( i = 0; i < sizeof(array)/sizeof(*array); i++ ) { array[i].id = i; }The downside is that if you ever change the array to be dynamically allocated using new or malloc(), then sizeof() will only return the pointer size. Of course, in a large project you should have have a defined constant for the number of entries.
Else Flattening
Deeply nested else clauses are hard to understand. But if they look like this, you are in luck:
if ( A ) { } else { if ( B ) { } else { if ( C ) { } } }
Save money on wide screen monitors by flattening the elses.
if ( A ) { } else if ( B ) { } else if ( C ) { }
Black is White and Up is Down if you're not using a mainstream compiler
If your code has to run on a lot of different platforms, you should know that GCC and Microsoft VC++ are very similar and this similarity will trick you into thinking that all compilers are alike. But the C standard leaves a lot of stuff out that will surprise you, if you do work on embedded systems with crappy, but standards-conforming compilers.
What is sizeof(char)? In most compilers it is 1 byte. Switch compilers, and it might be 2 or 4 bytes. The C standards only says that it cannot be larger than an int.
What happens if you add something to the maximum integer?
int a = MAX_INT; a++; printf("%dn", a);
In mainstream compilers you get a negative number, since the number space wraps around. But the C standard says the result is undefined. So some compilers will leave a
at the maximum value, MAX_INT.
And don't even think about bit-shifting a negative number:
int a = -2; printf("%dn", a >> 1 );
I was on the C language standards committee (X3J11) ... in fact, I was the first person in the world to vote in favor of adoption of the standard (alphabetic privilege) ... and I can tell you authoritatively that this is wrong. Yes, the standard says that a char cannot be bigger than an int, and does not specify how many bits are in a char--nevertheless, sizeof(char) == 1 and cannot be otherwise (the standard does not state this explicitly, although I think it may have made it into a footnote, but it can be logically derived). You could, for instance, have 32-bit chars and 32-bit ints, in which case sizeof(char) == sizeof(int) == 1. I don't know why you think otherwise.
"In mainstream compilers you get a negative number, since the number space wraps around. But the C standard says the result is undefined."
I hadn't realized that there's anyone alive who wasn't aware of this.
"So some compilers will leave a at the maximum value, MAX_INT."
This isn't a function of the compiler, but of the host hardware. (But it's pretty weird hardware that clamps at the max integer value.) In fact this is *why* the C standard leaves such things undefined--so that compilers don't have to generate extra code to force the computer to produce results that are "unnatural" for the machine's architecture. (Java takes a different approach, where all results are strictly defined, so such extra code does need to be generated for unusual hardware. And Java requires that bytes are 8 bits and cannot be made to work on hardware with a different byte size).
"And don't even think about bit-shifting a negative number: "
Right ... which is why one should use the / operator, and then examine the generated code to see if the compiler is smart enough to optimize division by powers of 2.
Another problem is shifting by >= the number of bits in the operand ... e.g., i >> 32 is undefined if i is a 32-bit int. This is to accommodate the Intel architecture, which stores the shift amount in 5 bits of the instruction, so a shift by 32 is actually a noop because (32 & 0x1f) == 0. I've seen more than one instance of this bug in widely distributed code.
Your tips here are rather basic. For something a bit meatier, see graphics.stanford.edu/~seander/bithacks.html
In loops, else flattening can even omit else:
for() {
    if(A) {
        ...
        continue;
    }
    if(B) {
        ...
        continue;
    }
    if(C) {
        ...
        continue;
    }
}
If you are a C expert, please try this challenge:
http //www.linkedin.com/groups?gid=4264441
This is wrong. On all compilers sizeof(char) is 1. The C standard defines sizeof in terms of numbers of chars, since a char is one byte. If sizeof(char) is not 1, you're not using a conforming C compiler.
branch prediction: but the lookup table is then mispredicted all the time, whereas the nested ifs are correctly branch predicted ~always and therefore free.
this blog is awesome :) all mediocre programmers should advertise themsleves widely so they, and their followers can be screened.
rather serious not nice Danger: your flags carry into the next flag if it is set twice. |= would probably be better, (or perhaps a language that doesn't use smileys <= ?:) (or perhaps less than a million settings ;)
settingsSoFar+=1<<(5-1)
No, sorry, that is wrong. sizeof(char) is always 1, and the sizeof all other types is measured in chars. Even if a character is stored as 2 or 4 bytes on some platform, sizeof(char) will equal 1. And don't forget that a "byte" is not 8 bits on every architecture.
On the ternary conditional operator ?: : yes it saves on typing but it's dog-slow compared to a switch statement. Save it for when a switch statement won't work (for instance, when an expression must be reevaluated if it doesn't match). The example you gave is the perfect time to use a switch statement, although your default case should be "str = rand() == 42?"four":"unknown number";break; to make the nested ?: code.
On DeMorgan's: This is CompSci 101, or 201 at the latest. They teach you precisely this in school.
On rounding: this code is not portable to platforms where a sizeof(char) is not 8. For chars the code should be unsigned char bitmap [NUMBER_OF_BITS + sizeof(char)-1/sizeof(char)]; You should be able to figure out how to do that with ints as well.
On real rounding: The best one of the list.
On bit-shifting: if they don't teach you this in school, you're at the wrong school. Same if they don't tell you that if you want multiplication, unless you have hardware reasons to bit-shift (side-effects in a MIPS processor, for instance), you should simply multiply. Let the complier sort it out. The compiler knows better than you, for instance, that multiplication always works, but weird things can happen on certain models of 64-bit processors running in 32-bit mode if you shift left by 32.
Else flattening: seriously dude, what crappy school did you go to that they didn't tell you about this?
On non-mainstream compilers: indeed, so apply that to your own examples! And bit-shifting a negative number works exactly as well as bit shifting a positive number, which is to say, again, if you want multiplication (or division), JUST DO IT!
// Set the xth bit (to 1).
flags |= 1 << x;
// Clear the xth bit (set it to 0).
flags &= ~(1 << x);
// Flip the xth bit (if it's set, clear it; if it's clear, set it).
flags ^= 1 << x;
// Extract the xth bit as an integer and store it in test.
test = flags >> x & 1;
#define elif else if
Assuming you are in an environment where saving memory is more precious than saving time, you can do so by "packing" information into casual numbers.
E.g. you've got 32 boolean flags determining settings of your program. Then you can save 1 to 5-th flag by doing:
settingsSoFar+=1<<(5-1)
Extracting is analogous.
Modus ponendo tollens.