1.#将一个int型数据的第三位置1,再将其第三位清零,其它位不变
#include
#define BIT3_1 0x08
#define BIT3_0 0xfffffff7
int main(int argc, const char *argv[])
{
int a=0xffffff00;
a=a|BIT3_1;
printf("%#x\n",a);
a=a&BIT3_0;
printf("%#x\n",a);
return 0;
}
2.#用malloc分配0个空间,malloc的返回值是一个地址,不是NULL
#include
int main(int argc, const char *argv[])
{
char *ptr, *ptr1 ;
ptr1 = (char *)malloc(0);
if((ptr=(char *)malloc(0)) == NULL)
printf("Got a null pointer\n");
else
{
printf("Got a valid pointer\n");
printf("%p\n",ptr);
printf("%p\n",ptr1);
free(ptr);
free(ptr1);
}
return 0;
}
3.#运算符优先级的应用
#include
int main(int argc, const char *argv[])
{
int a = 5, b = 7, c;
c = a+++b;
printf("a=%d\n",a);
printf("b=%d\n",b);
printf("c=%d\n",c);
return 0;
}
4.#运算符优先级的应用
#include
int main(int argc, const char *argv[])
{
int a = 2, b ;
b = a<<2+3;
printf("c=%d\n",b);
return 0;
}
5.#变量的存活期
#include
#include
#include
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
int main(int argc, const char *argv[])
{
char *str = NULL;
GetMemory(str);
strcpy(str,"hello world");
printf("str=%s\n",str);
return 0;
}
阅读(505) | 评论(0) | 转发(0) |