基本运算
code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 4 + 5 * 2 - 6 / 3 + 11 % 4;
printf("%d\n", a);
return 0;
}
获取数值中的每一位
code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 4 + 5 * 2 - 6 / 3 + 11 % 4; /* assign initial value */
printf("%d\n", a);
scanf("%d", &a); /* wait to input when add \n after escape character */
printf("a = %d\n", a);
while (a != 0) {
printf("%d\n", a % 10);
a /= 10;
}
return 0;
}
浮点数运算
存储方式
code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
float f = 234.56;
return 0;
}
如何做浮点型的等值运算?
矛盾点—-浮点数无法直接比较大小
code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
//int a = 8;
///* 关系运算符与逻辑运算符搭配 */
//if (a > 3 && a < 10) {
// printf("a is right.\n");
//}
//else {
// printf("a is wrong.\n");
//}
/* 判断两个浮点数是否相等 */
float f = 234.56;
if (f == 234.56) {
printf("f is equal to 234.56.\n");
}
else {
printf("f is not equal to 234.56.\n");
}
return 0;
}
解决方案【做差逼近系统设定的精度】
code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
//int a = 8;
///* 关系运算符与逻辑运算符搭配 */
//if (a > 3 && a < 10) {
// printf("a is right.\n");
//}
//else {
// printf("a is wrong.\n");
//}
/* 判断两个浮点数是否相等 */
float f = 234.56;
if (f - 234.56 > -0.0001 && f - 234.56 < 0.0001) {
printf("f is equal to 234.56.\n");
}
else {
printf("f is not equal to 234.56.\n");
}
return 0;
}
逻辑非运算符
code1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 8;
/* 关系运算符与逻辑运算符搭配 */
if (a > 3 && a < 10) {
printf("a is right.\n");
}
else {
printf("a is wrong.\n");
}
/* 判断两个浮点数是否相等 */
float f = 234.56;
if (f - 234.56 > -0.0001 && f - 234.56 < 0.0001) {
printf("f is equal to 234.56.\n");
}
else {
printf("f is not equal to 234.56.\n");
}
/* 学习逻辑非 */
int i = 5, j = 10;
i = !j;
printf("i=%d\n", i);
return 0;
}
code2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a = 8;
/* 关系运算符与逻辑运算符搭配 */
if (a > 3 && a < 10) {
printf("a is right.\n");
}
else {
printf("a is wrong.\n");
}
/* 判断两个浮点数是否相等 */
float f = 234.56;
if (f - 234.56 > -0.0001 && f - 234.56 < 0.0001) {
printf("f is equal to 234.56.\n");
}
else {
printf("f is not equal to 234.56.\n");
}
/* 学习逻辑非 */
int i = 5, j = 10;
i = !!j;
printf("i=%d\n", i);
return 0;
}
关于赋值表达式应该注意的
表达式的值只存在于运算过程中
左值只能是变量
自增减运算符巧记
code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int i = -1;
int j;
/*前++直接按照顺序运算*/
j = i++ > -1; /* 拆分成两步:j = i > -1, i++ */
printf("i = %d, j = %d\n", i, j);
printf("i 的字节数为: %d.\n", sizeof(i));
return 0;
}
获取数字输入,输出对应的字符【数字需在0-128之间】
code1
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
char a;
scanf("%d", &a);
printf("%c", a);
return 0;
}
警告
code1
Run-Time Check Failure #2 - Stack around the variable ‘a’ was corrupted.
当你操作的空间,超过了变量本身占用的空间的大小时,就会出现栈崩溃的错误,stack around the variable ‘a’ was corrupted;
code2
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main() {
int a;
scanf("%d", &a);
printf("%c", a);
return 0;
}