Posts Tagged ‘c’

C programming language most popular

April 22, 2010 Leave a comment

Good news for C programmers: The April 2010 update of the TIOBE index shows that C is the most popular programming language. With about 18% it is close to Java, but 0.007% ahead. Of course, there is criticism of how to measure language popularity, and there are other methods as well. But it is good to know that doing low-level work in C is staying on the popular and important side of software development.

Categories: programming Tags:

Coding in C song

March 14, 2010 Leave a comment

This is a funny song for everybody writing C code:

Categories: programming Tags: , ,

Compiler optimization

January 13, 2010 2 comments

I saw this a while back, but i still think that this is an impressive example of what modern compilers can do. Say, when writing C code you have the need to rotate bits. There is no rotate operator in C so the bit rotation has to be implemented like this:

$ cat rotate.c
unsigned long rotate_left(unsigned long a, unsigned int shift)
return a << shift | a >> (sizeof(a) * 8 - shift);
unsigned long rotate_right(unsigned long a, unsigned int shift)
return a >> shift | a << (sizeof(a) * 8 - shift);

Now compile it with gcc (version 4.4.1 here, but most other versions should do the same):

$ gcc -c -O rotate.c

The compiler knows that the C code only rotates the bits and that this can be done with the x86 rol and ror instructions:

$ objdump -d rotate.o

rotate.o: file format elf32-i386

Disassembly of section .text:

00000000 :
0: 55 push %ebp
1: 89 e5 mov %esp,%ebp
3: 8b 4d 0c mov 0xc(%ebp),%ecx
6: 8b 45 08 mov 0x8(%ebp),%eax
9: d3 c0 rol %cl,%eax
b: 5d pop %ebp
c: c3 ret

0000000d :
d: 55 push %ebp
e: 89 e5 mov %esp,%ebp
10: 8b 4d 0c mov 0xc(%ebp),%ecx
13: 8b 45 08 mov 0x8(%ebp),%eax
16: d3 c8 ror %cl,%eax
18: 5d pop %ebp
19: c3 ret

This means that it is usually the best thing to write readable C code and let the compiler optimize things. Modern compilers can be pretty good at this.

Categories: programming Tags: ,