1、赋值运算符和比较运算符的优先级问题
int c ;
c = getchar() != EOF
(c = getchar()) != EOF
2、x *= y +1 问题。
#include
void sizeof_type_test(void);
void strlen_test(void);
int my_strlen(char s[]);
int is_leap_year(unsigned int year);
void is_leap_year_test(void);
void relation_operate_test(void);
void my_atoi_test(void);
int my_atoi(char s[]);
int my_lower(int c);
void my_lower_test(void);
void signed_unsigned_test(void);
int my_rand(void);
void my_srand(unsigned int seed);
void squeeze_test(void);
void squeeze(char s[],int c);
void my_strcat_test(void);
void my_strcat(char s[],char t[]);
void char_point_test(void);
void getbits_test(void);
unsigned int getbits(unsigned int x,int p,int n);
int bitcount(unsigned x);
void bitcount_test(void);
void question_operate_test(void);
void question_operate(int a[],int n);
int main(void)
{
//sizeof_type_test();
//strlen_test();
//
//relation_operate_test();
//my_atoi_test();
//my_lower_test();
//signed_unsigned_test();
//squeeze_test();
//char_point_test();
//my_strcat_test();
//getbits_test();
//bitcount_test();
question_operate_test();
return 0;
}
void sizeof_type_test(void)
{
printf("sizeof short = %d\n",sizeof(short));
printf("sizeof int = %d\n",sizeof(int));
printf("sizeof long long = %d\n",sizeof(long long));
printf("sizeof long int = %d\n",sizeof(long int));
printf("sizeof double = %d\n",sizeof(double ));
}
void strlen_test(void)
{
char *name = "China";
printf("the name of length is %d",my_strlen(name));
}
int my_strlen(char s[])
{
int i;
while (s[i] != '\0')
++i;
return i;
}
void is_leap_year_test(void)
{
unsigned int year = 2016;
int i;
for (i = 0; i < 100; ++i)
{
is_leap_year(year + i);
}
}
int is_leap_year(unsigned int year)
{
if ( year %4 == 0 && year % 100 !=0 || year % 400 == 0)
printf("%d is a leap year \n",year);
else
printf("%d is not a leap year \n",year);
}
void relation_operate_test(void)
{
int i =1;
int j =2;
if (i >= j)
printf("i >= j\n");
else
printf("i < j\n");
if (i <= j)
printf("i <= j\n");
else
printf("i > j\n");
}
void my_atoi_test(void)
{
char *pdata = "124a45";
printf("%d\n",my_atoi(pdata));
}
int my_atoi(char s[])
{
int i,n;
for ( i = 0; s[i] >= '0' && s[i] <= '9'; ++i)
n = 10 * n + ( s[i] - '0');
return n;
}
void my_lower_test(void)
{
char c = 'A';
int i;
for (i = 0; i < 26; ++i)
printf("%c ",my_lower('A' + i) );
}
int my_lower(int c)
{
if ( c >= 'A' && c <= 'z' )
return 'a' + c - 'A';
else
return c;
}
void signed_unsigned_test(void)
{
unsigned char ui = 10;
signed char si = -1;
//有符号和无符号之间的运算,我总是模不清楚
unsigned int n = ui + si;
printf("n = %d\n", n);
}
unsigned long int next = 1;
int my_rand(void)
{
next = next *1102515245 + 12345;
return (unsigned int)(next/65536) %32678;
}
void my_srand(unsigned int seed)
{
next = seed;
}
//函数用来测试字符指针和字符数组的区别
//看字符指针是否可以修改
void char_point_test(void)
{
char *pstr = "HAC";
puts(pstr);
//pstr[1] = 'a';
//pstr[0] = pstr[1]; //这里不能修改,是指针指向的内容本身就是 不能修改的
puts(pstr);
while (*pstr)
{
putchar(*pstr);
printf(" %p\n",pstr); //每个字节的地址,是从低到高的
pstr++;
}
}
void squeeze_test(void)
{
char str[]= "bbbbbaaabbbcccbb";
//char *pstr= "bbbbbaaabbbcccbb";//用字符串指针操作是会有段错误
squeeze(str,'b');
printf("%s\n",str);
}
void squeeze(char s[],int c)
{
int i, j;
for (i = j = 0; s[i] != '\0'; i++)
{
if (s[i] != c)
{
s[j++] = s[i];
}
}
//字符串结束符,一定要有,并且必须放到for循环外面
s[j] = '\0';
}
void my_strcat_test(void)
{
char s[1024] ="HAC";
puts(s);
char t[] =" is my company";
my_strcat(s,t);
puts(s);
char d[] = "and I love this company";
my_strcat(s,d);
puts(s);
}
void my_strcat(char s[],char t[])
{
int i , j;
i = j = 0;
while (s[i] != '\0')
i++;
while ( (s[i++] = t[j++]) != '\0')//这里抄写犯的错误是 两个数组写成一样的。
;
}
void getbits_test(void)
{
//0001 0001 0010 0011
unsigned int n = 0x1123;
printf("%d\n", getbits(n,5,3));
}
unsigned int getbits(unsigned int x,int p,int n)
{
//0x89
return ( x >> (p + 1 - n) &~(~ 0<< n));
}
void bit_operate_test(void)
{
// & | << >> ~ ^ C语言提供了6个位操作符号
int i = 0x020;
int tmp;
tmp = i << 1;
printf("%d\n",tmp);
tmp = i << 1;
printf("%d\n",tmp);
tmp = i >> 1;
printf("%d\n",tmp);
tmp = i & 0x01;
printf("%d\n",tmp);
tmp = i | 0x01;
printf("%d\n",tmp);
}
void bitcount_test(void)
{
//unsigned char ui = 0x2f;
//printf("%d\n",bitcount(ui));
signed char si = -2;
printf("%d\n",bitcount(si));
}
//此函数是为了验证 x *= y +1 等价于 x = x * (y + 1)
int bitcount(unsigned x)
{
int b;
for (b = 0; x != 0; x >>= 1)
if (x & 01)
b++;
return b;
}
//测试的时候,为什么会11,12另外输出?
void question_operate_test(void)
{
int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,14,15};
#define N sizeof(a) /sizeof(a[0])
question_operate(a,N);
}
void question_operate(int a[],int n)
{
int i;
for (i = 0; i < n; ++i)
printf("%d%c",a[i],(i%10 == 9 || i == '\n') ? '\n':' ');
}
//1、说明 C语言的运算符非常的丰富,这个我们可以从以上的代码中可以看出来
//2、记住C语言代码里面的每一个运算符的具体含义,是比较难的,唯一理解这些好的用法,就是去实践。
void operat_test(void)
{
int i = +-10;
printf("i = %d",i);
//if ( (x & MASK) == 0)
//dosomthing...
//这个例子就是为了说明,x = f() + g()计算顺序对于结果的影响。
//printf("%d %d\n",++n,power(2,n));
}