我发现你们c语言的基础好像不是太好, 必须要强化。
试着作一些练习, 提高自己:
比如 下面的小程序 , 自己试着改改,编译一下, 我有一些注释, 有些是错的。
这个练习是我自己刚才练的, 因为改 clock的时候, 一下子糊涂了, 搞个小程序试试 , 果然有问题。
尤其注意 溢出的问题,
另外 写driver 要注意用 u8/u16/u32 这样的类型, 而不是char ,int ,long 这些不可移植的。
尤其是写driver的时候 经常要用掩码什么的, ~0UL 用的很多。
另外 注意 ,%u , %hu %hhu , 这些不太常用的 printf()的参数, man 3 printf
#include <stdio.h>
#include <stdlib.h>
#define INT_MAX ((int)(~0U>>1))
#define INT_MIN (-INT_MAX - 1)
#define UINT_MAX (~0U)
#define LONG_MAX ((long)(~0UL>>1))
#define LONG_MIN (-LONG_MAX - 1)
#define ULONG_MAX (~0UL)
#define LLONG_MAX ((long long)(~0ULL>>1))
#define LLONG_MIN (-LLONG_MAX - 1)
#define ULLONG_MAX (~0ULL)
int main(void)
{
unsigned long long ll1 = 0;
unsigned int ii = 0;
unsigned char cc = 0;
unsigned char mul = 2;
//ii = (1<<32)-1;
//ii = 4294967295;
ii = (~0U);
printf("ii=%u\n",ii);
ll1 = (unsigned long long)ii * mul; //KERNEL 里面有一些很大的数 ,尤其要注意。
//ll1 = (unsigned long long)(ii * mul); //这个就是错误的, 溢出了。 自己试试,得到的value就是0 了。
//ll1 = (unsigned long long)(ii *2); //error ,overlow
//ll = ii *2;//the same above
cc = ii; //only get the low 8 bits
printf("ll1=%llu\n",ll1);
printf("cc=%u\n",cc);
printf("cc=%hhu\n",cc); //the result is same to above
printf("LONG_MAX=%ld, hex type=%#08lx\n",LONG_MAX,LONG_MAX);
printf("ULONG_MAX=%lu , hex type=%#08lx\n",ULONG_MAX,ULONG_MAX);
printf("ULLONG_MAX=%llu , hex type=%#08llx\n",ULLONG_MAX,ULLONG_MAX);
return 0;
}