对于初学着学习C语言一定会遇到写一个求和的C代码,这一段代码虽然很简单,但写出漂亮的代码,是要很多讲究的,请看下面这代码:
#include <stdio.h>
#include <stdlib.h>
#include <cutils.h>
typedef int bool;
#define true 1;
#define false 0;
unsigned checksum(void *buffer,size_t len,unsigned int seed)
{
unsigned char *buf = (unsigned char*)buffer;
size_t i;
for(i = 0;i < len; i++)
seed += (unsigned char)(*buf++);
return seed;
}
这段代码功能很简单,但蕴含C语言的精华值得细细体味。