How are integers operated in the C language?

In the C language, the int data type is used to represent integers. Arithmetic operations on int types can be performed using common operators such as addition, subtraction, multiplication, and division. Here are some examples:

  1. Addition:
  2. int a is assigned a value of 10, int b is assigned a value of 5, int result holds the sum of a and b, and this result is printed with the statement “The sum is: %d”.
  3. Subtraction operation:
  4. Declare two variables, ‘a’ and ‘b’, with integer values of 10 and 5 respectively. Subtract ‘b’ from ‘a’ and store the result in a variable called ‘result’. Print the result as “The difference is: ” followed by the value of ‘result’.
  5. Multiplication operation:
  6. Create two variables, a and b, and assign the values 10 and 5 to them respectively. Then, multiply a by b and store the result in a variable named result. Finally, print the product with the message “The product is: %d”.
  7. Division operation:
  8. int divideBy = 10;
    int divideInto = 5;
    int quotient = divideBy / divideInto;
    printf(“The result of the division is: %d”, quotient);

It is important to note that integer division results in truncating to an integer. For example, the result of dividing 10 by 5 is 2, not 2.5.

Furthermore, other operators such as the modulus operator (%) and increment/decrement operators (++ and –) can also be used for integer operations.

bannerAds