Arithmetics
Variable
  • Made up of letters and digits
  • The first character must be a letter
  • The underscore ‘‘_’’ counts as a letter
  • Don’t begin variable names with underscore, however, since library routines often use such names
  • Upper and lower case letters are distinct, so x and X are two different names
  • Traditional C practice is to use lower case for variable names, and all upper case for symbolic constants
  • Constant
    1. #include <stdio.h>
    2.  
    3. #define PI 3.14
    4.  
    5. int main()
    6. {
    7. const int j = 30;
    8. //j = 100; // not allowed to change value
    9.  
    10. printf("%f\n", PI);
    11.  
    12. return 0;
    13. }
    Arithmetic Operators
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int i = 10, j = 100;
    6.  
    7. int r;
    8.  
    9. r = i + j;
    10. printf("%d\n", r); // 110
    11.  
    12. r = i - j;
    13. printf("%d\n", r); // -90
    14.  
    15. r = i * j;
    16. printf("%d\n", r); // 1000
    17.  
    18. r = j / i;
    19. printf("%d\n", r); // 10
    20.  
    21. printf("%f\n", (float) i / j); // 0.1
    22. printf("%f\n", (float) (i / j)); // 0.0
    23.  
    24. r = i % 3;
    25. printf("%d\n", r); // 1
    26.  
    27. i++;
    28. printf("%d\n", i); // 11
    29.  
    30. j--;
    31. printf("%d\n", j); // 99
    32.  
    33. return 0;
    34. }
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int a = 10;
    6.  
    7. a += 1; // a = a + 1
    8.  
    9. a -= 1; // a = a - 1
    10.  
    11. a *= 2; // a = a * 2
    12.  
    13. a /= 2; // a = a / 2
    14.  
    15. a %= 3; // a = a % 3
    16.  
    17. return 0;
    18. }
    Type Casting
  • implicit type casting, is performaed by compiler automatically
  • explict type casting, need to specify the cast operator
  • data type order: int < unsigned int <long < unsigned long < float < double
  • Two different types in an arithmetic expersion, the lower rank data type will be converted to the higher rank data type
    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. float a = 3.14;
    6. int b;
    7.  
    8. b = a; // implicit conversion
    9. printf("%d\n", b);
    10.  
    11. b = (int) a; // explicit conversion
    12. printf("%d\n", b);
    13.  
    14. printf("%f\n", a + 1); // integer 1 is converted to float implicitly
    15.  
    16. return 0;
    17. }
    Bitwise Operators

    1. #include <stdio.h>
    2.  
    3. int main()
    4. {
    5. int a = 10;
    6. int b;
    7.  
    8. b = a << 2;
    9.  
    10. printf("%d\n", b); // 40
    11.  
    12. b = a >> 1;
    13.  
    14. printf("%d\n", b); // 5
    15.  
    16. a <<= 2; // a = a << 2
    17. printf("%d\n", a); // 40
    18.  
    19. a >>= 1; // a = a >> 1
    20. printf("%d\n", a); // 20
    21.  
    22. a = 5, b = 9;
    23.  
    24. printf("%d\n", a&b); // 1, 0101 and 1001
    25. printf("%d\n", a|b); // 13, 0101 or 1001
    26. printf("%d\n", a^b); // 12, 0101 exclusive or 1001
    27. printf("%d\n", ~a); // -6, not 0101
    28.  
    29. return 0;
    30. }
    Reference
  • Variable
  • Constant
  • C Programming Language