1
0

04_arithmetic.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /*
  2. * 示例4:算术运算(Arithmetic Operations)
  3. * 功能:学习 C 语言中的基本算术运算符
  4. */
  5. #include <stdio.h>
  6. int main() {
  7. int a = 10;
  8. int b = 3;
  9. // 加法运算
  10. int sum = a + b;
  11. printf("%d + %d = %d\n", a, b, sum);
  12. // 减法运算
  13. int diff = a - b;
  14. printf("%d - %d = %d\n", a, b, diff);
  15. // 乘法运算
  16. int product = a * b;
  17. printf("%d * %d = %d\n", a, b, product);
  18. // 除法运算(注意:整数除法会丢弃小数部分)
  19. int quotient = a / b;
  20. printf("%d / %d = %d(整数除法,丢弃小数)\n", a, b, quotient);
  21. // 取余运算(求余数)
  22. int remainder = a % b;
  23. printf("%d %% %d = %d(取余数)\n", a, b, remainder);
  24. // 浮点数除法(保留小数)
  25. float result = (float)a / b; // (float)a 把 a 转换为浮点数
  26. printf("%d / %d = %.2f(浮点数除法)\n", a, b, result);
  27. // 自增和自减运算
  28. int count = 5;
  29. count++; // count 加 1,等价于 count = count + 1
  30. printf("自增后 count = %d\n", count);
  31. count--; // count 减 1
  32. printf("自减后 count = %d\n", count);
  33. return 0;
  34. }