Today lets learn about basic arithmetic operations in C programming language.
Page Contents
We shall learn, addition, subtraction, multiplication, division and modular division in this video tutorial. And we’ll also learn how to use pow() method present in math.h library file.
#include < stdio.h > #include < math.h > int main() { int a = 10, b = 2; char m = 'A', n = 'a'; printf("\nAddition of a and b is %d\n", (a+b)); printf("\nSubtraction of a and b is %d\n", (a-b)); printf("\nMultiplication of a and b is %d\n", (a*b)); printf("\nDivision of a and b is %d\n", (a/b)); printf("\nModular Division of a and b is %d\n", (a%b)); printf("\n3 to the power of 2 is %f\n", pow(3,2)); printf("\nASIIC value of A and a is %d and %d\n\n", m, n); return 0; }
Output:
Addition of a and b is 12
Subtraction of a and b is 8
Multiplication of a and b is 20
Division of a and b is 5
Modular Division of a and b is 0
3 to the power of 2 is 9.000000
ASIIC value of A and a is 65 and 97
Basic Arithmetic Operations In C
1st priority: * / %
2nd priority: + –
3rd priority: =
When there is tie between same priority operators then we check the associativity. For example, for * and /, associativity is same. i.e., left to right.
So in the expression: c = a * b + a / b;
a * b gets executed first.
When you try to apply Arithmetic Operation on characters(alphabets, character digits, special characters) its correspondent ASCII value gets operated on. ASCII value of A is 65 and ASCII value of a is 97. This is the reason why capital A and small letter a are treated differently in C programming language. Under the hood, they have different ASCII values.
Note 1: All the variables and constants are called operands. All the arithmetic symbols(+, -, /, *, %) are called operators. = is called assignment operator.
Note 2: Only one variable is allowed on the left hand side of the equation or expression.
Ex:
c = a + b is valid.
a + b = c is invalid.
Note 3: You can’t store a decimal value in a integer variable. So if the result of evaluation of an expression has decimal value and you’re assigning it to an integer variable, then only the integer part gets stored. Decimal part will be discarded. So be careful while choosing the data type of variables while performing arithmetic operation.
Note 4: We need to always, explicitly mention the arithematic operators.
Ex:
c = (a + b)(a – b) throws error.
c = (a + b) * (a – b); is the correct way in C programming.
Note 5: An integer value divided by an integer value gives back an integer value. A float value divided by a float value gives back a floating point value.
Note 6: Modular division gives reminder of division.
Ex:
c = 10 / 2; gives c = 5;
c = 10 % 2; give c = 0;
Also,
c = -3 % 2; gives c = -1
c = 3 % -2; gives c = 1
Modular division doesn’t take float values as its operands.
Ex: c = 3.0 % 2.0; gives error
Note 7: math.h library file has other mathematical operations related methods like: pow(), sin(), cos(), tan(), abs(), sqrt() etc.
We’ll look more arithmetic operators as and when we encounter them in programs.
For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert