Docsity

C Programming Operators: Arithmetic, Relational, and Logical, Schemes and Mind Maps of Computer Fundamentals

Toggle sidebar
bg1
Operator:
An operator is a symbol that operates on a value or a variable.
type of operators.
Arithmetic Operators
Increment and Decrement Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values
Operator
Meaning of Operator
+
addition or unary plus
-
subtraction or unary minus
*
multiplication
/
division
%
remainder after division (modulo division)
pf3
pf4
pf5

Partial preview of the text

Download C Programming Operators: Arithmetic, Relational, and Logical and more Schemes and Mind Maps Computer Fundamentals in PDF only on Docsity!

Operator: An operator is a symbol that operates on a value or a variable. type of operators. Arithmetic Operators Increment and Decrement Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators Misc Operators C Arithmetic Operators An arithmetic operator performs mathematical operations such as addition, subtraction, multiplication, division etc on numerical values Operator Meaning of Operator

  • addition or unary plus
  • subtraction or unary minus
  • multiplication / division % remainder after division (modulo division)

C Increment and Decrement Operators C programming has two operators increment ++ and decrement -- to change the value of an operand by 1. Example: #include <stdio.h> int main() { int a = 10, b = 100; float c = 10.5, d = 100.5; printf("++a = %d \n", ++a); printf("--b = %d \n", --b); printf("++c = %f \n", ++c); printf("--d = %f \n", --d); return 0; }

C Logical Operators An expression containing logical operator returns either 0 or 1 depending upon whether expression results true or false. Logical operators are commonly used in decision making. Operator Meaning Example && Logical AND. True only if all operands are true If c = 5 and d = 2 then, expression ((c==5) && (d>5)) equals to 0. || Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression ((c==5) || (d>5)) equals to 1. ! Logical NOT. True only if the operand is 0 If c = 5 then, expression !(c==5) equals to 0. Example: #include <stdio.h> int main() { int a = 5, b = 5, c = 10, result; result = (a == b) && (c > b); printf("(a == b) && (c > b) is %d \n", result); result = (a == b) && (c < b); printf("(a == b) && (c < b) is %d \n", result); result = (a == b) || (c < b); printf("(a == b) || (c < b) is %d \n", result); result = (a != b) || (c < b); printf("(a != b) || (c < b) is %d \n", result); result = !(a != b); printf("!(a != b) is %d \n", result); result = !(a == b); printf("!(a == b) is %d \n", result); return 0; }

Explanation of logical operator program (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true). (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false). (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true). (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false). !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true). !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).

C Bitwise Operators During computation, mathematical operations like: addition, subtraction, multiplication, division, etc are converted to bit-level which makes processing faster and saves power. Bitwise operators are used in C programming to perform bit-level operations. Operators Meaning of operators & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR ~ Bitwise complement << Shift left

Shift right Other Operators Comma Operator Comma operators are used to link related expressions together. For example: int a, c = 5, d; The sizeof operator The sizeof is a unary operator that returns the size of data (constants, variables, array, structure, etc).