首页 > 代码库 > C语言复习1
C语言复习1
一、C语言变量的内存分析
C语言寻址由大到小#include <stdio.h>
int main(int argc, const char * argv[]) {
int a = 100;
int b = 200;
printf("The address of a is %d\n", &a);
printf("The address of b is %d\n", &b);
return 0;
}
int main(int argc, const char * argv[]) {
int a = 100;
int b = 200;
printf("The address of a is %d\n", &a);
printf("The address of b is %d\n", &b);
return 0;
}
out:
The address of a is 1606416268
The address of b is 1606416264
The address of b is 1606416264
—》相差4个字节,由大到小
二/三、scanf 函数
这是一个阻塞式函数
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a = 0;
printf("Please input a num:");
scanf("%d", &a);
printf("The num is %d\n", a);
return 0;
}
int main(int argc, const char * argv[]) {
int a = 0;
printf("Please input a num:");
scanf("%d", &a);
printf("The num is %d\n", a);
return 0;
}
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a;
int b;
printf("Please input the first number:\n");
scanf("%d", &a);
printf("Please input the second number:\n");
scanf("%d", &b);
printf("The result is %d\n", a + b);
return 0;
}
int main(int argc, const char * argv[]) {
int a;
int b;
printf("Please input the first number:\n");
scanf("%d", &a);
printf("Please input the second number:\n");
scanf("%d", &b);
printf("The result is %d\n", a + b);
return 0;
}
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a;
int b;
printf("Please input the numbers:");
scanf("%d %d", &a, &b);
printf("The result is %d\n", a + b);
return 0;
}
int main(int argc, const char * argv[]) {
int a;
int b;
printf("Please input the numbers:");
scanf("%d %d", &a, &b);
printf("The result is %d\n", a + b);
return 0;
}
四、算术运算符
运算符的优先级
括号 > 正负 > 数学运算 > 位运算 > 数学对比 > 逻辑对比 > 条件运算 > 赋值运算
五、赋值运算符
复合赋值运算符
+=
-+
*=
/=
六、自增自减
a++ ++a
a— —a
ps:没有自乘自除:a** a//
七、sizeof
输出所占字节数
八 、关系运算
除了0之外都是true
返回值只有0和1, 真即为1, 假即为0
存在多个运算符的时候,可以使用结果1和0继续按照优先级顺序运算
九、逻辑运算
逻辑运算的结果只有1和0
逻辑与: &&
逻辑或: ||
位与: &
位或: |
位抑或: ^
#include <stdio.h>
int main(int argc, const char * argv[]) {
int result = 1 ^ 1;
printf("result = %d\n", result);
return 0;
}
int main(int argc, const char * argv[]) {
int result = 1 ^ 1;
printf("result = %d\n", result);
return 0;
}
out:
result = 0
逻辑非: !
任何数值都有真假!!!
十、三目运算
xx?xx:xx
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
int result = a>b?33:11;
printf("result = %d\n", result);
return 0;
}
int main(int argc, const char * argv[]) {
int a = 10;
int b = 20;
int result = a>b?33:11;
printf("result = %d\n", result);
return 0;
}
out:
result = 11
十一、if基本使用
十二、if使用注意
变量的作用域
十三、switch
条件只能是int 或 char
在case中定义了变量,必须使用{}包括起来,不然会出现编译错误
#include <stdio.h>
int main(int argc, const char * argv[]) {
int a = 10;
switch (a) {
case 10:
{
int b = a + 1;
break;
}
default:
break;
}
return 0;
}
int main(int argc, const char * argv[]) {
int a = 10;
switch (a) {
case 10:
{
int b = a + 1;
break;
}
default:
break;
}
return 0;
}
C语言复习1
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。