C语言不容易识别的坑
1、重复两次定义
#include<stdio.h>
#include<stdlib.h>
#include<string.h> int a,b;
void fun()
{
a=;
b=;
printf("%d,%d\n",&a,&b);
}
int main()
{
int a=,b=;
fun();
printf("%d,%d\n",a,b);
printf("%d,%d\n",&a,&b);
return ;
}

从运行结果中可以看出,如果全局变量定义一次a,b, main函数中定义一次a,b两个地址是不一样的。
改成下面结果就对
#include<stdio.h>
#include<stdlib.h>
#include<string.h> int a,b;
void fun()
{
a=;
b=;
printf("%d,%d\n",&a,&b);
}
int main()
{
/*int*/ a=,b=;
fun();
printf("%d,%d\n",a,b);
printf("%d,%d\n",&a,&b);
return ;
}

2、强制转换优先级
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 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 ;
}

3、等号的左边只能是变量,不可以是表达式
#include <stdio.h>
#include <stdlib.h> int main()
{
int k,a,b=;
unsigned long w=;
k=b+=w; //错误语句
return ;
}
4、逗号运算符的使用(必须在赋值运算符前加括号)
#include <stdio.h>
#include <stdlib.h> int main()
{
int a=, b=,c=;
c = ( a-=a-),(a=b,b+); //赋值运算符的优先级比逗号运算符高,先把5赋值给c;
printf("%d %d %d\n",a,b,c); c = ( ( a-=a-),(a=b,b+) ); //逗号运算符,把最右边的值赋值给c
printf("%d %d %d",a,b,c);
return ;
}

5、#define 预定义的用法
#include <stdio.h>
#include <stdlib.h> #define S(x) 4*(x)*x+1
int main()
{
int k=,j=;
printf("%d",S(k+j)); // 4*(5+2)*5+2+1 =143 注意define没有括号坚决不加括号,直接写
return ;
}
6、&&符号左边不成立不再执行右边, ||左边成立不再执行右边
#include <stdio.h>
#include <stdlib.h> int main()
{
int i=,j=,k=;
if( (j++||k++) && i++) //k++将不会被执行
printf("%d %d %d\n",i,j,k); //2 2 2
return ;
}
7、scanf中出现*表示跳过指定宽度
#include <stdio.h>
#include <stdlib.h> 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 ;
}

8、字符串长度,遇到转义字符
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
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 ;
}
从这里也可以看出 \141代表的是字符a,\111代表的是字符I,而超过8的就直接输出了
9、自增自减运算符
#include <stdio.h>
#include <stdlib.h>
#include<string.h> int main()
{
int i=,s=;
do{
s+= i++; //这里是s为 4 7 11 17而不是5 9 15
if( s%==) continue;
else ++i;
printf("i=%d s=%d\n",i,s);
}while( s<);
return ;
}
C语言不容易识别的坑的更多相关文章
- [R语言]R语言计算unix timestamp的坑
R+mongo的组合真是各种坑等着踩 由于mongo中的时间戳普遍使用的是unix timestamp的格式,因此需要对每天的数据进行计算的时候,很容易就想到对timestamp + gap对方式来实 ...
- Python locale 多语言模块和我遇到的坑
Table of Contents 1. locale遇到的问题 1.1. locale 简介 1.1.1. 什么是locale 1.1.2. locale 相关命令 1.2. Python loca ...
- Spring Boot Security 国际化 多语言 i18n 趟过巨坑
网上很多的spring boot国际化的文章都是正常情况下的使用方法 如果你像我一样用了Spring Security 那么在多语言的时候可能就会遇到一个深渊 Spring Security里面的异常 ...
- C语言read函数的那些坑
今天在复习UNIX文件系统,用到那个read函数,但是无意中却掉到一个坑里了,用了一个多小时才找到问题根源,这里记录一下. 问题是这样的:我需要使用read和write函数把键盘输入的信息复制到输出. ...
- Python语言基础01-初识Python
本文收录在Python从入门到精通系列文章系列 1. Python简介 1.1 Python的历史 Python的创始人为吉多·范罗苏姆(荷兰语:Guido van Rossum) 1989年的圣诞节 ...
- C语言那年踩过的坑--局部变量,静态变量,全局变量在内存中存放的位置
先看几个概念: 1.bss是英文block started by symbol的简称,通常是指用来存放程序中未初始化的全局变量的一块内存区域,在程序载入时由内核清0.bss段属于静态内存分配.它的初始 ...
- Android 应用内多语言切换
最近公司的 App 里需要用到多语言切换,简单来说,就是如果用户没有选择语言选项时,App 默认跟随系统语言,如果用户在 App 内进行了语言设置,那么就使用用户设置的语言.当然,你会发现,App 的 ...
- C++的坑真的多吗?
先说明一下,我不希望本文变成语言争论贴.希望下面的文章能让我们客观理性地了解C++这个语言.(另,我觉得技术争论不要停留在非黑即白的二元价值观上,这样争论无非就是比谁的嗓门大,比哪一方的观点强,毫无价 ...
- 【转】R语言知识体系概览
摘要:R语言的知识体系并非语法这么简单,如果都不了R的全貌,何谈学好R语言呢.本文将展示介绍R语言的知识体系结构,并告诉读者如何才能高效地学习R语言. 最近遇到很多的程序员都想转行到数据分析,于是就开 ...
随机推荐
- Android上的线程安全
Thread-safe methods In some situations, the methods you implement might be called from more than one ...
- net start iisadmin报错:系统找不到指定的文件
IIS Admin Service不能启动 ,直接启动或命令(net start iisadmin)都不成功.导致IIS站点访问异常. 最终参考网上解决方案: 这是大多是由于windows\syste ...
- java自动包装与解包
关于java的自动包装机制想必大家都用过吧,一般这些机制都用于在往容器中存储基本类型数据的时候,因为容器中不允许存在基本数据类型,所以就会调用自动包装机制,将基本数据类型转换为对象,将基本数据保存在对 ...
- iOS---设置控件的内容模式
容易混淆的内容摆放属性: 1. textAligment : 文字的水平方向的对齐方式 取值 NSTextAlignmentLeft = 0, // 左对齐 NSTextAlignmentCenter ...
- hibernate4+spring4+struts2的Maven中的pom.xml文件的配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...
- 泛型术语:占位类型placeholder
Here’s a generic version of the same code: struct Stack<Element> { var items = [Element]() mut ...
- swift class extension 与继承
1.扩展中无法继承重写已有函数,不能添加函数. Extensions can add new functionality to a type, but they cannot override exi ...
- css 样式通用样式
属性: vertical-align (这个属性主要作用是用于将相邻的文本与元素对齐,用于对齐行内元素,也就是说,display 的属性为 inline. inline-block 行内块,顾名 ...
- 【原创】如何编写c#用户登陆后用户名在前台显示
这种肯定是判断session啦!!!!! @{ string username = (string)Session["username"]; user user = new use ...
- AutoIt简单使用
以上是自己在公司做培训的PPT首页,其实在线的中文参考文档很全面,很值得学习的.