Operators in C Programming with Examples

0

Operators in C Programming with Examples
Operators in C Programming with Examples

📑 Table of Contents

Operators in C Programming

C Operator Definition: Operator is a special symbol it can be used to perform logical or mathematical operations performed on data or variables. Operators is very important part of every programming languages because operators is process of our data and provide accurate results.

Types of Operators in C with Example

  • Arithmetic Operators in C
  • Relational Operators in C
  • Logical Operators in C
  • Assignment Operators in C
  • Bitwise Operators in C
  • Increment Operators in C
  • Decrement Operators in C
  • Conditional Operators in C
  • Special Operator in C

Arithmetic Operators in C Programming

Arithmetic Operator: In this operator can be used to mathematical operation perform like addition, subtraction, multiplication and more. It can be very most important operator in our programming languages.

Arithmetic Operators in C Language

Symbols Operation Example
    (+) Addition (a+b)=> (5+6)=>9
    (-) Subtraction (a-b)=> (8-3)=>5
    (*) Multiplication (a*b)=> (5*6)=>30
    (/) Division (a/b)=> (5/2)=>2.5
   (%) Modulus (a%b)=> (5%2)=>1

Arithmetic Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a,b;
printf("Enter First Number:");
scanf("%d",&a);
printf("Enter Second Number:");
scanf("%d",&b);

printf("Addition of a+b:%d\n",(a+b));
printf("Subtraction of a-b:%d\n",(a-b));
printf("Multiply of a*b:%d\n",(a*b));
printf("Division of a/b:%d\n",(a/b));
printf("Modulus of a%b:%d\n",(a%b));
getch();
}
============OUTPUT============
Enter First Number: 5
Enter Second Number: 2
Addition of a+b: 7
Subtraction of a-b:3
Multiply of a*b:10
Division of a/b:2.5
Modulus of a%b:1

Relational Operators in C Programming

Relational Operator: A relational operators can be used to performed operation two values compression and results provide true and false. Relational operators compare two variables expression and check relationship than provide results.

Relational Operators in C Language

Symbols Operation Example
    (==) Equal To (a==b)
    (>) Greater Than (a>b)
    (<) Less Than (a<b)
    (>=) Greater Than Or Equal To (a>=b)
   (<=) Less Than Or Equal To (a<=b)
   (!=) Not Equal To (a!=b)

Relational Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a,b;
printf("Enter First Number:");
scanf("%d",&a);
printf("Enter Second Number:");
scanf("%d",&b);

printf("Equal To a==b:%d\n",(a==b));
printf("Greater Than a>b:%d\n",(a>b));
printf("Less Than a<b:%d\n",(a<b));
printf("Greater Than or Equal To a>=b:%d\n",(a>=b));
printf("Less Than or Equal To a<=b:%d\n",(a<=b));
printf("Not Equal To a!=b:%d",(a!=b));
getch();
}
============OUTPUT============
Enter First Number: 5
Enter Second Number: 2
Equal To a==b: 0
Greater Than a>b:1
Less Than a<b:0
Greater Than or Equal To a>=b:1
Less Than or Equal To a<=b:0
Not Equal To:1

Logical Operators in C Programming

Logical Operator: A logical operators can be used compare two variables and perform logical operations. If the logical condition is true than return 1 and logical condition is false than return 0.


Logical AND Operator: A logical AND operator condition is a both condition are true than result are true. If the any one condition are false then return false.

Logical AND Operators in C Program

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char username[100]="jiocoding";
char password[100]="12345";
char user[100];
char pwd[100];
printf("Enter Username:");
scanf("%s",&user);
printf("Enter Password:");
scanf("%s",&pwd);
if(strcmp(username,user)==0&&strcmp(password,pwd)==0)
{
printf("Login Successfuly");
}
else{
printf("Username or Password Invalid");
}
getch();
}
============OUTPUT============
Enter Username: jiocoding
Enter Password: 12345
Login Successfully


Logical OR Operator: A logical OR operator can be used to anyone condition are true than both condition are true. If one condition are false than return true.

Logical OR Operators in C Program

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char username[100]="jiocoding";
char password[100]="12345";
char user[100];
char pwd[100];
printf("Enter Username:");
scanf("%s",&user);
printf("Enter Password:");
scanf("%s",&pwd);
if(strcmp(username,user)==0||strcmp(password,pwd)==0)
{
printf("Login Successfuly")
}
else{
printf("Username or Password Invalid");
}
getch();
}
============OUTPUT============
Enter Username: jiocoding
Enter Password: 12345654
Login Successfully


Logical NOT Operator: A logical NOT operator reverses the state means if the condition is true it returns false and if the condition is false it returns true.

Logical Operators in C Language

Symbols Operation Example
    (&&) Logical AND (a&&b)
    (||) Logical OR (a||b)
    (!) Logical NOT (a!=b)

Logical Operators in C Program

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char username[100]="jiocoding";
char password[100]="12345";
char user[100];
char pwd[100];
printf("Enter Username:");
scanf("%s",&user);
printf("Enter Password:");
scanf("%s",&pwd);
if(strcmp(username,user)==0&&strcmp(password,pwd)==0)
{
printf("Login Successfuly")
}
else{
printf("Username or Password Invalid");
}
getch();
}
============OUTPUT============
Enter Username: JIOCODING
Enter Password: 12345
Username or Password Invalid


Assignment Operator in C

Assignment Operator: A assignment operator can be used to assign the value of an right side and variable on the left side. 

Assignment Operators in C Language

Symbols Operation Example
    (=) (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)

Assignment Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a,b;
printf("Enter First Number:");
scanf("%d",&a);
printf("Enter Second Number:");
scanf("%d",&b);
a+=b;
printf("Assignment Addition of a+=b:%d\n",a);

getch();
}
============OUTPUT============
Enter First Number: 8
Enter Second Number: 5
Assignment Addition of a+=b: 13

Bitwise Operator in C Programming

Bitwise Operator: A bitwise operator can be used to perform individual operation bits on integers data. Bitwise operators are used to binary operations performed.

Bitwise Operators in C Language

Symbols Operation Example
    (&) Bitwise AND (a&b)
    (|) Bitwise OR (a|b)
    (<<) Bitwise Shift Left (a<<b)
    (>>) Bitwise Shift Right (a>>b)

Bitwise Operator in C with Example

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a,b;
printf("Enter First Number:");
scanf("%d",&a);
printf("Enter Second Number:");
scanf("%d",&b);

printf("Bitwise AND operator:%d\n",(a&b));
printf("Bitwise OR Operator:%d\n",(a|b));
printf("Bitwise Shift Left Operator:%d\n",(a<<2));
printf("Bitwise Shift Right Operator:%d\n",(a>>2));
getch();
}
============OUTPUT============
Enter First Number: 5
Enter Second Number: 3
Bitwise AND Operator: 1
Bitwise OR Operator:7
Bitwise Shift Left Operator: 20
Bitwise Shift Right Operator:1


Increment and Decrement Operators

Increment Operator: increment or decrement operators can be used to increase 1 value and decrement 1 value. Mostly increment operator commonly used to loops in C.

Increment and Decrement Operators in C Language

Symbols Name Function Example
    (++) Increment It increment the value 1 ++a
    (--) Decrement It Decrement the value 1 --a

Increment Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a;
printf("Enter First Number:");
scanf("%d",&a);

printf("Increment Operator of ++a:%d\n",++a);

getch();
}
============OUTPUT============
Enter any Number: 5
Increment Operator of ++a:6

Decrement Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a;
printf("Enter any Number:");
scanf("%d",&a);

printf("Decrement Operator of --a:%d\n",--a);

getch();
}
============OUTPUT============
Enter any Number: 5
Increment Operator of --a:4


Conditional Operators in C with Examples

Conditional Operators: A conditional operators can be used to one line operators or ternary operators because it can be used to three operands values.

Conditional Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
10<5?printf("Welcome"):printf("Jiocoding");
getch();
}
============OUTPUT============
Jiocoding


Special Operators in C with Examples

Special operator: A special operator can be used to perform special operations performed logical or mathematical. 

Special Operators in C Language

Symbols Descriptions Example
    (&) It is used to find address of variable. int x=10;
    (*) It is used to declared Pointer Variable. int *p;
    (sizeof()) It can be used find memory size. int x=5;

Special Operators in C Program

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
int a=10;
printf("Address of a:%u\n",&a);
printf("Size of a:%d\n",sizeof(a));
getch();
}
============OUTPUT============
Address of a:7339548
Size of a:4


Conclusion of Operators in C Programming

Conclusion: A C operators can be used mathematical operations performed on data variables. I can explain everyone operators including with example if your any query or operator related questions than you can contact me by email or comment.

Tags

Post a Comment

0 Comments
Post a Comment (0)
To Top