OPERATORS IN C
Operators are the symbols that are used to perform various operations on data, like arithmetic operations, comparison or logical operations etc. Operator refers to a symbol that tells the computer to perform certain mathematical or logical tasks on operands. Operators can be Unary and Binary. Unary Operators work on one operand like -a, b++ etc and Binary operators work on two operators like a+b,a*c etc. C provides different types of operators as follow:
1) Arithmetic operators in c
2) Logical operators in c
3) Assignment operators in c
4) Relational operators in c
5) Bit wise operators in c
6) Unary operators in c
7) sizeof operator in c
8) Comma operator in c
9) Conditional operator in c
These operators are performed on operands and operands are the variables on which these operations are performed. e.g x+y
In this x,y are operands and + is operator and x+y is called expression.
These operators are used in arithmetic operations. C provides all the basic arithmetic operators like +,-,*,/,%.
The modulus(%) operator tells us what would be the remainder after integer division.Name Symbol Addition + Subtraction - Multification * Division / Modulus %
Example of program of addition Operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a=8,b=12,c;
Name | Symbol |
---|---|
Addition | + |
Subtraction | - |
Multification | * |
Division | / |
Modulus | % |
clrscr();
c=a+b;
printf("%d",c);
getch();
}
Output:-
20
Example of program of Subtraction Operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a=18,b=12,c;
clrscr();
c=a-b;
printf("%d",c);
getch();
}
Output:-
6
Example of program of Multification Operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a=3,b=12,c;
clrscr();
c=a*b;
printf("%d",c);
getch();
}
Output:-
36
Example of program of Division Operator
#include<stdio.h>
#include<conio.h>
void main()
{
int a=18,b=2,c;
clrscr();
c=a/b;
printf("%d",c);
getch();
}
Output:-
9
Example of program division operator with float data type
#include<stdio.h>
#include<conio.h>
void main()
{
float a=9,b=5,c;
clrscr();
c=a/b;
printf("%f",c);
getch();
}
Output:-
1.8
Example of program of Modulus Operator
This Operator perform operation only on integer data type, it will not perform on float, char other data type.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=19,b=5,c;
clrscr();
c=a%b;
printf("%d",c);
getch();
}
Output:-
4
Note:-it will not perform on float, char other data type.
#include<stdio.h>
#include<conio.h>
void main()
{
float a=19,b=5,c;
clrscr();
c=a%b;//error
printf("%f",c);
getch();
}
Output:-
error
# C program to swap two number without using third variable example simple program in c language
We can swap two number without using third variable. We can swap with two ways without using third variable in c/c++ language.
a) By + and -
b)By * and /
Example a: Using + and -
Simple C program example swap two number without using third variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=9;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=14(5+9)
b=a-b;//b=5(14-9)
a=a-b;//a=9(14-5)
printf("\n After swap a=%d b=%d",a,b);
getch();
}
Output:-
Before swap a=5 b=9
After swap a=9 b=5
Example b: Using * and /
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=9;
printf("Before swap a=%d b=%d",a,b);
a=a*b;//a=45(5*9)
b=a/b;//b=5(45/9)
a=a/b;//a=9(45/5)
printf("\n After swap a=%d b=%d",a,b);
getch();
}
Output:-
Before swap a=5 b=9
After swap a=9 b=5
# C program to swap two number using with third variable example simple program in c language
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=9,c;
printf("Before swap a=%d b=%d",a,b);
c=a;//c=5
a=b;//a=9
b=c;//b=5
printf("\n After swap a=%d b=%d",a,b);
getch();
}
Output:-
Before swap a=5 b=9
After swap a=9 b=5
Comma operator
The comma operator (,) is used to declare more than one variable in single line . It is als known as separater as it separater one variable name from other.
Example:- int a,b,c;
The sizeof operator returns the size , in bytes of the given operator. A sizeof operator is a unary operator that returns the number of bytes required store a variable or a data type.
Sizeof operator in c
Syntax is: sizeof(datatype or variable)
Example of find the size of int, float,char,long,double,long double
#include< stdio.h >
#include< conio.h >
void main()
{
int a;
float b;
char c;
long d;
double e;
long double f;
printf("\n size of int=%d bytes",sizeof(a));
printf("\n size of float=%d bytes",sizeof(b));
printf("\n size of char=%d bytes",sizeof(c));
printf("\n size of long=%d bytes",sizeof(d));
printf("\n size of duble=%d bytes",sizeof(e));
printf("\n size of long double=%d bytes",sizeof(f));
getch();
}
Output:-
size of int =2 bytes
size of float =4 bytes
size of char =1 bytes
size of long =4 bytes
size of double =8 bytes
size of long double =10 bytes
Post Increment Operator
A post-increment operator is used to increment the value of variable after executing expression completely in which post increment is used. In the Post-Increment, value is first used in a expression and then incremented.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b;
clrscr();
b=a++;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Output:-
a=6
b=5
Pre Increment Operator
The pre increment operator is used to increment the value of some variable before using it in an expression. In the pre increment the value is incremented at first, then used inside the expression.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b;
clrscr();
b=++a;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}Output:-
a=6
b=6Post Decrement Operator
A post-decrement operator is used to decrement the value of a variable after executing the expression in which the operator is used. With the post-decrement operator, the value of the variable is first used in an expression and then decremented.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b;
clrscr();
b=a--;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Output:-
a=4
b=5
Pre Decrement Operator
Pre decrement operator is used to decrement variable value by 1 before assigning the value to the variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b;
clrscr();
b=--a;
printf("\n a=%d",a);
printf("\n b=%d",b);
getch();
}
Output:-
a=4
b=4
Assignment operator It is used to assign value the variable. Value can be any constant value or evaluation of an expression. It evaluates the expression on right hand side and then assigns the value to a variable on the left hand side.
Some assignment operatorsOperator Example Meaning += x+=4 x=x+4 -= x-=4 x=x-4 *= x*=4 x=x*4 /= x/=4 x=x/4 %= x%=4 x=x%4
#include< stdio.h >
#include< conio.h >
void main()
{
int a=5,b=6,c=8,d=16,e=13;
clrscr();
a+=7;
b-=3;
c*=2;
d/=4;
e%=8;
printf("\naddition assignment operator%d",a);
printf("\nsubtraction assignment operator%d",b);
printf("\nmultification assignment operator%d",c);
printf("\ndivision assignment operator%d",d);
printf("\nmodulud assignment operator%d",e);
getch();
}
Output:-
addition assignment operator 12
subtraction assignment operator 3
multification assignment operator 16
division assignment operator 4
modulud assignment operator 5
Operator | Example | Meaning |
---|---|---|
+= | x+=4 | x=x+4 |
-= | x-=4 | x=x-4 |
*= | x*=4 | x=x*4 |
/= | x/=4 | x=x/4 |
%= | x%=4 | x=x%4 |
Bit Wise Operator
Bitwise operators are used to change individual bits in an operand. A single byte of computer memory-when viewed as 8 bits-can signify the true/false status of 8 flags because each bit can be used as a boolean variable that can hold one of two values: true or false.
#include<conio.h>
void main()
{
int n,m,and,or,com,rs,ls,xor;
n=5,m=11;
clrscr();
and=n&m;
or=n|m;
xor=n^m;
com=~n;
ls=n<<2;
rs=n>>2;
printf("\n n&m=%d",and);
printf("\n n|m=%d",or);
printf("\n n^m=%d",xor);
printf("\n ~n=%d",com);
printf("\n rs>>2=%d",rs);
printf("\n ls<<2=%d",ls);
getch();
}
Output :-
n&m=1
n|m=15
n^m=14
~n= -6
rs>>2=1
ls<<2=20
Logical operators
The logical operators && and ||are used when we want to test more than one condition to make decisions,the result of logical operator AND (&&) will be true only if both the conditions are true,whereas the result of a logical operators OR (||) will be true if one or more conditions are true. In other words,the result of a logical OR operation will be false only if all conditions are false,
logical operators
NAME SYMBOL IN 'C'
AND &&
OR ||
NOT !
e,g, 1) (age>18 && age<=25)
2) (number<0 || number>10)
LOGICAL AND(&&)
AND(&&) is a logical operator that will give result if and only if all of its conditions are true.
Condition 1 | Condition 2 | result | value |
False | False | False | 0 |
False | True | False | 0 |
True | False | False | 0 |
True | True | True | 1 |
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=5,b=7;
if(a>3&&b>5)
{
printf("condition is true");
}
else
{
printf("condition is false");
}
getch();
}
Output: condition is true
LOGICAL OR(||)
OR(||) is a logical operator that will give result if minimum one of its condition is true.
Condition 1 | Condition 2 | result | value |
False | False | False | 0 |
False | True | True | 1 |
True | False | True | 1 |
True | True | True | 1 |
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=5,b=7;
if(a>3||b<5)
{
printf("condition is true");
}
else
{
printf("condition is false");
}
getch();
}
Output: condition is true
LOGICAL NOT(!)
NOT (!) is a logical operator that convert true into false and false into true.
input | output | value |
False | True | 1 |
True | False | 0 |
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a=5;
if(!(a<3))
{
printf("condition is true");
}
else
{
printf("condition is false");
}
getch();
}
Output: condition is true
Condition operator or ternary operator and Nested ternary operators.
The condition operator is sometime called ternary operator since they take three arguments. These arethe replacement for if else statement.
Expression1? Expression2: Expression3
If expression 1 is true then the value returned will be expression 2
Otherwise the value returned will be expression 3
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter the value of a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
(a>b)?printf(“a is big”):printf(“b is big”);
getch();
}
Nested ternary operators
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf(“enter the value of a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
printf(“enter the value of c”);
scanf(“%d”,&c);
(a>b&&a>c)?printf(“a is big”):(b>c)?printf(“b is big”):printf(“c is big”);
(a>b&&a>c)?printf(“a is big”):(b>c)?printf(“b is big”):printf(“c is big”);
getch();
}
The Escape Sequences :-
Escape sequences are used for formatting the output string mainly written in printf(). It consists of backslash followed by a character. The character sequence is always enclosed in the control string. Various Escape sequences are:
\n the next line character
\t tab position
\a used for sound
\r carriage return
\b shift cursor one position left
Without any Escape sequence:-
printf(“hellobye”);
Output:
Hellobye
With \n Escape sequence:-
Using \n the output will be shifted to next line
Printf(“Hello\nBye”);
Output:
Hello
Bye
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello\nBye”);
getch();
}
Output:
Hello
Bye
With \t Escape sequence:-
Using \t some space [TAB]will be displayed before printing next information
printf(“hello\tbye”);
Output:
hello bye
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“hello\tbye”);
getch();
}
Output:
hello bye
With \b Escape sequence:-
After \b one character form the left of \b is deleted
printf(“hello\bbye”);
Output:
hellbye
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“hello\bbye”);
getch();
}
Output:
hellbye
With \r Escape sequence :-
Using \r position of the cursor come to the beginning of the line.
printf(“hello\rbye”);
Output:
byelo
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“hello\rbye”);
getch();
}
Output:
byelo
with \a sequence:-
it produce beep sound.
printf(“Hello \abye”)
Output:
Hello[beep sound]bye
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(“Hello \abye”)
getch();
}
Output:
Hello[beep sound]bye
I/O Function in ‘C’ Language:-
Input/output means to receive data form any input device and send data to the output device respectively.
I\O Function can be classified into two types:
1. Disk I/O Function:- These are the functions that perform I\O operations on secondary storage device like floppy, hard disk etc.
2. Console i/o function:-These are the function that receive input form keyboard and output to screen.
These function are further classified into two types:
a) Unformatted I\O Functions
b) Formatted I\O Functions
Unformatted I/O Function:-
1. getch():- when we type a character ,the character would not be displayed on the screen but it is assigned to a variable immediately without pressing enter button form the keyboard .
for e.g.
char A:
A= getch();
2. getche():-when we type a character, the character is displayed on the screen and assigned to a variable immediately without pressing enter button form the keyboard.
For e.g.
char A:
A =getche();
3. getchar():-The typed character is displayed on the screen but it is assigned to a variable when we press enter button form the keyboard.
For e.g.
char A
4. A=getchar();
5. gets():-it is used to input string. we can also input spaces between the various words of the string.
6. putch() or putchar():- These function are exactly the same and use to print a character to screen.
7. puts:- This function is used to print the string on the monitor.
Formatted i\o function:-
1. scanf():- in scanf we can input integer ,float ,char, string simultaneously. The limitation of this function is that we cannot enter space between the words of the string.
2. printf():- this function is used to display values of int , float or plain messages to the user.
Syntax is:-
printf(“format string”,variablelist);
Type casting or type conversion:-
It is used to convert the one data type into another temporarily. It can be implicit or explicit. Implicit type conversion is automatically performed by the computer and explicit type conversion is performed by the programmer.
Void main() void main()
{ {
Int a=5; int a=5;
float b; float b;
b=a/2; b=(float)a/2;
printf(“b=%f”,b); printf(“b=%f”,b);
} }
Output:-b=2.000000 output:-b=2.500000
*******************************
ReplyDelete