博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C语言不容易识别的坑
阅读量:6979 次
发布时间:2019-06-27

本文共 2895 字,大约阅读时间需要 9 分钟。

1、重复两次定义

#include
#include
#include
int a,b;void fun(){ a=100; b=200; printf("%d,%d\n",&a,&b);}int main(){ int a=5,b=7; fun(); printf("%d,%d\n",a,b); printf("%d,%d\n",&a,&b); return 0;}

从运行结果中可以看出,如果全局变量定义一次a,b, main函数中定义一次a,b两个地址是不一样的。

改成下面结果就对

#include
#include
#include
int a,b;void fun(){ a=100; b=200; printf("%d,%d\n",&a,&b);}int main(){ /*int*/ a=5,b=7; fun(); printf("%d,%d\n",a,b); printf("%d,%d\n",&a,&b); return 0;}

 

 

2、强制转换优先级

#include
#include
#include
int main(){ double a,b; a = 5.5; b = 2.5; printf("%f\n",(int)a+b/b); //先强制转换为 int a ,在+double型的b/b printf("%d",(int) (a+b/b)); return 0;}

 

3、等号的左边只能是变量,不可以是表达式

#include 
#include
int main(){ int k,a,b=1; unsigned long w=5; k=b+2=w; //错误语句 return 0;}

 

4、逗号运算符的使用(必须在赋值运算符前加括号)

#include 
#include
int main(){ int a=0, b=0,c=0; c = ( a-=a-5),(a=b,b+3); //赋值运算符的优先级比逗号运算符高,先把5赋值给c; printf("%d %d %d\n",a,b,c); c = ( ( a-=a-5),(a=b,b+3) ); //逗号运算符,把最右边的值赋值给c printf("%d %d %d",a,b,c); return 0;}

 

5、#define 预定义的用法

#include 
#include
#define S(x) 4*(x)*x+1int main(){ int k=5,j=2; printf("%d",S(k+j)); // 4*(5+2)*5+2+1 =143 注意define没有括号坚决不加括号,直接写 return 0;}

 

6、&&符号左边不成立不再执行右边, ||左边成立不再执行右边

#include 
#include
int main(){ int i=1,j=1,k=2; if( (j++||k++) && i++) //k++将不会被执行 printf("%d %d %d\n",i,j,k); //2 2 2 return 0;}

 

7、scanf中出现*表示跳过指定宽度

#include 
#include
int main(){ int a,b; double c; scanf("%2d%*2d%2d%2lf",&a,&b,&c); //跳过第3,4个字符 printf("%d %d %f",a,b,c); return 0;}

 

8、字符串长度,遇到转义字符

#include 
#include
#include
int main(){ char s1[] = "\\141\141abc\t"; printf("\\141\141abc\t :%d\n",strlen(s1)); /* \\ 是一个字符,前一个表示转义 141 三个字符 \141 1个字符,注意这里是八进制符,如果超过8那就不是按三个算了 abc 三个字符 \t 一个字符 再加上结束符'\0' 共10个字符 而strlen(s)的值为9 */ char s2[] = "\\141\1111abc\t"; char s3[] = "\\141\128abc\t"; char s4[] = "\\141\888abc\t"; printf("\\141\1111abc\t :%d\n",strlen(s2)); printf("\\141\89abc\t :%d\n",strlen(s3)); printf("\\141\888abc\t :%d\n",strlen(s4)); return 0;}

从这里也可以看出 \141代表的是字符a,\111代表的是字符I,而超过8的就直接输出了

 

9、自增自减运算符

#include 
#include
#include
int main(){ int i=1,s=3; do{ s+= i++; //这里是s为 4 7 11 17而不是5 9 15 if( s%7==0) continue; else ++i; printf("i=%d s=%d\n",i,s); }while( s<15); return 0;}

 

转载于:https://www.cnblogs.com/yuxiaoba/p/8492855.html

你可能感兴趣的文章
ChaLearn Gesture Challenge_2:examples体验
查看>>
asp.net 获取当前时间的格式
查看>>
Java实现二维码
查看>>
平安陆金所-点金计划,简直是骗子行为。
查看>>
C#设置IE代理
查看>>
Zabbix 3.0 从入门到精通(zabbix使用详解)
查看>>
sql server 在占用服务器内存居高不下怎么办【转】
查看>>
回忆之城市搜索
查看>>
MYSQL忘记登录密码
查看>>
halcon算子翻译——set_framegrabber_callback
查看>>
FairyGUI和NGUI对比
查看>>
Avogadro-2
查看>>
MySQL5.7配置日志
查看>>
1 sec on Large Judge (java): https://github.com/l...
查看>>
面向对象(三)
查看>>
如何衡量一个项目的交付质量???
查看>>
Ka的递归编程练习 Part4|Hanoi汉诺塔,双色汉诺塔的也有
查看>>
3月14号作业
查看>>
Feign实现服务调用
查看>>
菜鸟学习HTML5+CSS3(一)
查看>>