#include<stdio.h> int main() { int x=2,y,z; x *=(y = z = 5);printf("%d\n”,x); //10 后面表达式的结果是5 z = 3; x ==(y = z); printf("%d\n”,x); //10 只是判断,没有给x赋值 x =(y == z); printf("%d\n”,x); //1 x = (y&z); printf("%d\n”,x); //3 位与操作 x = (y&&z); printf("%d\n”,x); //1 y = 4; x = (y | z); printf("%d\n”,x); //7 位或操作 x = (y || z); printf("%d\n”,x); //1 return 0; }
#include<stdio.h> main() { int b = 3; int arr[] = {6,7,8,9,10}; int *ptr = arr; *(ptr++)+=123; // arr[0]变成129,ptr指向arr[1] printf("%d,%d\n",*ptr,*(++ptr)); }
#include<stdio.h> int main() { unsigned char a = 0xa5; //a中装着一个16进制数 二进制为:1010 0101 unsigned char b = ~a>>4; //a参与运算,自动转换为int,b的结果二进制为 //1111 0101 printf("a = %d\n",a); //a的十进制为165 printf("b = %d\n",b); //b的十进制为245 return 0; }
int main() { unsigned int a = 0xCCCCCCF7; unsigned char i = (unsigned char)a; //i中装着a的后两位,也就是F7 char* b = (char*)&a; //b指向a printf("%08x,%08x",i,*b); //16进制输出,000000F7 ,FFFFFFF7 return 0; }
(x & (x-1) == 0) ? printf("yes\n") : printf("yes\n")
#include<stdio.h> main() { int count = 0; int m = 9999; while(m) { count++; m = m&(m-1); } printf("%d\n",count); //8,进两次循环m的二进制去掉一位 }
a = a + b; b = a - b; a = a - b; //还有更快的位操作版本: a = a ^ b; b = b ^ a; a = a ^ b;
#define S (365*24*60*60)
#define min((a), (b)) ((a)<(b))?(a):(b)
#include<stdio.h> struct{ short a1; short a2; short a3; }A; struct{ long a1; short a2; }B; int main() { char* ss1 = "0123456789"; char ss2[] = "0123456789"; char ss3[100] = "0123456789"; int ss4[100]; char q1[] = "abc"; char q2[] = "a\n"; char* q3 = "a\n"; char (*str1)[100] = (char(*)[100])malloc(100); void* str2 = (void*)malloc(100); printf("%d\n",sizeof(ss1)); //4 printf("%d\n",sizeof(ss2)); //11 printf("%d\n",sizeof(ss3)); //100 printf("%d\n",sizeof(ss4)); //400 printf("%d\n",sizeof(q1)); //4 printf("%d\n",sizeof(q2)); //3 printf("%d\n",sizeof(q3)); //4 printf("%d\n",sizeof(A)); //6 结构体都跟其中最大的的元素类型对齐 printf("%d\n",sizeof(B)); //16 跟long对齐,8*2 printf("%d\n",sizeof(*str1)); //100 printf("%d\n",sizeof(str2)); //4 return 0; }
#include<stdio.h> main() { char *a []={"hello","the","world"}; char **pa=a; pa++; printf(*pa); //the }
#include<stdio.h> int main() { int x=10, y=10, i; for(i=0;x>8;y=i++) { printf("%d,%d",x--,y); //10, 10 return 0; } }
typedef struct s { int i; int * p; }S; main() { S s; //定义一个结构体,其中有两个元素,i 和 *p int *p = &s.i; //定义指针变量p,装着s中i的地址 p[0] = 4; //下标运算,在i中放入4 p[1] = 3; //在p中放入3 s.p = p; //把指针p中的地址,也就是s中i的地址给了s中的p s.p[1] = 1; //下标运算,把s中的p的东西,也就是i的地址的下一位,也就是 //s中p装入一个1 s.p[0] = 2; //在这一行崩溃,p中装着一个常量,无法进行写操作 }
原文:http://blog.csdn.net/x140yuyu/article/details/23618813