C Operators (Contd.)
5. Logical Operators
Logical Operators performs Logical Operations. Logical Operators are used to combine two or more expressions. "Logical Operators results in 0 for false and 1 for true".C has 3 Logical Operators :
Let's understand Each Logical Operator with the help of an example :
int a=5, b=6, c=7 ;
Now, let's check the conditions :
AND OPERATION :
if (a==5 && b==7)
This will return false as b holds value 6. So, the first condition is true but Second is not true.
AND Operation is true, when every condition is true, thus this will return False.
OR OPERATION :
AND Operation is true, when every condition is true, thus this will return False.
OR OPERATION :
if(a==5 || b==7)
This will return true, as one of the above condition is true i.e, "a is equals to 5". In OR operation, if any one condition is true then, output will be true.
NOT OPERATION
This will return true, because NOT operator invert the condition from false to true. A NOT operator reverses or inverts any condition. So, the first condition, which was false before will be inverted to true.
NOT OPERATION
if (!(a==5 && b==7))
Program Example:
#include <stdio.h>
// main function starts here
int main(){
int a=5, b=6, c=7;
if (a==5 && b==7)
printf("This line will not be printed, because the above condition is false");
if(a==5 || b==7)
printf("As, one condition is true, thus this line will be printed\n\n");
if (!(a==5 && b==7))
printf("We are using the first condition. It was false before, so NOT operator will invert it to true and this line will be printed.");
}
Click on Run
Above code is self-explanatory, go through the code and understand it’s meaning by yourself. If still faces any problem, tell us in the comments.
6. Conditional Operator
Conditional Operator is a ternary operator, which requires three expressions as operands. This is too used to test a condition as done by the if() structure.It takes the following syntax or format :
Condition ? expression1 : expression2;
First, the condition is evaluated :
(i) If condition is true, then ‘expression1’ will be evaluated and it becomes the value of the result.
(ii) If condition is false, then ‘expression2’ will be evaluated and it becomes the value of the result.
Lets understand this with the help of an example :
int a=5, b=15;
int max;
//max will hold the greater value
//max will hold the greater value
max = a>b ? a : b;
The right side statement first evaluates the condition - (a>b). If this condition is true, then ‘a’ is evaluated and becomes the value of max. If this condition is false, then ‘b’ is evaluated and becomes the value of max.
max = a>b ? a : b;
We know that, the condition is false because “a is not greater than b”, thus ‘b’ will be evaluated and becomes the value of ‘max’.
Thus, ‘max’ holds value of variable ‘b’ i.e, 15.
printf(“The greater number is %d”,max); //outputs 15
#include <stdio.h>
// main function starts here
int main(){
int a=5, b=15;
int max;
//max will hold the greater value
max = a>b ? a : b;
printf(“The greater number is %d”,max);
//outputs 15
}
7. sizeof Operator
sizeof is a unary operator, uses only one operand. This operator gives the size of its operand in terms of bytes. Operand can be a variable, constant or any data type (int, char, float or double).For Example -
printf(“Size of int = %d”,sizeof(int));
//outputs 2 or 4
The above code will print the size of int data type by using sizeof() operator.
Similarly,
printf(“Size of char = %d”,sizeof(char));
//outputs 1
The size of data types are given below :
Note : The size of data type depends upon the processor used by the compiler, therefore 'int' has sizes of "2 or 4" bytes.
Note : The size of data type depends upon the processor used by the compiler, therefore 'int' has sizes of "2 or 4" bytes.
Program Example :
#include <stdio.h>
// main function starts here
int main(){
printf(“Size of int = %d”,sizeof(int));
printf(“Size of char = %d”,sizeof(char));
printf("“Size of float = %d”,sizeof(float));
printf("“Size of double = %d”,sizeof(double));
printf("“Size of integer constant = %d”,sizeof(5));
}
8. Bitwise Operators
Bitwise Operators are used to perform the operation on bits of a given number. Thus, C language provides the support of manipulation of data at bit level. Bitwise Operators operate on integers only. The bitwise operators are as -
The Bitwise Operators are discussed later in this website.
Post a Comment