C Programming Operators and Expressions Assignment Help
Operators which perform operations on operands and yields output. It combines variables or constants to from an expression. Example of expression is a+10 in which a is variable, ‘10’ is constant and ‘+’ is operators.
C programming consists 8 categories of operators such as:
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Increment and Decrement operators
- Conditional operators
- Bitwise operators
- Special operators
C Programming Operators and Expressions Assignment Help By Online Tutoring and Guided Sessions from assignmenthippo.com
Arithmetic operators
It is used for basic arithmetic operations. They are ‘+’, ‘-‘, ‘*‘, ‘/’ and ‘%‘.
Unary Operators:
Unary operators operate on single operand.
+x, -y
Here ‘+’, ‘-‘ changes the sign of x and y.
Binary operators
Unary operators operate on two(binary) operand.
x+y, x-y
Relational Operators
Relational operators are used to find the relationship among operands in the expression. C supports 6 relational operators.
Relational Operators in C
Operators |
Example/Description |
> |
x > y (x is greater than y) |
< |
x < y (x is less than y) |
>= |
x >= y (x is greater than or equal to y) |
<= |
x <= y (x is less than or equal to y) |
== |
x == y (x is equal to y) |
!= |
x != y (x is not equal to y) |
Relational operator forms relational expressions. Relational operators are most commonly used in decision making statements such as if, while, until etc.
Let us consider two variables x=10 and y=5, the simple relational expressions are
Expression |
Relational Value |
Output |
x<y |
False |
0 |
x<=y |
False |
0 |
x = = y |
False |
0 |
x != y |
True |
1 |
x>y |
True |
1 |
y != 0 |
True |
1 |
Logical Operators
Logical operators are used to combine more than one relational expression. C programming supports three logical operators. They are:
Operators |
&& (logical AND) |
|| (logical OR) |
! (logical NOT) |
Let us consider two variables x=10 and y=5, the simple relational expressions are
Expression |
Relational Value |
Relational Value |
Output |
(x>5)&&(y<5) |
(True) && )False) |
False |
0 |
(x>=10)||(y>=10) |
(True) || (False) |
True |
1 |
!((x>5)&&(y<5)) |
!((true) && (False)) |
True |
1 |
Assignment Operator
Assignment operators are used to assign values from an expression after performing operations on it. ‘=’ is the assignment operator in C.
Operators |
Expression |
Output |
= (simple) |
a=2 |
2 is assigned to variable a |
+= |
a+=2 |
a=a+2 if a = 2 means output of a will be 4 |
-= |
a-=2 |
a=a-2 if a = 2 means output of a will be 0 |
*= |
a*=2 |
a=a*2 if a = 2 means output of a will be 4 |
/= |
a/=2 |
a=a/2 if a = 2 means output of a will be 1 |
%= |
a%=2 |
a=a%2 if a = 2 means output of a will be 0 |
&= |
a&=25 |
a=a&25 if a=12 means Bit Operation of 12 and 25 a(12) = 00001100 & (25)= 00011001 00001000 = 8 (In decimal) Output will be 8 |
^= |
a^=25 |
a=a^25 |
Increment and Decrement Operators
Program: do pre-increment and post-increment
#include<stdio.h>
#include<conio.h>
void main()
int x,i;
i=10;
j=10;
x=++i;
y=j++;
printf({"x: %d"},x);
printf({"i: %d"},i);
printf({"y: %d"},y);
printf({"j: %d"},j);
getch();
Output:
x:11
i:11
y=10
j=11
Program for pre-decrement and post-decrement
#include<stdio.h>
#include<conio.h>
void main()
int x,i;
i=10;
j=10;
x=--i;
y=j--;
printf({"x: %d"},x);
printf({"i: %d"},i);
printf({"y: %d"},y);
printf({"j: %d"},j);
getch();
Output:
x:9
i:9
y=10
j=9
Conditional operators
Conditional operator is otherwise called as a ternary operator. It has three parts such as test expression, statement1 if test expression is true and statement2 if test expression is false.It follows the same logic of If..Else.
Conditional operator is written as:
Test_condition ? statement1: statement2
Example:
#include<stdio.h>
int main()
int mark;
printf(" Please Enter your Mark here: \n ");
scanf(" %d ", &mark);
(age > =40) ? printf(" You are pass ") :
printf(" fail ");
return 0;
Output:
Please Enter your Mark here:
50
You are pass
Bitwise Operators
C has the ability to support mathematical operations like addition, subtraction, multiplication and division at binary level (0’s and 1’s).The binary level operations are accomplished with the help of 6 bitwise operators.
- & (bitwise AND) : It takes two integer numbers are operands.
Example:
Let us consider that we have 2 operands op1 and op2 are represented by binary values as follows:
op1 = 0000 1101
op2 = 0001 1001
&----------
0000 1001
-------
- | (bitwise OR) : It performs bitwise OR on two integers. The result of OR is 1 any of the two bits is 1.
Example:
op1 = 0000 1101
op2 = 0001 1001
|-----------
0001 1101
---------
- ^ (bitwise XOR) : It Takes two numbers as integer operands. XOR produces the result ‘1’ or ‘true’ if one of the value is true.If both values are true the result will be false.
Example:
op1 = 0000 1101
op2 = 0001 1001
^-----------
0001 0100
-----------
- << (left shift) : It takes two argument, the first argument is the operand in which left shifting is performed, the second argument decides number of moves on left side.
Example:
11010100<<1 = 110101000
11010100<<4 =110101000000
- >> (right shift) : It takes two argument, the first argument is the operand in which left shifting is performed, the second argument decides number of moves on left side.
Example:
11010100>>2 = 00110101
11010100>>8 = 00000000
- ~ (bitwise NOT) : It is complement operator. It converts ‘1’ to ‘0’ and vice-versa.
Example:
~ 00100011
11011100
Special Operators in C
Operator |
Description |
Example |
Comma(,) |
To connect more than one expression |
for(i=1,j=1;i<=10;i++,j++) |
Type cast Operator |
To convert one data type to specified data type |
float mark=51.6; int m=int(mark); |
sizeof |
Returns the size of the memory occupied by an entity |
sizeof(x)return size of the memory space holds by variablex |
& |
Returns the address of an variable |
&x ;return the starting address of the variablex in hexadecimal format |
* |
Direct access of memory using pointers, without seeking permission to OS |
*x ;will be pointer to a variablex |