Arithmetic Operators in C

Arithmetic Operators in C

Arithmetic Operators in C

This section introduces the arithmetic operators in the C language, including addition, subtraction, multiplication, division, and modulus. It also explains the concept of operands and binary operators.

Arithmetic Operators

  • Addition operator is used to perform addition between two numbers.
  • Subtraction operator is used to perform subtraction between two numbers.
  • Multiplication operator is used to perform multiplication between two numbers.
  • Division operator is used to perform division between two numbers.
  • Modulus operator calculates the remainder of a division operation.

Example of Division and Modulus

  • An example code snippet demonstrates how to use scanf function to accept user input for two numbers and calculate their quotient and remainder using division and modulus operators.
  • The result of dividing 9 by 3 is 3 (quotient) with a remainder of 0.

Precedence and Associativity

  • Precedence determines the order in which operations are performed within an expression. Multiplication, division, and modulus have the same precedence, while addition and subtraction have the same precedence as well.
  • Associativity comes into play when multiple operators have the same precedence. For example, addition and subtraction associate from left to right.

Example Expression Evaluation

  • An example code snippet evaluates an expression involving multiplication and division with variables a, b, c assigned values 2, 3, 4 respectively. The result is printed using printf function.
  • The expression a * b / c is evaluated as 2 * 3 / 4, resulting in the value of 1.5 which is truncated to 1 when printed as an integer.

Summary and Study Notes

Arithmetic Operators in C

  • Addition, subtraction, multiplication, division, and modulus are the arithmetic operators in C.
  • Addition operator performs addition between two numbers.
  • Subtraction operator performs subtraction between two numbers.
  • Multiplication operator performs multiplication between two numbers.
  • Division operator performs division between two numbers.
  • Modulus operator calculates the remainder of a division operation.

Precedence and Associativity

  • Precedence determines the order in which operations are performed within an expression.
  • Multiplication, division, and modulus have the same precedence.
  • Addition and subtraction have the same precedence.
  • Associativity comes into play when multiple operators have the same precedence.

Example Expression Evaluation

  • An example code snippet demonstrates how to evaluate an expression involving multiplication and division using variables in C.
  • The result of the expression is printed using printf function.

New Section

In this section, the speaker explains the usage of the printf function and evaluates an expression step by step.

Understanding the printf Function

  • The printf function is used to print output on the screen.
  • When using the % symbol in printf, it needs to be written as %% to avoid confusion with format specifiers.
  • The speaker demonstrates how to print a single % symbol using two % symbols in the code.

Evaluating an Expression

  • The expression being evaluated is: a + (3 * 5) - (4 % 2).
  • Multiplication and modulus have higher precedence than addition and subtraction.
  • After replacing the values, the expression becomes: a + 15 - 0.
  • Addition and subtraction have equal precedence, so they are performed from left to right.
  • Therefore, 2 + 15 is equal to 17, resulting in a final output of 17 for this expression.

New Section

In this section, the speaker continues explaining how to evaluate an expression step by step.

Understanding Precedence and Associativity

  • Multiplication and modulus have higher precedence than addition and subtraction.
  • This means that multiplication and modulus will be performed first before addition and subtraction.
  • Associativity determines which operation is performed first when multiple operations have the same precedence.
  • In this case, associativity is from left to right.

Evaluating an Expression (Continued)

  • The expression being evaluated is: a + (3 * 5) - (4 % 2).
  • After replacing the values, the expression becomes: a + 15 - 0.
  • Addition is performed first: 2 + 15 = 17
  • Subtraction is then performed: 17 - 0 = 17
  • Therefore, the final output of this expression is 17.

New Section

In this section, the speaker concludes the evaluation of the expression and provides the outputs for different expressions.

Evaluating an Expression (Continued)

  • The expression being evaluated is: a + (3 * 5) - (4 % 2).
  • After replacing the values, the expression becomes: 2 + 15 - 0.
  • Addition is performed first: 2 + 15 = 17
  • Subtraction is then performed: 17 - 0 = 17
  • Therefore, the final output of this expression is 17.

Outputs of Different Expressions

  • The output of the first expression is 1.
  • The output of the second expression is also 1.
  • The output of the last expression is 17.

The % symbol in printf should be written as %% to avoid confusion with format specifiers.

Video description

C Programming & Data Structures: Arithmetic Operators in C Topics discussed: 1. Arithmetic operators in C language. 2. Addition operator. 3. Subtraction operator. 4. Multiplication operator. 5. Division operator. 6. Modulus operator. 7. Precedence and Associativity of arithmetic operators. C Programming Lectures: https://goo.gl/7Eh2SS Follow Neso Academy on Instagram: @nesoacademy(https://bit.ly/2XP63OE) Follow me on Instagram: @jaspreetedu(https://bit.ly/2YX26E5) Contribute: http://www.nesoacademy.org/donate Memberships: https://bit.ly/2U7YSPI Books: http://www.nesoacademy.org/recommended-books Website ► http://www.nesoacademy.org/ Forum ► http://forum.nesoacademy.org/ Facebook ► https://goo.gl/Nt0PmB Twitter ► https://twitter.com/nesoacademy Music: Axol x Alex Skrindo - You [NCS Release] #CProgrammingByNeso #CProgramming #ArithmeticOperators