Arithmetic Operator (Toán Tử Số Học) Là Gì Trong C/C++?

bài trước chúng ta đã được giới thiệu sơ lược về các loại toán tử (operator) trong C. Ở bài viết hôm nay, ta sẽ tìm hiểu sâu hơn về toán tử số học (Arithmetic Operator).

Tóm Tắt Các Loại Operator Trong C

Trong C, toán tử có thể chia thành:
  • Arithmetic Operators - Toán Tử Số Học (+, -, *, /, %, post-increment, pre-increment, post-decrement, pre-decrement)
  • Relational Operators - Toán Tử Quan Hệ (==, !=, >, <, >= & <=)
  • Logical Operators - Toán Tử Logic (&&, || and !)
  • Bitwise Operators - Toán tử Bitwise (&, |, ^, ~, >> and <<)
  • Assignment Operators - Toán Tử Gán (=, +=, -=, *=, etc)
  • Các toán tử khác (conditional, comma, sizeof, address, redirecton)

Toán Tử Số Học (Arithmetic Operators) là gì?

Được sử dụng để thực hiện các phép toán số học / toán học trên toán hạng. Các toán tử nhị phân thuộc loại này là:
  • Cộng: Toán tử '+' cộng hai toán hạng. Ví dụ: x + y.
  • Phép trừ: Toán tử '-' trừ hai toán hạng. Ví dụ: x-y.
  • Phép nhân: Toán tử '*' nhân hai toán hạng. Ví dụ: x * y.
  • Chia: Toán tử '/' chia toán hạng thứ nhất cho toán hạng thứ hai. Ví dụ: x / y.
  • Mô-đun: Toán tử '%' trả số dư của phép chia toán hạng thứ nhất cho toán hạng thứ hai. Ví dụ: x/y.
// C program to demonstrate working of binary arithmetic operators  #include <stdio.h>    int main()  {  int a = 10, b = 4, res;    // printing a and b  printf("a is %d and b is %d\n", a, b);    res = a + b; // addition  printf("a+b is %d\n", res);    res = a - b; // subtraction  printf("a-b is %d\n", res);    res = a * b; // multiplication  printf("a*b is %d\n", res);    res = a / b; // division  printf("a/b is %d\n", res);    res = a % b; // modulus  printf("a%b is %d\n", res);    return 0;  } 
Kết quả:
a is 10 and b is 4
a+b is 14
a-b is 6
a*b is 40
a/b is 2
a%b is 2
Về toán tử số học đơn phân, ta sẽ có:
Increment - Gia tăng: Toán tử '++' được sử dụng để tăng giá trị của một số nguyên. Khi được đặt trước tên biến (còn được gọi là pre-increment operator - toán tử tăng trước), giá trị của nó được tăng ngay lập tức. Ví dụ: ++x.
Đồng thời, khi được đặt sau tên biến (còn được gọi là post-increment operator - toán tử tăng sau), giá trị của nó được bảo toàn tạm thời cho đến khi thực thi câu lệnh này, và sẽ được cập nhật trước khi thực thi câu lệnh tiếp theo. Ví dụ: x ++.
Decrement - Giảm: Toán tử '- -' được sử dụng để giảm giá trị của một số nguyên. Khi được đặt trước tên biến (còn được gọi là pre-decrement operator - toán tử giảm trước), giá trị của nó bị giảm ngay lập tức. Ví dụ: - - x.
Mặt khác, khi được đặt sau tên biến (còn được gọi là post-decrement operator - toán tử giảm sau), giá trị của nó được bảo toàn tạm thời cho đến khi thực thi câu lệnh này, và sẽ được cập nhật trước khi thực thi câu lệnh tiếp theo. Ví dụ: x - -.
// C program to demonstrate working of Unary arithmetic operators  #include <stdio.h>    int main()  {  int a = 10, b = 4, res;    // post-increment example:  // res is assigned 10 only, a is not updated yet  res = a++;  printf("a is %d and res is %d\n", a, res); // a becomes 11 now    // post-decrement example:  // res is assigned 11 only, a is not updated yet  res = a--;  printf("a is %d and res is %d\n", a, res); // a becomes 10 now    // pre-increment example:  // res is assigned 11 now since a is updated here itself  res = ++a;  // a and res have same values = 11  printf("a is %d and res is %d\n", a, res);    // pre-decrement example:  // res is assigned 10 only since a is updated here itself  res = --a;  // a and res have same values = 10  printf("a is %d and res is %d\n", a, res);    return 0;  } 
Kết quả:
a is 11 and res is 10
a is 10 and res is 11
a is 11 and res is 11
a is 10 and res is 10
Như vậy, chúng ta đã cùng tìm hiểu toán tử số học (Arithmetic Operator) là gì? Bạn hãy đọc thêm về toán tử bitwise nhé!

Tham khảo: geeksforgeeks.org

Nhận xét