Operators & Expressions
Operator is a symbol used for preparing an equation or expression.
Most of the equation/expression include two things namely operands and operators.
For instance,
a+b
where
a and b are operands,
+ is an operator
There are 3 category of operators,
1. Unary
2. Binary
3. Ternary
Unary Operators:- This operator takes only one operand. Minus is a unary opeartor. Ex: -5, -a
Binary Operators:- This operator takes two operands like +,*,/,% etc. Ex: a+b
Ternary Operators:- This operator takes three operands. In C, a conditional operator (?:) is a ternary operator. Ex: a>b ? 1 : 0
Different types of Operators:-
Arithmetic Operators : +,-,*,/ and % (Moduls division used for getting remainder of division)
Relational Operators: <, <=, >, >=, ==, !=
These operators are used to make comparisons. After comparison they return either true or false.
Logical Operators:
These are used join more than one relational expression.
&& Logical AND
|| Logical OR
! Logical NOT
Assignment operator (=):
a single equal to sign is called as assignment operator used for assigning a value to the variable.
For example,
a=10;
a value 10 is assigned to the variable a.
Increment and Decrement Opeartors
++ is called as increment operator and
-- is called as decrement operator
++ and -- operators are used to increase or decrease the value of the variable by 1. For example,
a=10;
if we write,
a++;
Value of variable a is increased by 1 and now the value of a is 11.
It is equivalent to statements,
a=a+1;
or
a+=1;
In the same way Decrement operator works excepts it decreases value by 1.
Pre & Post - Increment and Decrement Operators
If we write ++ before the variable name it is called as Pre Increment and if we write after the variable name then it is called as Post Decrement like,
++a Pre Increment
a++ Post Increment
Same thing applies to Decrement operator.
For example,
let a=10;
b=20;
c=a+b+(++a)+(b--)
Operator is a symbol used for preparing an equation or expression.
Most of the equation/expression include two things namely operands and operators.
For instance,
a+b
where
a and b are operands,
+ is an operator
There are 3 category of operators,
1. Unary
2. Binary
3. Ternary
Unary Operators:- This operator takes only one operand. Minus is a unary opeartor. Ex: -5, -a
Binary Operators:- This operator takes two operands like +,*,/,% etc. Ex: a+b
Ternary Operators:- This operator takes three operands. In C, a conditional operator (?:) is a ternary operator. Ex: a>b ? 1 : 0
Different types of Operators:-
Arithmetic Operators : +,-,*,/ and % (Moduls division used for getting remainder of division)
Relational Operators: <, <=, >, >=, ==, !=
These operators are used to make comparisons. After comparison they return either true or false.
Logical Operators:
These are used join more than one relational expression.
&& Logical AND
|| Logical OR
! Logical NOT
Assignment operator (=):
a single equal to sign is called as assignment operator used for assigning a value to the variable.
For example,
a=10;
a value 10 is assigned to the variable a.
Increment and Decrement Opeartors
++ is called as increment operator and
-- is called as decrement operator
++ and -- operators are used to increase or decrease the value of the variable by 1. For example,
a=10;
if we write,
a++;
Value of variable a is increased by 1 and now the value of a is 11.
It is equivalent to statements,
a=a+1;
or
a+=1;
In the same way Decrement operator works excepts it decreases value by 1.
Pre & Post - Increment and Decrement Operators
If we write ++ before the variable name it is called as Pre Increment and if we write after the variable name then it is called as Post Decrement like,
++a Pre Increment
a++ Post Increment
Same thing applies to Decrement operator.
For example,
let a=10;
b=20;
c=a+b+(++a)+(b--)
In case of Pre
Increment/Decrement first increment or decrement of value of variable
will be done then the incremented or decremented value will be
substituted into the expression whereas incase of Post
Increment/Decrement, as the name itself says that increment/decrement
will be done after the substitution of present value into expression and
in the next occurrence the incremented/decremented value will be
substituted.
So can you tell me what is the value of a and b after the above equation.
sizeof and comma operator:
sizeof operator is used to find out the number of bytes required to store a particular value.
Syntax:
sizeof(type); or sizeof(value);
Eg.
sizeof(int);
sizeof(4.5);
comma operator is used to write the related expression in a single line like,
a=10;
b=20;
c=a+b;
using comma operator
c=(a=10,b=20,a+b);
Data type conversion:
There are two methods for converting a data from one type to another type.
Automatic Data Type Conversion:
While processing an expression C will see to that operands of an expression must be of same type. If they are of different type then it will automatically convert low priority data type value to high priority data type and then evaluates the expression.
For ex: int a,c; float b;
c=a/b;
In the above case a is of type integer and b is of type floating point. Both are of different type now the lower priority type value "a" is converted to higher priority type float and then division is performed.
Type casting :
In this method programmer converts a value to a particular type.
int a=(float) 7.5;
sizeof and comma operator:
sizeof operator is used to find out the number of bytes required to store a particular value.
Syntax:
sizeof(type); or sizeof(value);
Eg.
sizeof(int);
sizeof(4.5);
comma operator is used to write the related expression in a single line like,
a=10;
b=20;
c=a+b;
using comma operator
c=(a=10,b=20,a+b);
Data type conversion:
There are two methods for converting a data from one type to another type.
Automatic Data Type Conversion:
While processing an expression C will see to that operands of an expression must be of same type. If they are of different type then it will automatically convert low priority data type value to high priority data type and then evaluates the expression.
For ex: int a,c; float b;
c=a/b;
In the above case a is of type integer and b is of type floating point. Both are of different type now the lower priority type value "a" is converted to higher priority type float and then division is performed.
Type casting :
In this method programmer converts a value to a particular type.
int a=(float) 7.5;
Comments
Post a Comment