C Language Full course
5.Operators in C Programming l C Tutorials for beginers #5
5.Operators in C Programming l C Tutorials for beginers #5
Operator in C Programming
ans: An operator is nothing but a symbol which tells the compiler to do specific mathematical operation.
Types Of Operator in C Programming
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Conditional Operators
- Assignment Operators
- Special Operators
Arithmetic Operator
Operator | Description | Example |
---|---|---|
+ | Adds two operands. | A + B = 50 |
− | Subtracts second operand from the first. | A − B = -30 |
* | Multiplies both operands. | A * B = 600 |
/ | Divides numerator by de-numerator. | B / A = 25 |
% | Modulus Operator and remainder of after an integer division. | B % A = 0 |
++ | Increment operator increases the integer value by one. | A++ = 5 |
-- | Decrement operator decreases the integer value by one. | A-- = 7 |
Relational Operator
Operator | Description | Example |
---|---|---|
== | Checks if the values of two operands are equal or not. If yes, then the condition becomes true. | (A == B) is not true. |
!= | Checks if the values of two operands are equal or not. If the values are not equal, then the condition becomes true. | (A != B) is true. |
> | Checks if the value of left operand is greater than the value of right operand. If yes, then the condition becomes true. | (A > B) is not true. |
< | Checks if the value of left operand is less than the value of right operand. If yes, then the condition becomes true. | (A < B) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand. If yes, then the condition becomes true. | (A >= B) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand. If yes, then the condition becomes true. | (A <= B) is true. |
Logical Operator
Operator | Description | Example |
---|---|---|
&& | Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. | (A && B) is false. |
|| | Called Logical OR Operator. If any of the two operands is non-zero, then the condition becomes true. | (A || B) is true. |
! | Called Logical NOT Operator. It is used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make it false. | !(A && B) is true. |
Bitwise Operator
A | B | A & B | A | B | A ^ B |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
Operator | Description | Example |
---|---|---|
& | Binary AND Operator copies a bit to the result if it exists in both operands. | (A & B) = 12, i.e., 0000 1100 |
| | Binary OR Operator copies a bit if it exists in either operand. | (A | B) = 61, i.e., 0011 1101 |
^ | Binary XOR Operator copies the bit if it is set in one operand but not both. | (A ^ B) = 49, i.e., 0011 0001 |
~ | Binary Ones Complement Operator is unary and has the effect of 'flipping' bits. | (~A ) = -61, i.e,. 1100 0011 in 2's complement form. |
<< | Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. | A << 2 = 240 i.e., 1111 0000 |
>> | Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. | A >> 2 = 15 i.e., 0000 1111 |
Conditional Operator
it work same as if else statement we will read if else statement in our next chapter.
syntax:
ConditionalExpression ? expression1 :expression2
The conditional operator works as follows:
- The first expression conditionalExpression is evaluated at first. This expression evaluates to 1 if it's and evaluates to 0 if it's false.
- If conditionalExpression is true, expression1 is evaluated.
- If conditionalExpression is false, expression2 is evaluated.
example:
#include <stdio.h> main() { int a , b; a = 10; printf( "Value of b is %d\n",(a==1)?20:30); printf( "Value of b is %d\n",(a==10)?20:30); }
Óutput of the above program is:
Value of b is 30 Value of b is 20 |
Assignment Operator in C
ans: This operator is used to assign a value to a variable.
Operator | Example | Same as |
---|---|---|
= | a = b | a = b |
+= | a += b | a = a+b |
-= | a -= b | a = a-b |
*= | a *= b | a = a*b |
/= | a /= b | a = a/b |
%= | a %= b | a = a%b |
example of Assignment Operator:
#include <stdio.h>
void main()
{
int a = 3, c;
c = a;
printf("c = %d \n", c);
c += a; // c = c+a
printf("c = %d \n", c);
c -= a; // c = c-a
printf("c = %d \n", c);
c *= a; // c = c*a
printf("c = %d \n", c);
c /= a; // c = c/a
printf("c = %d \n", c);
c %= a; // c = c%a
printf("c = %d \n", c);
}
Output:
c = 3 c = 6 c = 3 c = 9 c = 3 c = 0
Special Operator in C
Operator | Description | Example |
---|---|---|
sizeof() | Returns the size of a variable. | sizeof(a), where a is integer, will return 4. |
& | Returns the address of a variable. | &a; returns the actual address of the variable. |
* | Pointer to a variable. | *a; |
Operator precedence in C Program
Category | Operator | Associativity |
---|---|---|
Postfix | () [] -> . ++ - - | Left to right |
Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |
Multiplicative | * / % | Left to right |
Additive | + - | Left to right |
Shift | << >> | Left to right |
Relational | < <= > >= | Left to right |
Equality | == != | Left to right |
Bitwise AND | & | Left to right |
Bitwise XOR | ^ | Left to right |
Bitwise OR | | | Left to right |
Logical AND | && | Left to right |
Logical OR | || | Left to right |
Conditional | ?: | Right to left |
Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |
Comma | , | Left to right |
* C programs operator:-
1. # include<stdio.h>
# include <math.h>
int main(){
int a = 4;
int b = 8;
printf("The value of a + b is: %d\n", a + b);
printf("The value of a - b is: %d\n", a - b);
printf("The value of a * b is: %d\n", a * b);
printf("The value of a / b is: %d\n", a / b);
int z;
z = b * a; // legal
//b * a = z; // Illegal
printf("The value of z is: %d\n", z);
printf("5 when divided by 2 leaves a remainder of %d\n", 5%2);
printf("-5 when divided by 2 leaves a remainder of %d\n", -5%2);
printf("5 when divided by -2 leaves a remainder of %d\n", 5%-2);
// No operator is assumed to be present
//printf("The value of 4 * 5 is %d\n", (4)(5)); --> Will not return 20/ Wrong!!
printf("The value of 4 * 5 is %d\n", (4)*(5));
// There is no operator to perform exponentiation in C
printf("The value of 4 ^ 5 is %d\n", 4^5); // -> Will not produce 4 to the power 5
printf("The value of 4 to the power 5 is %f\n", pow(2, 5));
printf("The value of 6 + 5 is %d\n", 6 + 5);
printf("The value of 6 + 5.6 is %f\n", 6 + 5.6);
printf("The value of 6.1 + 5.6 is %f\n", 6.1 + 5.6);
printf("The value of 5/2 is %d\n", 5/2);
printf("The value of 3.0/9 is %f\n", 3.0 / 9);
return 0;
}
2.#include <stdio.h>
int main()
{
int x = 2;
int y = 3;
printf("The value of 3*x - 8*y is %d \n", 3*x - 8*y);
printf("The value of 8*y / 3*x is %d \n", 8 * y / 3 * x);
// 8*3 /3*x = 24/6 will give wrong answer
// 24/3*2
// 8*2
// 16
return 0;
}
3.#include<stdio.h>
int main(){
// int a; int b=a;
// int v = 3^3;
// char dt = '2';
// float d = (3.0/8-2);
// printf("%d\n", v);
// printf("%f\n", d);
// Q3. Write a program to determine whether a number is divisible by 97 or not
int num;
printf("Enter the number\n");
scanf("%d", &num);
printf("Divisibility test returns: %d\n", num%97);
// Q4. Step by step evaluation of 3*x/y-z+k
int x = 2, y=3, z=3, k=1;
int result = 3 * x / y - z + k;
// 6/3 - 3 + 1
// 2 - 3 + 1
// 2 - 3 + 1
// 0
printf("The value of result is %d", result);
return 0;
}
No comments