Operators Are The Symbol Which Operates On Value Or A Variable. For Example: + Is A Operator To Perform Addition.
C Programming Language Has Wide Range Of Operators To Perform Various Operations. For Better Understanding Of Operators, These Operators Can Be Classified As:
Operators In C Programming
1st. Arithmetic Operators
2nd. Increment And Decrement Operators
3rd. Assignment Operators
4th. Relational Operators (comparison)
5th. Logical Operators
6th. Conditional Operators(Ternary)
7th. Bitwise Operators
8th. Special Operators
Arithmetic Operators
Operator Meaning Of Operator
+ Addition Or Unary Plus
- Subtraction Or Unary Minus
* Multiplication
/ Division
% Remainder After Division( Modulo Division)
Example Of Working Of Arithmetic Operators
/* Program To Demonstrate The Working Of Arithmetic Operators In C. */
#include<stdio.h>
#include<conio.h>
Int Main(){
Int A=9,B=4,C;
C=A+B;
Printf("A+B=%D\N",C);
C=A-B;
Printf("A-B=%D\N",C);
C=A*B;
Printf("A*B=%D\N",C);
C=A/B;
Printf("A/B=%D\N",C);
C=A%B;
Printf("Remainder When A Divided By B=%D\N",C);
Return 0;
}
}
A+B=13
A-B=5
A*B=36
A/B=2
Remainder When A Divided By B=1
Explanation
Here, The Operators +, - And * Performed Normally As You Expected. In Normal Calculation, 9/4 Equals To 2.25. But, The Output Is 2 In This Program. It Is Because, A And B Are Both Integers. So, The Output Is Also Integer And The Compiler Neglects The Term After Decimal Point And Shows Answer 2 Instead Of 2.25. And, Finally A%B Is 1,I.E. ,Whena=9 Is Divided By B=4, Remainder Is 1.
Suppose A=5.0, B=2.0, C=5 And D=2
In C Programming,
A/B=2.5
A/D=2.5
C/B=2.5
C/D=2
Note: % Operator Can Only Be Used With Integers.
Increment And Decrement Operators
In C, ++ And -- Are Called Increment And Decrement Operators Respectively. Both Of These Operators Are Unary Operators, I.E, Used On Single Operand. ++ Adds 1 To Operand And -- Subtracts 1 To Operand Respectively. For Example:
Let A=5 And B=10
A++; //A Becomes 6
A--; //A Becomes 5
++A; //A Becomes 6
--A; //A Becomes 5
Difference Between ++ And -- Operator As Postfix And Prefix
When I++ Is Used As Prefix(Like: ++Var), ++Var Will Increment The Value Of Var And Then Return It But, If ++ Is Used As Postfix(Like: Var++), Operator Will Return The Value Of Operand First And Then Only Increment It. This Can Be Demonstrated By An Example:
#include<stdio.h>
#include<conio.h>
Int Main(){
Int C=2,D=2;
Printf("%D\N",C++); //This Statement Displays 2 Then, Only C Incremented By 1 To 3.
Printf("%D",++C); //This Statement Increments 1 To C Then, Only C Is Displayed.
Return 0;
}
Output
2
4
Assignment Operators
The Most Common Assignment Operator Is =. This Operator Assigns The Value In Right Side To The Left Side. For Example:
Var=5 //5 Is Assigned To Var
A=C; //Value Of C Is Assigned To A
5=C; // Error! 5 Is A Constant.
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
Relational Operator
Relational Operators Checks Relationship Between Two Operands. If The Relation Is True, It Returns Value 1 And If The Relation Is False, It Returns Value 0. For Example:
A>B
Here, > Is A Relational Operator. If A Is Greater Than B, A>B Returns 1 If Not Then, It Returns 0.
Relational Operators Are Used In Decision Making And Loops In C Programming.
Operator Meaning Of Operator Example
== Equal To 5==3 Returns False (0)
> Greater Than 5>3 Returns True (1)
< Less Than 5<3 Returns False (0)
!= Not Equal To 5!=3 Returns True(1)
>= Greater Than Or Equal To 5>=3 Returns True (1)
<= Less Than Or Equal To 5<=3 Return False (0)
Logical Operators
Logical Operators Are Used To Combine Expressions Containing Relation Operators. In C, There Are 3 Logical Operators:
Operator Meaning Of Operator Example
&& Logial And If C=5 And D=2 Then,((C==5) && (D>5)) Returns False.
|| Logical Or If C=5 And D=2 Then, ((C==5) || (D>5)) Returns True.
! Logical Not If C=5 Then, !(C==5) Returns False.
Explanation
For Expression, ((C==5) && (D>5)) To Be True, Both C==5 And D>5 Should Be True But, (D>5) Is False In The Given Example. So, The Expression Is False. For Expression ((C==5) || (D>5)) To Be True, Either The Expression Should Be True. Since, (C==5) Is True. So, The Expression Is True. Since, Expression (C==5) Is True, !(C==5) Is False.
Conditional Operator
Conditional Operator Takes Three Operands And Consists Of Two Symbols ? And : . Conditional Operators Are Used For Decision Making In C. For Example:
C=(C>0)?10:-10;
If C Is Greater Than 0, Value Of C Will Be 10 But, If C Is Less Than 0, Value Of C Will Be -10.
Bitwise Operators
A Bitwise Operator Works On Each Bit Of Data. Bitwise Operators Are Used In Bit Level Programming.
Operators Meaning Of Operators
& Bitwise And
| Bitwise Or
^ Bitwise Exclusive Or
~ Bitwise Complement
<< Shift Left
>> Shift Right
Bitwise Operator Is Advance Topic In Programming . Learn More About Bitwise Operator In C Programming.
Other Operators
Comma Operator
Comma Operators Are Used To Link Related Expressions Together. For Example:
Int A,C=5,D;
The Sizeof Operator
It Is A Unary Operator Which Is Used In Finding The Size Of Data Type, Constant, Arrays, Structure Etc. For Example:
#include<stdio.h>
#include<conio.h>
Int Main(){
Int A;
Float B;
Double C;
Char D;
Printf("Size Of Int=%D Bytes\N",Sizeof(A));
Printf("Size Of Float=%D Bytes\N",Sizeof(B));
Printf("Size Of Double=%D Bytes\N",Sizeof(C));
Printf("Size Of Char=%D Byte\N",Sizeof(D));
Return 0;
}
Output
Size Of Int=4 Bytes
Size Of Float=4 Bytes
Size Of Double=8 Bytes
Size Of Char=1 Byte
Conditional Operators (?:)
Conditional Operators Are Used In Decision Making In C Programming, I.E, Executes Different Statements According To Test Condition Whether It Is Either True Or False.
Syntax Of Conditional Operators
Conditional_Expression?Expression1:Expression2
If The Test Condition Is True, Expression1 Is Returned And If False Expression2 Is Returned.
Example Of Conditional Operator
#include<stdio.h>
#include<conio.h>
Int Main(){
Char Feb;
Int Days;
Printf("Enter L If The Year Is Leap Year Otherwise Enter 0: ");
Scanf("%C",&Feb);
Days=(Feb=='L')?29:28;
/*If Test Condition (Feb=='L') Is True, Days Will Be Equal To 29. */
/*If Test Condition (Feb=='L') Is False, Days Will Be Equal To 28. */
Printf("Number Of Days In February = %D",Days);
Return 0;
}
Output
Enter L If The Year Is Leap Year Otherwise Enter N: L
Number Of Days In February = 29
Other Operators Such As &(Reference Operator), *(Dereference Operator) And ->(Member Selection) Operator Will Be
Precedence And Associativity Of Operators
Precedence Of Operators
If More Than One Operators Are Involved In An Expression Then, C Language Has Predefined Rule Of Priority Of Operators. This Rule Of Priority Of Operators Is Called Operator Precedence.
In C, Precedence Of Arithmetic Operators(*,%,/,+,-) Is Higher Than Relational Operators(==,!=,>,<,>=,<=) And Precedence Of Relational Operator Is Higher Than Logical Operators(&&, || And !). Suppose An Expression:
(A>B+C&&D)
This Expression Is Equivalent To:
((A>(B+C))&&D)
I.E, (B+C) Executes First
Then, (A>(B+C)) Executes
Then, (A>(B+C))&&D) Executes
Associativity Of Operators
Associativity Indicates In Which Order Two Operators Of Same Precedence(Priority) Executes. Let Us Suppose An Expression:
A==B!=C
Here, Operators == And != Have Same Precedence. The Associativity Of Both == And != Is Left To Right, I.E, The Expression In Left Is Executed First And Execution Take Pale Towards Right. Thus, A==B!=C Equivalent To :
(A==B)!=C
The Table Below Shows All The Operators In C With Precedence And Associatively.
Note: Precedence Of Operators Decreases From Top To Bottom In The Given Table.
Summary Of C Operators With Precedence And Associativity
Operator Meaning Of Operator Associativity
()
[]
->
. Functional Call
Array Element Reference
Indirect Member Selection
Direct Member Selection Left To Right
!
~
+
-
++
--
&
*
Sizeof
(Type) Logical Negation
Bitwise(1 'S) Complement
Unary Plus
Unary Minus
Increment
Decrement
Dereference Operator(Address)
Pointer Reference
Returns The Size Of An Object
Type Cast(Conversion) Right To Left
*
/
% Multiply
Divide
Remainder Left To Right
+
- Binary Plus(Addition)
Binary Minus(Subtraction) Left To Right
<<
>> Left Shift
Right Shift Left To Right
<
<=
>
>= Less Than
Less Than Or Equal
Greater Than
Greater Than Or Equal Left To Right
==
!= Equal To
Not Equal To Left To Right
& Bitwise And Left To Right
^ Bitwise Exclusive Or Left To Right
| Bitwise Or Left To Right
&& Logical And Left To Right
|| Logical Or Left To Right
?: Conditional Operator Left To Right
=
*=
/=
%=
-=
&=
^=
|=
<<=
>>= Simple Assignment
Assign Product
Assign Quotient
Assign Remainder
Assign Sum
Assign Difference
Assign Bitwise And
Assign Bitwise Xor
Assign Bitwise Or
Assign Left Shift
Assign Right Shift Right To Left
, Separator Of Expressions Left To Right
C Program To Print A Sentence
Source Code
/* C Program To Print A Sentence. */
#include<stdio.h>
#include<conio.h>
Int Main()
{
Printf("C Programming"); /* Printf() Prints The Content Inside Quotation */
Return 0;
}
Output
C Programming
Explanation
Every C Program Starts Executing Code From Main( ) Function. Inside Main( ), There Is A Printf( ) Function Which Prints The Content Inside The Quotation Mark Which Is "C Programming" In This Case. Learn More About Output In C Programming Language.
C Program To Display Its Own Source Code
/* The File Name Of This C Program Should Be Sourcecode.C */
#include<stdio.h>
#include<conio.h>
Int Main(){
File *Fp;
Char C;
Fp = Fopen("Sourcecode.C","R");
Do{
C= Getc(Fp);
Putchar(C);
}
While(C!=Eof);
Fclose(Fp);
Return 0;
}
Source Code To Display Its Own Source Code Using Macro
A Predefined Macro __File__ Contains The Location Of A C Programming File It Is Working On. For Example:
#Include
Int Main(){
Printf("%S",__File__);
}
The Output Of This Program Is The Location Of This C Programming File.
C Program To Display Its Own Source Code Using __File__
#include<stdio.h>
#include<conio.h>
Int Main(){
File *Fp;
Char C;
Fp = Fopen(__File__,"R");
Do{
C= Getc(Fp);
Putchar(C);
}
While(C!=Eof);
Fclose(Fp);
Return 0;
}
This Program Displays The Content Of This Particular C Programming File(Source Code) Because __File__ Contains The Location Of This C Programming File In A String.
No comments:
Post a Comment