इस tutorial में हम operators और operators के प्रकार (types of operators) के बारे में सीखेंगे और उदाहरणो से समझेंगे भी की किस operator का C programming में क्या उपयोग होता हैं ?
चलिये operators को समझते हैं –
Operators in C Programming | Hindi
C programming में operators वे symbols होते हैं जिनके द्वारा गणितीय (mathematical) और logical संगणनाएं (computations) करने के लिए compiler को निर्देश दिये जाते हैं ।
जैसे
Plus + एक ऑपरेटर हैं जिसके द्वारा values को जोड़ने (Addition ) का mathematical logic बनाया जा सकता हैं और C program को रन करने पर values का sum प्राप्त किया जा सकता हैं ।
c=a+b;
ऐसे ही minus – operator के द्वारा घटाने (Subtraction) का logic आदि –
c=a-b;
दूसरे शब्दो में कहे तो operators ऐसे symbols होते हैं जिसके द्वारा variables और values को operate किया जाता हैं ।
जैसे –
z=x*y;
यहाँ पर x और y variables हैं जिन्हे operands भी कहा जाता हैं operands के बारे में हमने tokens वाले tutorial में भी सीखा था । तो यहाँ पर x और y की values का addition होके z variable में स्टोर हो रहा हैं । यहा पर z को प्रिंट करने से x और y की sum value प्राप्त हो सकती हैं । यहाँ पर आप देख सकते हैं की operator के द्वारा data manipulation (डाटा में हेर फेर करना ) किया गया हैं तो operators को variables का data manipulation के लिए भी उपयोग में लाया जाता हैं ।
इसलिए operators का उपयोग C programming मे बहुत शारे लॉजिक को बनाने में किया जाता हैं ।
यदि आपको अच्छे से C programming language को सीखना हैं तो आपको सभी operators और उनके उपयोग के बारे में पता होना बहुत ही जरूरी हैं ।
चलिये operators के प्रकार के बारे में सीखते हैं ।
C language में operators के प्रकार | Types of operators in C language:–
C language में operators कई प्रकार के होते हैं जो की नीचे लिस्ट में लिखे गए हैं –
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Bitwise Operators
5. Assignment Operators
6. Misc & Other Operators
Arithmetic Operators in C programming –
उपर जो उदाहरण समझे थे जैसे + operator के द्वारा addition और – operator के द्वारा subtraction तो ये ये दोनों operators arithmetic operators होते हैं ।
Arithmetic operators वे operators होते हैं जिनके द्वारा mathematical operation perform किए जाते हैं ।
साधारण हिन्दी भाषा में समझे तो arithmetic operators, mathematics operations जैसे- Addition , subtraction , multiplication , divide आदि के लिए उपयोग किए जाते हैं ।
Operator |
Meaning |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
उपर सभी arithmetic operators और उनका मतलब दिया गया हैं ।
चलिये एक उदाहरण से इन सभी arithmetic operators के बारे में समझते हैं –
#include <stdio.h>
void main()
{
int x = 10,y = 4, z;
z = x+y;
printf("x+y (Addition) = %d \n",z);
z = x-y;
printf("x-y (Subtraction) = %d \n",z);
z = x*y;
printf("x*y (Multiplication) = %d \n",z);
z = x/y;
printf("x/y (Divide)= %d \n",z);
z = x%y;
printf("x%y (Modulus)= %d \n",z);
}
Output -
x+y (Addition) = 14
x-y (Subtraction) = 6
x*y (Multiplication) = 40
x/y (Divide)= 2
x%y (Modulus)= 2
उपर दिये गए C program में आप देख सकते हैं की किस तरह से हमने arithmetic operators का उपयोग करके 2 numbers का addition,subtraction,divide और modulus प्राप्त किया जो की mathematical logics हैं ।
C language में Arithmetic operators भी दो प्रकार के होते हैं ।
Unary Operators –
Binary Operators –
Unary और binary operators के बारे में हमने C tokens के tutorial में भी समझा था ।
Unary operators in C -
ऐसे operators जिनको ऑपरेशन परफॉर्म करने के लिए केवल एक ही Operand की जरुरत होती है उसे Unary Operators कहते है | उदाहरण के लिए increment और decrement ऑपरेटर में ऑपरेशन परफॉर्म करने के लिए केवल एक ही Operands की जरुरत होती है | increment operator (++), decrement operator (–) और sizeof, (type)* आदि Unary Operators है |
Increment Operator in C
Increment operator ++ वह operator होता हैं जिसके द्वारा variable की वैल्यू में 1 का increment होता हैं साधारण भाषा में समझे तो यदि x एक variable हैं जिसकी initial value x=4; हैं यदि हम x operand पर ++x increment operator लगाए तो x की value में 1 का increment हो जाता हैं तो अब x की value 5 होगी ।
यहाँ पर हमने single operand का उपयोग किया जो की x हैं क्यूकी यह unary operator हैं जो की एक ही operand पर काम करता हैं ।
चलिये उदाहरण से समझते हैं –
#include <stdio.h>
int main()
{
int x = 4;
float y = 4.5;
printf("++x = %d \n", ++x);
printf("++y = %f \n", ++y);
return 0;
}
Output –
++x = 5
++y = 5.5
उपर दिये गए उदाहरण में हमने int data type और float data type से बने variables की initial value में ++ increment operator के द्वारा 1 का increment किया जिसके कारण x की वैल्यू 4 थी और increment operator का उपयोग करने से 5 हो गयी ऐसे ही y की value 4.5 से 5.5 हो गयी । इस तरह से C language में increment ++ operator का उपयोग किया जाता हैं ।
Decrement Operator in C language
Decrement operator -- वह operator होता हैं जिसके द्वारा variable की वैल्यू में 1 का decrement होता हैं साधारण भाषा में समझे तो यदि x एक variable हैं जिसकी initial value x=7; हैं यदि हम x operand पर --x decrement operator लगाए तो x की value में 1 का decrement हो जाता हैं तो अब x की value 6 होगी ।
चलिये उदाहरण से समझते हैं –
#include <stdio.h>
int main()
{
int x = 7;
float y = 9.5;
printf("--x = %d \n", --x);
printf("--y = %f \n", --y);
return 0;
}
Output-
--x = 6
--y = 8.5
Sizeof operator in C language
Sizeof operator एक ऐसा unary operator हैं जिसके द्वारा variables और constant की data का size प्राप्त किया जाता हैं।
साधारण हिन्दी भाषा में समझे तो sizeof के द्वारा सभी data type के variables और constant की data size प्राप्त करने के लिए उपयोग किया जाता हैं जो की नीचे दिये गए उदाहरण से समझ सकते हैं ।
#include <stdio.h>
int main()
{
int x;
float y;
double z;
char c;
printf("Size of int=%lu bytes\n",sizeof(x));
printf("Size of float=%lu bytes\n",sizeof(y));
printf("Size of double=%lu bytes\n",sizeof(z));
printf("Size of char=%lu byte\n",sizeof(c));
return 0;
}
Output –
Size of int=4 bytes
Size of float=4 bytes
Size of double=8 bytes
Size of char=1 byte
उपर दिये गए उदाहरण में आप देख सकते हैं की किस तरह से अलग अलग data टाइप से बने variables का data size प्राप्त होता हैं ।
Unary operators में address operator & और indirectional operator * भी आते हैं जिनके बारे में हम एक tutorial में सीख चुके हैं ।
Binary Operators in C
Unary operators में आपने देखा की हम एक ही operand के साथ लॉजिक बना रहे थे पर binary operators में हमे एक से ज्यादा operands की जरूरत पड़ती हैं । उन्हे ही ही हम binary operators कहते हैं ।
Relational Operators in C Language –
Relational operators का उपयोग किन्ही दो operands के बीच तुलना करने के लिए किया जाता हैं । Relational operators दो operands के बीच relation चेक करने के लिए उपयोग में लाये जाते हैं जैसे- कोनसा operand बड़ा हैं (x>y) कोनसा operand छोटा हैं (x<y) कोनसा operand बराबर हैं (x==y) या कोनसा operand छोटा भी हैं और बराबर भी हैं (x<=y) या कोनसा operand बड़ा भी हैं और बराबर भी हैं (x>=y) आदि इसी तरह के logic या कह सकते conditions बनाने के लिए relational operators का उपयोग C programming में किया जाता हैं ।
Operator |
Meaning |
Example |
== |
Equal to |
7 == 4 is evaluated to 0 (False) |
> |
Greater than |
7 > 4 is evaluated to 1 (True) |
< |
Less than |
7 < 4 is evaluated to 0 (False) |
!= |
Not equal to |
7 != 4 is evaluated to 1 (True) |
>= |
Greater than or equal to |
7 >= 4 is evaluated to 1 (True) |
<= |
Less than or equal to |
7 <= 4 is evaluated to 0 (False) |
उपर दी गयी table में आप सभी relational operators और उनका क्या मतलब हैं देख सकते हैं साथ ही साथ उदाहरण भी हैं । यहाँ पर 0 false condition होती हैं और 1 true condition होती हैं ।
चलिये एक उदाहरण से समझते हैं –
#include <stdio.h>
void main()
{
int x = 7, y = 4;
int a = 8, b = 9;
if(x>y)
{
printf("X is greater than y .");
}
else
{
printf("Y is greater than X .");
}
if(a==b)
{
printf("a equal to b .");
}
else {
printf("na is not equal to b .");
}
}
Output-
X is greater than y .
a is not equal to b .
उपर दिये गए उदाहरण में हमने relational operators का उपयोग किया साथ ही साथ यहाँ पर if else statement के द्वारा condition बनाई ।
int x = 7, y = 4;
x>y
यहाँ पर x , y से बड़ा हैं तो condition true हो जाती हैं और पहली statement execute हो जाती हैं compiler else part पर नही जाएगा क्यूकी उसको पहली condition true 1 मिल चुकी हैं ।
int a = 8, b = 9;
a==b
इसमे a variable b के बराबर नही हैं तो condition तो false हो तो compiler else statement को execute कर देता हैं ।
इस तरह से variables की value को बदलके भी आप देख सकते हैं की कोनसी statement execute होती हैं ।
इस तरह से C program में सभी relational operators का उपयोग कर सकते हैं ।
Assignment Operators in C Programming –
Assigment operators का उपयोग किसी variable में वैल्यू assign करने के लिए किया जाता हैं । Assignment एक binary ऑपरेटर हैं । लेफ्ट साइड का ऑपरेंड वेरिएबल होता है जिसमे वैल्यू को असाइन किया जाता है और राइट साइड के ओपेरंडस में constant, variable और expression में से कोई भी हो सकता है |
सबसे common assignment operator = हैं ।
Operator |
Example |
Same as |
= |
x = y |
x = y |
+= |
x += y |
x = x+y |
-= |
x -= y |
x = x-y |
*= |
x*= y |
x = x*y |
/= |
x /= y |
x = x/y |
%= |
x %= y |
x= x%y |
उपर दी गयी table में आप सभी assignment operators और उनके उदाहरण देख सकते हैं ।
जैसे-
x+=y;
इसे हम कुछ इस प्रकार से भी लिख सकते हैं –
x=x+y;
चलिये उदाहरण से समझते हैं –
#include <stdio.h>
int main()
{
int x = 4, y;
y = x; // Y की value 4 हो gyi
printf("y = %d\n", y);
y += x; // y=y+x; y=4+4; aise ही aage
printf("y = %d\n", y);
y -= x;
printf("y = %d\n", y);
y *= x;
printf("y = %d\n", y);
y /= x;
printf("y = %d\n", y);
y %= x;
printf("y = %d\n", y);
return 0;
}
Output -
y = 4
y = 8
y = 4
y = 16
y = 4
y = 0
उपर दिये गए उदाहरण में सबसे पहले x को declare किया और साथ में initial value 4 assign की और y variable को खाली declare किया कोई भी वैल्यू assign नही की उसके बाद assignment operators का उपयोग किया ।
y = x; :- यहाँ पर y में x की वैल्यू store की गयी इसका मतलब अब y की value 4 हो गयी हैं ।
y += x; :- इसे समझने के लिए कुछ इस तरह से भी लिख सकते है y=y+x; पिछले स्टेप में आपने देखा था y की वैल्यू 4 हो गयी थी मतलब y के पास अब 4 वैल्यू हैं अब इस स्टेप में y=4+4 ; तो अब y की वैल्यू 8 होगी ।
ऐसे ही आगे process चलता हैं –
y -= x; और y=y-x; y=8-4=4; अब y=4;
y *= x; और y=y*x; y=4*4= 16 अब y =16;
y /= x; और y=y/x; y=16/4=4 अब y=4;
y%=x; और y=y%x; y=4/4=0 अब y=0
इस तरह से आप देख सकते हैं की y में किस किस तरह से वैल्यू में बदलाव हुआ हैं जो की assignment operators के द्वारा हुआ हैं ।
इस तरह से C language में assignment operatos का उपयोग किया जाता हैं ।
Logical Operators in C language
C programming language में logical operators का उपयोग दो या दो से अधिक conditions को एक साथ बनाने के लिए किया जाता हैं ।
साधारण हिन्दी भाषा में समझे तो जैसे daily life में आप planning करते हैं की अगर मेरे पास 20,000 rupees बचते हैं तो मे ये ख़रीदूँगा / खरीदूँगी और अगर मेरे पास 15000 rupes होते हैं तो कुछ उससे सस्ता खरीदेंगे तो यहाँ पर 2 कंडिशन आजती हैं । इसी तरह programming में भी बहुत शारी conditions बनानी पड़ती हैं ताकि हमारा प्रोग्राम हमारे according जिस तरह हमने control किया हैं उस तरह ही काम करे ।
Logical operators का उपयोग descision making के लिए किया जाता हैं की आपको अपने प्रोग्राम से कब और क्या कराना हैं उसका decision बनाए उसकी कंडिशन बनाए तब वहाँ पर logcal operators का उपयोग किया जाता हैं ।
Operator |
Meaning |
Example |
&& |
Logical AND. यह true तब होगा जब सभी expression true होगी |
If x = 7 and y = 6 then, expression ((x==7) && (y>5)) equals to 1. |
|| |
Logical OR. यह तब true होगा जब कोई भी एक condition (expression) true होता हैं |
If x = 7 and y = 6 then, expression ((x==4) || (y>7)) equals to 0. |
! |
Logical NOT. यह तब true होगा जब expression (condition) 0 होगी |
If x = 12 then, expression !(x==14) equals to 0. |
चलिये एक उदाहरण से समझते हैं ।
#include <stdio.h>
void main()
{
int x = 7, y = 6;
if((x==7) && (y>5))
{
printf("x is equal to 7 AND y is greater than 5 ( true 1 )\n");
}
else
{
printf("x is not equal to 7 AND y is not greater than 5 ( false 0 ) \n");
}
// Next Condition
if((x==4) || (y>7))
{
printf("This condition is evaluated to be true 1\n");
}
else
{
printf("This condition is evaluated to be false 0 \n");
}
// Next Condition
if(!(x==14))
{
printf("x is not equal to 14 (True 1) \n");
}
else
{
printf(" X is not equal to 14 (false 0) \n");
}
}
Output –
x is equal to 7 AND y is greater than 5 ( true 1 )
This condition is evaluated to be false 0
x is not equal to 14 (True 1)
उपर दिये गए उदाहरण में आप देख सकते हैं की किस तरह से C program में if condition का उपयग करके logical operators का उपयोग किया हैं ।
1. पहली condition में (x==7) && (y>5) x की वैल्यू 7 के बराबर हैं और y की वैल्यू 5 से बड़ी है । यहाँ पर && logical operator का उपयोग किया हैं तो दोनों expression true होने चाइए तभी if block execute होगा यदि एक true और एक fasle तब else block execute होगा यहाँ पर int x = 7, y = 6; जिससे यह true होगी क्यूकी x की वैल्यू 7 के बराबर हैं और y की वैल्यू 5 से बड़ी हैं ।
2. || OR operator का उपयोग करने पर यदि एक expression true हैं एक false हैं तब भी यह condition true होगा क्यूकी यहाँ पर OR || operator लगा हैं । इसी वजह से x की वैल्यू 4 के बराबर नही हैं क्यूकी x वैल्यू 7 हैं और न ही y की वैल्यू 7 से बड़ी हैं क्यूकी y की value 6 तो दोनों expression ही false तो else block execute होगा ।
3. X की वैल्यू 14 के बराबर नही हैं तो condition यहाँ पर true होती हैं क्यूकी यहाँ पर ! NOT logical operator लगा हैं ।
यहाँ पर if else stataments का उपयोग किया हैं ।
इस तरह से C programming language में logical operators का उपयोग decision making में किया जाता हैं ।
Bitwise Operator in C Programming
C programming में कुछ ऐसे operators भी होते हैं जो identifiers की bit पर काम कर सकते हैं जिनका उपयोग bit level पर टास्क perform करने के लिए किया जाता हैं ।
साधारण हिन्दी भाषा में समझे तो गणना (computation) के दौरान, mathematics operators जैसे: जोड़ (Addition), घटा (Subtraction), गुणा(Multiplication), भाग (Divide), आदि को bit level में बदल दिया जाता है जो processing को तेज करता हैं और बिजली की बचत भी करता हैं ।
C programming में 6 प्रकार के bitwise operators हैं जिन्हे नीचे दी गयी तालिका मे देखा जा सकता हैं ।
Operators |
Meaning |
& |
Bitwise AND |
| |
Bitwise OR |
^ |
Bitwise exclusive OR |
~ |
Bitwise complement |
<< |
Shift left |
>> |
Shift right |
C programming Bitwise operator के उपयोग को अच्छे से समझने के लिए हमने एक tutorial bitwaise operators पर बनाया हैं जहां से आप C bitwise operators को समझ सकते हैं ।
Misc & Other Operators In C language –
उपर हमने सभी categorised operators के बारे में सीखा इनके अलावा C language में अन्य operators भी होते हैं ।
Comma Operator in C language –
Comma operator (,) का उपयोग अलग अलाग related expressions को एक साथ लिखने के लिए किया जाता हैं । C programming में comma operator का उपयोग program को बनाते समय variables के बीच जब variables को डिक्लैर किया जा रहा हो या जब कोई दो या दो से अधिक expressions को एक साथ लिखना हो तब किया जाता हैं । comma
Operator एक binary ऑपरेटर हैं ।
जैसे –
Int x,y,z;
Int x=20, y=30, z;
Float x,y,z;
या
#include <stdio.h>
void main() {
int id=5,age=20;
printf("User id = %d and age = %d ",id,age);
}
Output -
User id = 5 and age = 20
इस तरह से दो या दो से अधिक expressions को एक साथ लिखने के लिए variable की वैल्यू प्रिंट करने के लिए comma operator का उपयोग किया जाता हैं ।
अन्य operators में sizeof operator भी आता हैं जिसके बारे में उपर unary operators में सीख चुके है।
Ternary Operator in C language | Conditional operator –
C programming में जिस operator को काम करने के लिए तीन operands चाइए होते हैं उन्हे ternary operator कहा जाता हैं । इसे "? द्वारा दर्शाया जा सकता है ? : " ternary operator को conditional operator भी कहते हैं । Ternary operator performance अच्छी करता हैं और बहुत शारी कोड़ line को भी कम करता हैं जिससे code कम और perfromance अच्छी हो जाती हैं ।
C programming में Conditional operator या ternary operator का syntax –
Expression1 ? Expression2 : Expression3
Example -
#include <stdio.h>
int main()
{
int x = 12, y ;
y = ( x == 12 ? 2 : 0 ) ;
printf("value of x is %d\n",x);
printf("value of y is %d",y);
return 0;
}
Output –
value of x is 12
value of y is 2
उपर दिये गए उदाहरण में जब expression 1 x == 12 true हैं क्यूकी x की वैल्यू 12 ही हैं तो y variable में second expression में जो value हैं वह store होगी ओर वो ही प्रिंट होगी इस तरह से यदि पहला expression true होता हैं तो दूसरे expression की वैल्यू display होगी ओर यदि पहला expression false हैं तो तीसरे expression की वैल्यू display होगी ।
#include <stdio.h>
int main()
{
int x = 12, y ;
y = ( x == 1 ? 2 : 0 ) ;
printf("value of x is %d\n",x);
printf("value of y is %d",y);
return 0;
}
Output –
value of x is 12
value of y is 0
इस उदाहरण में y की वैल्यू 0 होती हैं क्यूकी यहाँ पर पहला expression evaluation x == 1 false 0 होता हैं क्यूकी x की वैल्यू 12 हैं जो की 1 के बराबर नही हैं तो दूसरे expression की नही बल्कि तीसरे expression की वैल्यू y में store होकर डिस्प्ले होती हैं जो की 0 हैं ।
इस तरह से C programming में operators की बहुत जरूरत पड़ती है और अलग अलाग जरूरतों के आधार पर C langauge में operators का उपोग किया जाता हैं ।
Recommended Posts:-