Introduction to C Programming
Operators in C Part 1
Operators in C Part 2
Precedence and Associativity Of Operators in C
If else Statements in C
Switch Statement in C
Loops in C
While and Do-While loops in C
Nested loops in C
Loop Control Statements in C
1D Arrays in C
2D Arrays in C
Pattern Printing in 2D arrays
Functions in C
Pointers in C
ASCII Encoding and Typecasting in C
Operators in C Part 1
Operators are symbols used to perform certain operations on the data. There are
basically 5 types of operators :
- Arithmetic Operators
- Assignment Operators
- Comparision Operators
- Logical Operators
- Bitwise Operators
- Arithmetic Operators
- Binary Operators
- Unary Operators
- Assignment Operators
Jump to specific sections
Arithmetic Operators
They are used to perform mathematical operations on values. They are of two types :
- Binary Operators
- Unary Operators
Binary Operators
These are the operators which work on at least two operands(or numbers). For example, you need addition,
multiplication, division, etc.For example.
#include <stdio.h>
void main()
{
int a=5;
int b=10;
int c=a+b;
int d=a-b;
printf("%d + %d = %d ",a,b,c);
printf("%d - %d = %d ",a,b,d);
//Or, we can simply do it without creating any variables.
printf("%d X %d = %d ",a,b,a*b);
printf("%d / %d = %d ",b,a,b/a);
}
Output
5 + 10 = 15
5 - 10 = -5
5 * 10 = 50
10 / 5 = 2
Unary Operators
They work on only one operand(or number). They are majorly of 4 types -
- Pre-increment - First increase the value by 1 and then prints it.
- Pre-decrement - First decrease the value by 1 and then prints it.
- Post-increment - First prints the value and then increases it by 1.
- Post-decrement - First prints the value and then decreases it by 1.
#include
void main()
{
int a=4;
printf("Value of a = %d",a);
printf("Value of ++a = %d",++a); // a becomes 5
printf("Value of --a = %d",--a); // a becomes 4
printf("Value of a++ = %d",a++); // a becomes 5 but prints 4
printf("Value of a = %d",a); // Current value of a is 5
printf("Value of a-- = %d",a--); //Now a is 5 so print 5 and then decrement
printf("Value of a = %d",a); // Current value of a is 4
}
Output
Value of a = 4
Value of ++a = 5
Value of --a = 4
Value of a++ = 4
Value of a = 5
Value of a-- = 5
Value of a = 4
Assignment Operators
The (=) operator is used to assign values to variables. For example -
#include <stdio.h>
void main()
{
int a=4;
printf("Value of a = %d",a);
//Make b equal to a
int b=a;
printf("Value of b = %d",b);
b=7;
//Value of a remains unchanged
printf("Value of a = %d",a);
printf("Value of b = %d",b);
}
Output
Value of a = 4
Value of b = 4
Value of a = 4
Value of b = 7
It is of 4 types as follows :
- Addition Assignment : a+=2; (Same as a=a+2;)
- Subtraction Assignment : a-=2; (Same as a=a-2;)
- Multiplication Assignment : a*=2; (Same as a=a*2;)
- Division Assignment : a/=2; (Same as a=a/2;)
#include <stdio.h>
void main()
{
int a;
printf("Enter the value of a :");
scanf("%d",&a);
a+=2;
printf("Value of a after adding 2 = %d ",a);
}
Output
Enter the value of a:10
Value of a after adding 2 = 12