## C Programming Arithmetic Operators Tutorial

Table of Contents

Learn C Programming Arithmetic Operators MCQ Questions and Answers on Basics to attend job placement exams, interview questions, college viva and Lab Tests

C language comes with Arithmetic Operators like Plus, Minus, Multiplication, Division and Modulo Division. One more operator ‘=’, also called Assignment operator is also present.

C Programming Arithmetic Operators are used to work with integers and real numbers.

Study **C basics and keywords** before this tutorial.

## Arithmetic operators in c

SNO | Operator | Description |

1 | + | Addition Operator |

2 | – | Subtraction Operator |

3 | * | Multiplication Operator |

4 | / | Multiplication Operator |

5 | % | Modulo Division Operator |

### Arithmetic Operators in C

**Example 1:**

int main() { int a=5, b=10; int c; // = is Assignment operator c = a + b; printf("%d ", c); c = a - b; printf("%d ", c); c = a*b; printf("%d ", c); c = b/a; printf("%d ",c); c = a%b; //modulo division or remainder operator printf("%d", c); return 0; } //output: 15 -5 50 2 5

Assignment Operator allows only right side value to be assigned to the left side variable. In the above program, left side variable ‘C’ gets the value of right side operation like addition, subtraction etc.

**Note**: Modulo division operator works only with whole numbers like integers.

**Note**: Arithmetic operators can also be applied on char variables as characters are converted to their respective ASCII codes before arithmetic operation.

### Integer and Float Conversions

When you are working with integers and float (Real) numbers, some number conversions are applied.

- Result of int and int is integer only. Use
**%d**format specifier. Use an**int**variable to hold data. - Result of float and float is float only. Use
**%f**format specifier. Use a**float**variable to hold data. - Result of int and float is float only. Use
**%f**format specifier. Use a**float**variable to hold data. - If you use
**%d**instead of**%f,**precision data will be lost. - If you are working with
**long, double and long double data types**, result of an arithmetic operation contains**higher data type value**. For example, arithmetic operation with short and int is always int.

Arithmetic operators in c Example 1:

int main() { int a=5, b=10; float c = 12.5f, d = 10.5f; //int + int printf("%d, ", a+b); //float + float printf("%f, ", c+d); //int + float printf("%f, ", a + c); //int + float printf("%d", a + c); //%d truncates extra data i.e precision return 0; } //output: 15, 23, 17.500000, 17

## Arithmetic Operators

**Example 2**:

SNO | Arithmetic Operation | Result |

1. | 9/2 | 4 |

2. | 9/2.0 | 4.5 |

3. | 9.0/2 | 4.5 |

4. | 9.0/2.0 | 4.5 |

5. | 2/9 | 0 |

6. | 2/9.0 | 0.22 |

7. | 2.0/9 | 0.22 |

8. | 2.0/9.0 | 0.22 |

**Note**: To avoid losing of precision data with division operator, **multiply **the numerator or denominator with **1.0f or 1.0** double value. Use parantheses ( ) operator to group all numerator or denominator expressions.

int main() { int a=10, b=4; float c = (1.0f * a) / b; //multiply with 1.0f or 1.0 printf("%f", c); return 0; } //output: 2.500000

## C Operator Precedence or Priority or Hierarchy

Each operator in C has a precedence or priority or hierarchy. Operator precedence is useful to evaluate arithmetic expressions with operators of different priority. Operator priority decides which operator and operands should be evaluated first.

**Note**: Constants on the left and right side of operator are called **Operands**.

**Example 1**:

int main() { int a=2, b=3, c=8; int d=0; d = a + b * c; printf("%d", d); return 9; } //output: // 2 + (3*8) // 2 + 24 // 26 is the output //'*' operator has higher priority // wrong = (a+b)*c

### Arithmetic operators in c Precedence Chart

Priority | Operator | Description |

1st | *, /, % | Multiplication, Division, Modulo Division |

2nd | +, – | Addition, Subtraction |

3rd | = | Assignment |

**Example 2**:

int main() { float abc = 5 + 10 / 2 * 5 - 2 % 5; printf("Result=%f", abc); return 0; } //Evaluation steps //10 / 2 * 5 left to right processing //5 + 25 - 2 left to right processing // START // 5 + (10 / 2) * 5 - 2 % 5 // 5 + 5 * 5 - 2 % 5 // 5 + 25 - 2 % 5 // 5 + 25 - 2 // 30 - 2 // 28 //END

## C Operator Associativity

C operator associativity deals with the problem of associating an operand to either left operator or right operator. When two equal priority operators are encountered in an expression, this Operator associativity is used to solve deadlock. Usually arithmetic operators follow Left to Right Associativity. Equals to ‘=’ operator follows Right to Left Associativity.

**Example 1**:

int d = 3 + 4 + 5;

Now as per Left to Associativity of arithmetic operators, 4 belongs to the left side + or first + operator. So the expression becomes (3+4) + 5. Next, the result of expression is added to 5 like (7)+5.

Now as per Right to Left Associativity of Equals to Operator, right side value is assigned to left side variable. So the variable d now holds a value of 12.

**Example 2**:

float fd = 3 + 12 / 5;

In the code above, there are two operators + and /. As Division / operator has higher priority, operand 12 is associated with /. instead of + on the left side. So 12/5 evaluates to 2. fd = 3 + 2 = 5 now.

learn C Programming Syntax Basics in this multipart tutorial which are useful for students in the last minute before exam. C Programming Language was invented by Dennis Ritchie in Bell Laboratories. Now Nokia owns Bell Laboratories and experiments are still going on.

## Arithmetic operators in c Syntax Basics Part 1

We shall learn about basic structure of a C Program and predefined Keywords in this C Programming Syntax Basics Tutorial Part 1. You can **set up a Turbo C Compiler on your Windows Machine** easily.

## Structure of a C Program

Every C program contains a function or method called **main(). **All functions end with **Round Brackets** or **Paranthesis** **( )**. Inside Paratheses, arguments can be received or passed. Code of a Function is surrounded by **Curly Brackets** or **Braces { }**.

#include <stdio.h> void main() { /*This is a multi-line comment. It is not compiled or checked for Syntax*/ printf("Hello C.."); }

### Output

Hello C..

### Observations

**VOID**is a return type. VOID means nothing is being returned.**main() { }**is a compulsory function in any C Program.- void and main should be typed in
**lowercase**completely. **/**symbol is called Forward Slash or simply Slash.*****symbol is called**STAR**.**/* */**is a multiline comment. Comments are useful for analysing code logic flow in very big projects. Comments make others easily understand your program or project.**printf**() is a function which is passing one argument “Helllo C”.- “Hello C” is a string literal here.
- Code statements or lines end with
**Semicolon ;** - Putting a return type before a function name is not mandatory in a C Compiler. If you use a C++ Compiler to compile c programs, you must specify a return type.
**#include**includes an already written header file**stdio.h**which contains code for**printf**function. Without predefined functions, life becomes difficult to write everything every time on our own. Also the code without including files becomes clumsy to maintain and understand.

## Keywords in C Language (Arithmetic operators in c)

There are a total of 32 keywords in C Language which can not be used for the names of variables and functions. Find the C Programming Keyword list below. Students need to remember these keywords just like that to attempt questions asked in their exams.

Keyword | Meaning |

auto | Defines local life time for a variable |

break | Breaks a current loop in general |

case | Defines branch control point |

char | Basic data type, character literal |

const | Defines a Constant, unmodified variable |

continue | control goes to the loop beginning |

default | control point used in switch construct in general |

do | do while loop |

double | Floating point data type bigger than float type |

else | Usually followed by if construct. If conditon fails, branch to else block |

enum | Used to define a group of int constants like array |

extern | Used to define a variable or function with type and the definition may exist some where |

float | Floating point data type |

for | For Loop |

goto | Transfers execution control to defined Label |

if | A conditional statement |

int | Basic integer data type |

long | Integer data type bigger than int |

register | Tells to store the variable in RAM register |

return | ends execution immediately |

short | Type Modifier |

signed | Type Modifier |

sizeof | used to get the size of a variable. Eg. sizeof(integerType) |

static | used to create a variable with broad scope |

struct | Used to define a custom data type kind of thing |

switch | switch branch control |

typedef | used to create new type |

union | used in grouping of variables of same type |

unsigned | Modifiere used to increase positive max value |

void | empty data type or return type |

volatile | Used to create a variable with a value changed by any external process |

while | while loop with a condition |

##### [WpProQuiz 24]

## Arithmetic Operators in C MCQ

#### 1) Choose a correct statement.

int a = 12 + 3 * 5 / 4 - 10

A) 12, 3, 5, 4 and 10 are Operators. +, -, * and / are Operands. = is an increment operator.

B) 12, 3, 5, 4 and 10 are Operands. +, -, * and / are Operators. = is decrement operator.

C) 12, 3, 5, 4 and 10 are Operands. +, -, * and / are Operators. = is an assignment operator.

D) 12, 3, 5, 4 and 10 are Operands. +, -, * and / are Logical Operators. = is an assignment operator.

**Answer [=] C**

### 2) Operator % in C Language is called.?

A) Percentage Operator

B) Quotient Operator

C) Modulus

D) Division

**Answer [=] C**

Explanation:

Operator % is called Modulus or Modular or Modulo Division operator in C. It gives the reminder of the division.

int a = 11%4;

Now a holds only 3 which is the reminder.

### 3) Output of an arithmetic expression with integers and real numbers is ___ by default.?

A) Integer

B) Real number

C) Depends on the numbers used in the expression.

D) None of the above

**Answer [=] B**

Explanation:

Any arithmetic operation with both integers and real numbers yield output as Real number only.

5 + 10.56 = 15.560000 which is a real number.

5 + 10.0 = 15.000000 is also a real number.

#### 4) Choose a right statement.

int a = 10 + 4.867;

A) a = 10

B) a = 14.867

C) a = 14

D) compiler error.

**Answer [=] C**

Explanation:

a is an int variable. So 10+4.867 = 14.867 is truncated to 14 and assigned to a.

#### 5) Choose a right statement.

int a = 3.5 + 4.5;

A) a = 0

B) a = 7

C) a = 8

D) a = 8.0

**Answer [=] C**

Explanation:

3.5 + 4.5 = 8.0 is a real number. So it is converted to downgraded to int value. So a = 8.

#### 6) Choose a right statement.

float var = 3.5 + 4.5;

A) var = 8.0

B) var = 8

C) var = 7

D) var = 0.0

**Answer [=] A**

Explanation:

A float variable can hold a real number.

#### 7) Choose right statement.

int main() { float c = 3.5 + 4.5; printf("%f", c); return 0; }

A) 8.0

B) 8.000000

C) 8

D) 7

**Answer [=] B**

Explanation:

Float can print precision up to 6 digits. So 6 zeros will be shown if there are no digits after decimal point.

#### 8) Choose a right statement.

int main() { float c = 3.5 + 4.5; printf("%d", (int)c); return 0; }

A) 8.0

B) 8.000000

C) 7

D) 8

**Answer [=] D**

Explanation:

You are printing a float variable by type casting to int. So integer is printed.

int c = 3.5 + 4.5 also holds and prints 8.

#### 9) Choose a right statement.

int a = 5/2; int b = 5.0/2; int c = 5 / 2.0; int d = 5.0/2.0;

A) a = 2, b = 2, c = 2, d= 2

B) a = 2, b = 2.0, c = 2, d= 2.0

C) a = 2, b = 2.5, c = 2.5, d= 2.5

D) a = 2.5, b = 2.5, c = 2.5, d= 2.5

**Answer [=] A**

Explanation:

Irrespective of numbers after decimal point, an int variable holds only integer value i.e 2.

#### 10) Choose a right statement.

float a = 5/2; float b = 5/2.0; float c = 5.0/2; float d = 5.0/2.0;

A) a=2.5, b=2.5, c=2.5, d=2.5

B) a=2, b=2.5, c=2.5, d=2.5

C) a=2.0, b=2.5, c=2.5, d=2.5

D) a=2.0, b=2.0, c=2.0, d=2.0

**Answer [=] C**

Explanation:

In division, to get the actual real value, you should specify at least one real number.

Variable a holds only 2. But variables b,c and d contain real numbers as either numerator or denominator is a real number.

#### 11) If both numerator and denominator of a division operation in C language are integers, then we get.?

A) Expected algebraic real value

B) Unexpected integer value

C) Compiler error.

D) None of the above

**Answer [=] B**

Explanation:

int a = 5/2 stores only 2.

#### 12) Choose a right statement.

int var = 3.5;

A) a = 3.5

B) a = 3

C) a = 0

D) Compiler error

**Answer [=] B**

Explanation:

a stores only integer value. So, 3.5 is truncated to 3.

#### 13) Choose a right statement.

int main() { int var = 3.5;; printf("%f", var); return 0; }

A) 3.500000

B) 3

C) 3.5

D) 0.000000

**Answer [=] D**

Explanation:

As the variable type is an integer, you have to use %d as a format specifier. If you specify wrong format specifier, you will not get expected output.

#### 14) What is the output of the program.?

int main() { int a = 25%10; printf("%d", a); return 0; }

A) 2.5

B) 2

C) 5

D) Compiler error.

**Answer [=] C**

Explanation:

Modulo division operator returns the reminder of division of 25 by 10. 10×2 + **5** = 25. So reminder is 5.

#### 15) Can you use C Modulo Division operator % with float and int?

A) Only int variables = Okay

B) Only float variables = Okay

C) int or float combination = Okay

D) Numerator int variable, Denominator any variable = Okay

**Answer [=] A**

Explanation:

Modulo Division operator % in C language can be used only with integer variables or constants.

#### 16) What is the output of the C program with Modulo Division operator with – or Negative numbers.?

int main() { int a = -25%-10; int b = -25%10; int c = 25%-10; printf("%d %d %d", a, b, c); return 0; }

A) 5 -5 -5

B) 5 -5 5

C) -5 -5 5

D) 5 5 5

**Answer [=] C**

Explanation:

Sign of a modulo division operation is same as the sign of Numerator. So sign of 25 is taken always.

#### 17) What is the output of the program.?

int main() { float a = 45; printf("%f", a); return 0; }

A) 45

B) 45.0

C) 45.000000

D) 0.000000

**Answer [=] C**

Explanation:

Integer value 45 is promoted to float i.e 45.0 and printed with all 6 decimal numbers.

### 18) What is the priority of operators *, / and % in C language.?

A) * > / > %

B) % > * > /

C) Both % = / , * are same

D) All three operators *, / and % are same.

**Answer [=] D**

Explanation:

Operators Multiplication *, Division / and Modulo Division % are all having the same Priority.

### 19) In C language, which Operator group has more priority between (*, / and %) and (+, -) groups.?

A) Both groups share equal priority.

B) (+, -) > (*, / and %)

C) (+, -) < (*, / and %)

D) None of the above.

**Answer [=] C**

Explanation:

+ and – has same priority. *, / and % has equal priority. But (+, -) has less priority than (*, / and %).

### 20) Associativity of C Operators *, /, %, +, – and = is.?

A) Operators *, / and % have Left to Right Associativity. Operators + and – have Left to Right Associativity. Operator = has Right to Left Associativitiy.

B) Operators *, / and % have Right to Left Associativity. Operators + and – have Left to Right Associativity. Operator = has Right to Left Associativitiy.

C) Operators *, / and % have Right to Left Associativity. Operators + and – have Right to Left Associativity. Operator = has Right to Left Associativitiy.

D) Operators *, / and % have Right to Left Associativity. Operators + and – have Right to Left Associativity. Operator = has Left to Right Associativitiy.

**Answer [=] A**

Explanation:

Operators *, / and % have Left to Right Associativity. Operators + and – have Left to Right Associativity. Operator = has Right to Left Associativitiy.