基本运算

code

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. int a = 4 + 5 * 2 - 6 / 3 + 11 % 4;
  6. printf("%d\n", a);
  7. return 0;
  8. }

获取数值中的每一位

code

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. int a = 4 + 5 * 2 - 6 / 3 + 11 % 4; /* assign initial value */
  6. printf("%d\n", a);
  7. scanf("%d", &a); /* wait to input when add \n after escape character */
  8. printf("a = %d\n", a);
  9. while (a != 0) {
  10. printf("%d\n", a % 10);
  11. a /= 10;
  12. }
  13. return 0;
  14. }

image.png

浮点数运算

存储方式

code

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. float f = 234.56;
  6. return 0;
  7. }

image.png

如何做浮点型的等值运算?

矛盾点—-浮点数无法直接比较大小

code

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. //int a = 8;
  6. ///* 关系运算符与逻辑运算符搭配 */
  7. //if (a > 3 && a < 10) {
  8. // printf("a is right.\n");
  9. //}
  10. //else {
  11. // printf("a is wrong.\n");
  12. //}
  13. /* 判断两个浮点数是否相等 */
  14. float f = 234.56;
  15. if (f == 234.56) {
  16. printf("f is equal to 234.56.\n");
  17. }
  18. else {
  19. printf("f is not equal to 234.56.\n");
  20. }
  21. return 0;
  22. }

image.png

解决方案【做差逼近系统设定的精度】

code

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. //int a = 8;
  6. ///* 关系运算符与逻辑运算符搭配 */
  7. //if (a > 3 && a < 10) {
  8. // printf("a is right.\n");
  9. //}
  10. //else {
  11. // printf("a is wrong.\n");
  12. //}
  13. /* 判断两个浮点数是否相等 */
  14. float f = 234.56;
  15. if (f - 234.56 > -0.0001 && f - 234.56 < 0.0001) {
  16. printf("f is equal to 234.56.\n");
  17. }
  18. else {
  19. printf("f is not equal to 234.56.\n");
  20. }
  21. return 0;
  22. }

image.png

逻辑非运算符

code1

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. int a = 8;
  6. /* 关系运算符与逻辑运算符搭配 */
  7. if (a > 3 && a < 10) {
  8. printf("a is right.\n");
  9. }
  10. else {
  11. printf("a is wrong.\n");
  12. }
  13. /* 判断两个浮点数是否相等 */
  14. float f = 234.56;
  15. if (f - 234.56 > -0.0001 && f - 234.56 < 0.0001) {
  16. printf("f is equal to 234.56.\n");
  17. }
  18. else {
  19. printf("f is not equal to 234.56.\n");
  20. }
  21. /* 学习逻辑非 */
  22. int i = 5, j = 10;
  23. i = !j;
  24. printf("i=%d\n", i);
  25. return 0;
  26. }

code2

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. int a = 8;
  6. /* 关系运算符与逻辑运算符搭配 */
  7. if (a > 3 && a < 10) {
  8. printf("a is right.\n");
  9. }
  10. else {
  11. printf("a is wrong.\n");
  12. }
  13. /* 判断两个浮点数是否相等 */
  14. float f = 234.56;
  15. if (f - 234.56 > -0.0001 && f - 234.56 < 0.0001) {
  16. printf("f is equal to 234.56.\n");
  17. }
  18. else {
  19. printf("f is not equal to 234.56.\n");
  20. }
  21. /* 学习逻辑非 */
  22. int i = 5, j = 10;
  23. i = !!j;
  24. printf("i=%d\n", i);
  25. return 0;
  26. }

image.png

关于赋值表达式应该注意的

表达式的值只存在于运算过程中

左值只能是变量

自增减运算符巧记

code

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. int i = -1;
  6. int j;
  7. /*前++直接按照顺序运算*/
  8. j = i++ > -1; /* 拆分成两步:j = i > -1, i++ */
  9. printf("i = %d, j = %d\n", i, j);
  10. printf("i 的字节数为: %d.\n", sizeof(i));
  11. return 0;
  12. }

获取数字输入,输出对应的字符【数字需在0-128之间】

code1

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. char a;
  6. scanf("%d", &a);
  7. printf("%c", a);
  8. return 0;
  9. }

image.png

警告

code1

Run-Time Check Failure #2 - Stack around the variable ‘a’ was corrupted.

当你操作的空间,超过了变量本身占用的空间的大小时,就会出现栈崩溃的错误,stack around the variable ‘a’ was corrupted;

image.png

code2

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. int main() {
  5. int a;
  6. scanf("%d", &a);
  7. printf("%c", a);
  8. return 0;
  9. }

image.png