Operators in C Language (C लैंग्वेज में ऑपरेटर्स)
Introduction to Operators (ऑपरेटर्स का परिचय)
An operator is a special symbol that performs a specific operation on one or more operands. Operators are used to perform mathematical, relational, logical, bitwise, and other operations in C language. An operand may be a variable, constant, or expression on which an operator performs an operation.
ऑपरेटर एक विशेष चिन्ह होता है, जो एक या एक से अधिक ऑपरेंड पर किसी विशेष प्रकार की क्रिया करता है। C लैंग्वेज में ऑपरेटर्स का उपयोग गणितीय, तुलनात्मक, तार्किक, बिटवाइज तथा अन्य ऑपरेशन करने के लिए किया जाता है।
ऑपरेंड वह वैरिएबल, कॉन्स्टेंट या एक्सप्रेशन होता है जिस पर ऑपरेटर कार्य करता है।
Example:
int a = 10, b = 5;int c = a + b;
Here, + is an operator, while a and b are operands.
यहाँ + एक ऑपरेटर है तथा a और b इसके ऑपरेंड हैं।
Classification of Operators in C (C में ऑपरेटर्स का वर्गीकरण)
Operators in C language can be classified on two different bases:
On the basis of the number of operands.
On the basis of their applications or functionality.
C लैंग्वेज में ऑपरेटर्स को मुख्य रूप से दो आधारों पर वर्गीकृत किया जाता है:
ऑपरेंड की संख्या के आधार पर।
उनके उपयोग अथवा कार्य के आधार पर।
A. Types of Operators on the Basis of Number of Operands
ऑपरेंड की संख्या के आधार पर ऑपरेटर्स के प्रकार
1. Unary Operator (यूनरी ऑपरेटर)
An operator that performs an operation on a single operand is called a Unary Operator.
जो ऑपरेटर केवल एक ऑपरेंड पर कार्य करता है, उसे यूनरी ऑपरेटर कहते हैं।
Examples: Unary +, Unary -, ++, --, !, ~, sizeof, &, * (in pointer expressions).
Example 1:
#include <stdio.h>int main(void){int a = 5, b = 7;a++;--b;printf("Value of a = %d\n", a);printf("Value of b = %d\n", b);return 0;}
Output:
Value of a = 6Value of b = 6
Explanation:
a++increases the value ofaby 1.--bdecreases the value ofbby 1.
व्याख्या:
a++सेaका मान 1 बढ़ जाता है।--bसेbका मान 1 कम हो जाता है।
2. Binary Operator (बाइनरी ऑपरेटर)
An operator that performs an operation on two operands is called a Binary Operator.
जो ऑपरेटर दो ऑपरेंड पर कार्य करता है, उसे बाइनरी ऑपरेटर कहते हैं।
Examples: Binary +, Binary -, *, /, %, >, <, ==, &&, || etc.
Example:
#include <stdio.h>int main(void){int a = 5, b = 7, c;c = a + b;printf("Addition = %d\n", c);c = a - b;printf("Subtraction = %d\n", c);return 0;}
Output:
Addition = 12Subtraction = -2
Explanation:
a + badds two operands.a - bsubtracts the second operand from the first operand.
व्याख्या:
a + bदो ऑपरेंड को जोड़ता है।a - bपहले ऑपरेंड में से दूसरे ऑपरेंड को घटाता है।
Important Note: The same symbol may work as a unary or binary operator depending on its use. For example, -a uses unary minus, whereas a-b uses binary subtraction.
महत्वपूर्ण नोट: एक ही चिन्ह का उपयोग उसके प्रयोग के अनुसार यूनरी या बाइनरी ऑपरेटर के रूप में हो सकता है। उदाहरण के लिए, -a में Unary Minus तथा a-b में Binary Subtraction का प्रयोग होता है।
3. Ternary Operator (टर्नरी ऑपरेटर)
The Ternary Operator is a conditional operator that works with three operands. It is represented by ?: and is also known as the conditional operator.
टर्नरी ऑपरेटर एक कंडीशनल ऑपरेटर है, जो तीन ऑपरेंड पर कार्य करता है। इसे ?: से दर्शाया जाता है तथा इसे कंडीशनल ऑपरेटर भी कहते हैं।
It is commonly used as a short alternative to a simple if...else statement.
इसका उपयोग सामान्यतः साधारण if...else स्टेटमेंट के संक्षिप्त विकल्प के रूप में किया जाता है।
Syntax:
condition ? expression1 : expression2;If the condition is true,
expression1is evaluated.If the condition is false,
expression2is evaluated.यदि कंडीशन सत्य होती है, तो
expression1का मूल्यांकन किया जाता है।यदि कंडीशन असत्य होती है, तो
expression2का मूल्यांकन किया जाता है।
Example:
#include <stdio.h>int main(void){int x = 10, y = 20;(x > y) ?printf("%d is greater\n", x) :printf("%d is greater\n", y);return 0;}
Output:
20 is greaterExplanation:
The condition x > y is false because 10 is not greater than 20. Therefore, the expression after : is executed.
व्याख्या:
x > y कंडीशन असत्य है क्योंकि 10, 20 से बड़ा नहीं है। इसलिए : के बाद वाला एक्सप्रेशन निष्पादित होता है।
B. Types of Operators on the Basis of Applications
अनुप्रयोग के आधार पर ऑपरेटर्स के प्रकार
C language provides different operators for performing different types of operations. These are commonly classified into the following categories:
C लैंग्वेज में विभिन्न प्रकार के ऑपरेशन करने के लिए अलग-अलग ऑपरेटर्स उपलब्ध हैं। इन्हें सामान्यतः निम्नलिखित श्रेणियों में वर्गीकृत किया जाता है:
Arithmetic Operators (अंकगणितीय ऑपरेटर्स)
Relational Operators (तुलनात्मक ऑपरेटर्स)
Logical Operators (तार्किक ऑपरेटर्स)
Bitwise Operators (बिटवाइज ऑपरेटर्स)
Assignment Operators (असाइनमेंट ऑपरेटर्स)
Miscellaneous Operators (अन्य ऑपरेटर्स)
1. Arithmetic Operators (अंकगणितीय ऑपरेटर्स)
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, division, and modulus.
अंकगणितीय ऑपरेटर्स का उपयोग जोड़, घटाव, गुणा, भाग तथा शेषफल जैसी गणितीय क्रियाएँ करने के लिए किया जाता है।
Arithmetic Operators in C:
| Operator | Operation | Example |
|---|---|---|
+ | Addition (जोड़) | a + b |
- | Subtraction (घटाव) | a - b |
* | Multiplication (गुणा) | a * b |
/ | Division (भाग) | a / b |
% | Modulus (शेषफल) | a % b |
Example:
#include <stdio.h>int main(void){int a = 10, b = 3;printf("Addition = %d\n", a + b);printf("Subtraction = %d\n", a - b);printf("Multiplication = %d\n", a * b);printf("Division = %d\n", a / b);printf("Modulus = %d\n", a % b);return 0;}
Output:
Addition = 13Subtraction = 7Multiplication = 30Division = 3Modulus = 1
Explanation:
10 + 3 = 1310 - 3 = 710 * 3 = 3010 / 3 = 3(Integer division discards the fractional part.)10 % 3 = 1(Remainder is 1.)
व्याख्या:
10 + 3 = 13(जोड़)10 - 3 = 7(घटाव)10 * 3 = 30(गुणा)10 / 3 = 3(पूर्णांक भाग में दशमलव भाग हट जाता है।)10 % 3 = 1(शेषफल 1 है।)
Important Points:
The modulus operator
%is used with integer operands in C.Division by zero is not valid.
Integer division discards the fractional part.
महत्वपूर्ण बातें:
C में Modulus ऑपरेटर
%का उपयोग पूर्णांक ऑपरेंड के साथ किया जाता है।शून्य से भाग देना मान्य नहीं है।
पूर्णांक भाग में दशमलव भाग हट जाता है।
2. Relational Operators (तुलनात्मक ऑपरेटर्स)
Relational operators are used to compare two operands. The result of a relational operation is either 1 (true) or 0 (false).
रिलेशनल ऑपरेटर्स का उपयोग दो ऑपरेंड के बीच तुलना करने के लिए किया जाता है। रिलेशनल ऑपरेशन का परिणाम 1 (सत्य) या 0 (असत्य) होता है।
Relational Operators in C:
| Operator | Meaning | Example |
|---|---|---|
> | Greater than (से बड़ा) | a > b |
< | Less than (से छोटा) | a < b |
>= | Greater than or equal to (से बड़ा या बराबर) | a >= b |
<= | Less than or equal to (से छोटा या बराबर) | a <= b |
== | Equal to (बराबर) | a == b |
!= | Not equal to (बराबर नहीं) | a != b |
Example:
#include <stdio.h>int main(void){int a = 10, b = 5;printf("a > b = %d\n", a > b);printf("a < b = %d\n", a < b);printf("a >= b = %d\n", a >= b);printf("a <= b = %d\n", a <= b);printf("a == b = %d\n", a == b);printf("a != b = %d\n", a != b);return 0;}
Output:
a > b = 1a < b = 0a >= b = 1a <= b = 0a == b = 0a != b = 1
Explanation:
Since a = 10 and b = 5, the expressions a > b, a >= b, and a != b are true. The other expressions are false.
व्याख्या:
चूँकि a = 10 तथा b = 5 हैं, इसलिए a > b, a >= b तथा a != b सत्य हैं। अन्य एक्सप्रेशन असत्य हैं।
Important Note: Do not confuse = with ==.
=is an assignment operator.==is a relational operator used for comparison.
महत्वपूर्ण नोट: = तथा == में अंतर समझना आवश्यक है।
=का उपयोग मान असाइन करने के लिए होता है।==का उपयोग दो मानों की तुलना करने के लिए होता है।
3. Logical Operators (तार्किक ऑपरेटर्स)
Logical operators are used to combine or reverse conditions. They are commonly used with conditional statements such as if, if...else, and loops.
लॉजिकल ऑपरेटर्स का उपयोग एक या अधिक कंडीशन को जोड़ने अथवा किसी कंडीशन के परिणाम को उलटने के लिए किया जाता है। इनका उपयोग सामान्यतः if, if...else तथा लूप्स में किया जाता है।
Logical Operators in C:
| Operator | Name | Meaning | ||
|---|---|---|---|---|
&& | Logical AND | दोनों कंडीशन सत्य हों | ||
| ` | ` | Logical OR | कम से कम एक कंडीशन सत्य हो | |
! | Logical NOT | परिणाम को उलट देता है |
Truth Table of Logical Operators (लॉजिकल ऑपरेटर्स की ट्रुथ टेबल)
Logical AND (&&)
| A | B | A && B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
Logical OR (||)
| A | B | A || B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
Logical NOT (!)
| A | !A |
|---|---|
| 0 | 1 |
| 1 | 0 |
Example:
#include <stdio.h>int main(void){int a = 10, b = 5;printf("Logical AND = %d\n", (a > 5 && b < 10));printf("Logical OR = %d\n", (a < 5 || b < 10));printf("Logical NOT = %d\n", !(a > b));return 0;}
Output:
Logical AND = 1Logical OR = 1Logical NOT = 0
Explanation:
(a > 5 && b < 10)is true because both conditions are true.(a < 5 || b < 10)is true because the second condition is true.!(a > b)is false becausea > bis true and NOT reverses the result.
व्याख्या:
(a > 5 && b < 10)सत्य है क्योंकि दोनों कंडीशन सत्य हैं।(a < 5 || b < 10)सत्य है क्योंकि दूसरी कंडीशन सत्य है।!(a > b)असत्य है क्योंकिa > bसत्य है और NOT इसका परिणाम उलट देता है।
Important Note: Logical operators in C use short-circuit evaluation. In A && B, if A is false, B is not evaluated. In A || B, if A is true, B is not evaluated.
महत्वपूर्ण नोट: C में लॉजिकल ऑपरेटर्स Short-Circuit Evaluation का उपयोग करते हैं। A && B में यदि A असत्य है, तो B का मूल्यांकन नहीं होता। A || B में यदि A सत्य है, तो B का मूल्यांकन नहीं होता।
4. Bitwise Operators (बिटवाइज ऑपरेटर्स)
Bitwise operators perform operations on individual bits of integer operands. These operators work at the binary level.
बिटवाइज ऑपरेटर्स पूर्णांक ऑपरेंड के अलग-अलग बिट्स पर ऑपरेशन करते हैं। ये ऑपरेटर्स बाइनरी स्तर पर कार्य करते हैं।
Bitwise Operators in C:
| Operator | Name | Description |
|---|---|---|
& | Bitwise AND | दोनों बिट 1 होने पर परिणाम 1 |
| | Bitwise OR | किसी एक बिट के 1 होने पर परिणाम 1 |
^ | Bitwise XOR | दोनों बिट अलग होने पर परिणाम 1 |
~ | Bitwise NOT | प्रत्येक बिट को उलट देता है |
<< | Left Shift | बिट्स को बाईं ओर शिफ्ट करता है |
>> | Right Shift | बिट्स को दाईं ओर शिफ्ट करता है |
Example:
Consider two integer values:
a = 5 = 0101b = 3 = 0011
Bitwise AND (&)
0101& 0011------0001
Result = 1
Bitwise OR (|)
0101| 0011------0111
Result = 7
Bitwise XOR (^)
0101^ 0011------0110
Result = 6
Left Shift (<<)
5 << 1 = 10Right Shift (>>)
5 >> 1 = 2C Program:
#include <stdio.h>int main(void){unsigned int a = 5, b = 3;printf("Bitwise AND = %u\n", a & b);printf("Bitwise OR = %u\n", a | b);printf("Bitwise XOR = %u\n", a ^ b);printf("Left Shift = %u\n", a << 1);printf("Right Shift = %u\n", a >> 1);return 0;}
Output:
Bitwise AND = 1Bitwise OR = 7Bitwise XOR = 6Left Shift = 10Right Shift = 2
Explanation:
Bitwise operators perform operations on the binary representation of integer values. For example, 5 & 3 produces 1.
व्याख्या:
बिटवाइज ऑपरेटर्स पूर्णांक मानों के बाइनरी रूप पर कार्य करते हैं। उदाहरण के लिए, 5 & 3 का परिणाम 1 होता है।
Important Note: Bitwise operators should not be confused with logical operators.
&is Bitwise AND, whereas&&is Logical AND.|is Bitwise OR, whereas||is Logical OR.^is Bitwise XOR.
महत्वपूर्ण नोट: Bitwise तथा Logical ऑपरेटर्स को एक समान नहीं समझना चाहिए।
&Bitwise AND है, जबकि&&Logical AND है।|Bitwise OR है, जबकि||Logical OR है।^Bitwise XOR है।
5. Assignment Operators (असाइनमेंट ऑपरेटर्स)
Assignment operators are used to assign values to variables. The simple assignment operator is =, while compound assignment operators combine an operation with assignment.
असाइनमेंट ऑपरेटर्स का उपयोग वैरिएबल में मान असाइन करने के लिए किया जाता है। साधारण असाइनमेंट ऑपरेटर = है, जबकि Compound Assignment Operators किसी ऑपरेशन को असाइनमेंट के साथ जोड़ते हैं।
Assignment Operators in C:
| Operator | Example | Equivalent Expression |
|---|---|---|
= | a = 10 | Assigns 10 to a |
+= | a += 5 | a = a + 5 |
-= | a -= 5 | a = a - 5 |
*= | a *= 5 | a = a * 5 |
/= | a /= 5 | a = a / 5 |
%= | a %= 5 | a = a % 5 |
&= | a &= 5 | a = a & 5 |
|= | a |= 5 | a = a | 5 |
^= | a ^= 5 | a = a ^ 5 |
<<= | a <<= 1 | a = a << 1 |
>>= | a >>= 1 | a = a >> 1 |
Example:
#include <stdio.h>int main(void){int a = 10;a += 5;printf("After += : %d\n", a);a -= 3;printf("After -= : %d\n", a);a *= 2;printf("After *= : %d\n", a);a /= 4;printf("After /= : %d\n", a);a %= 3;printf("After %%= : %d\n", a);return 0;}
Output:
After += : 15After -= : 12After *= : 24After /= : 6After %= : 0
Explanation:
Initially,
a = 10.a += 5makesa = 15.a -= 3makesa = 12.a *= 2makesa = 24.a /= 4makesa = 6.a %= 3makesa = 0.
व्याख्या:
प्रारंभ में
a = 10है।a += 5सेa = 15हो जाता है।a -= 3सेa = 12हो जाता है।a *= 2सेa = 24हो जाता है।a /= 4सेa = 6हो जाता है।a %= 3सेa = 0हो जाता है।
6. Miscellaneous Operators (अन्य ऑपरेटर्स)
C language provides some additional operators that are used for specific purposes. These are commonly discussed under miscellaneous operators.
C लैंग्वेज में कुछ अन्य ऑपरेटर्स भी उपलब्ध हैं जिनका उपयोग विशेष कार्यों के लिए किया जाता है। इन्हें सामान्यतः Miscellaneous Operators के अंतर्गत समझाया जाता है।
Important Miscellaneous Operators:
| Operator | Name | Purpose |
|---|---|---|
sizeof | Size Operator | किसी डेटा टाइप या ऑब्जेक्ट का आकार बताता है |
& | Address-of Operator | वैरिएबल का मेमोरी एड्रेस प्राप्त करता है |
* | Pointer Dereference Operator | पॉइंटर द्वारा दर्शाए गए मान तक पहुँचता है |
, | Comma Operator | एक्सप्रेशन का मूल्यांकन क्रम से करता है |
. | Member Access Operator | Structure/Union के सदस्य तक पहुँचता है |
-> | Pointer Member Access Operator | Structure/Union पॉइंटर के सदस्य तक पहुँचता है |
[] | Subscript Operator | Array के तत्व तक पहुँचता है |
() | Function Call Operator | Function को कॉल करता है |
(a) sizeof Operator
The sizeof operator returns the size, in bytes, of a data type or object.
sizeof ऑपरेटर किसी डेटा टाइप या ऑब्जेक्ट का आकार बाइट्स में बताता है।
Example:
#include <stdio.h>int main(void){int a;printf("Size of int = %zu bytes\n", sizeof(int));printf("Size of variable a = %zu bytes\n", sizeof(a));return 0;}
Note: The exact size of a data type depends on the implementation and compiler.
नोट: किसी डेटा टाइप का वास्तविक आकार कंपाइलर और सिस्टम के अनुसार अलग हो सकता है।
(b) Address-of Operator (&)
The address-of operator & returns the memory address of a variable.
Address-of ऑपरेटर & किसी वैरिएबल का मेमोरी एड्रेस प्राप्त करता है।
Example:
int a = 10;int *p = &a;
Here, &a gives the address of variable a.
यहाँ &a वैरिएबल a का मेमोरी एड्रेस देता है।
(c) Pointer Dereference Operator (*)
The dereference operator * is used to access the value stored at the memory address held by a pointer.
Dereference ऑपरेटर * का उपयोग पॉइंटर में संग्रहित मेमोरी एड्रेस पर मौजूद मान को प्राप्त करने के लिए किया जाता है।
Example:
int a = 10;int *p = &a;printf("%d", *p);
Output:
10(d) Comma Operator (,)
The comma operator evaluates expressions from left to right and produces the value of the last expression.
Comma ऑपरेटर एक्सप्रेशन्स का मूल्यांकन बाएँ से दाएँ करता है तथा अंतिम एक्सप्रेशन का मान परिणाम के रूप में देता है।
Example:
#include <stdio.h>int main(void){int a;a = (5, 10, 15);printf("Value of a = %d", a);return 0;}
Output:
Value of a = 15(e) Member Access Operators (. and ->)
The dot operator . is used to access a structure or union member through a structure or union variable. The arrow operator -> is used to access a member through a pointer to a structure or union.
Dot ऑपरेटर . का उपयोग Structure या Union वैरिएबल के सदस्य तक पहुँचने के लिए किया जाता है। Arrow ऑपरेटर -> का उपयोग Structure या Union के पॉइंटर के माध्यम से सदस्य तक पहुँचने के लिए किया जाता है।
Example:
struct Student{int roll;};struct Student s = {101};struct Student *p = &s;/* s.roll accesses the member using a variable *//* p->roll accesses the member using a pointer */
(f) Subscript Operator ([])
The subscript operator [] is used to access elements of an array.
Subscript ऑपरेटर [] का उपयोग Array के तत्वों तक पहुँचने के लिए किया जाता है।
Example:
int arr[3] = {10, 20, 30};printf("%d", arr[1]);
Output:
20(g) Function Call Operator (())
Parentheses () are used to call a function and pass arguments to it.
Parentheses () का उपयोग किसी Function को कॉल करने तथा उसमें Arguments पास करने के लिए किया जाता है।
Example:
printf("Hello C");Output:
Hello CDifference Between Unary, Binary and Ternary Operators
यूनरी, बाइनरी तथा टर्नरी ऑपरेटर्स में अंतर
| Basis | Unary Operator | Binary Operator | Ternary Operator |
|---|---|---|---|
| Number of operands | One | Two | Three |
| हिंदी | एक ऑपरेंड | दो ऑपरेंड | तीन ऑपरेंड |
| Example | ++a | a + b | a > b ? a : b |
| Common use | Single operand operation | Two operand operation | Conditional expression |
Important Points for CPCT, Exams and Interviews
CPCT, परीक्षाओं एवं इंटरव्यू के लिए महत्वपूर्ण बातें
- An operator performs an operation on one or more operands.ऑपरेटर एक या अधिक ऑपरेंड पर ऑपरेशन करता है।
- Unary operators work on one operand.यूनरी ऑपरेटर एक ऑपरेंड पर कार्य करते हैं।
- Binary operators work on two operands.बाइनरी ऑपरेटर दो ऑपरेंड पर कार्य करते हैं।
- The conditional operator
?:is a ternary operator.कंडीशनल ऑपरेटर?:एक टर्नरी ऑपरेटर है। - The modulus operator
%gives the remainder.Modulus ऑपरेटर%शेषफल देता है। - Relational operators return either
1or0in C.C में रिलेशनल ऑपरेटर्स का परिणाम1या0होता है। - Logical AND is represented by
&&.Logical AND को&&से दर्शाया जाता है। - Logical OR is represented by
||.Logical OR को||से दर्शाया जाता है। - Bitwise XOR is represented by
^.Bitwise XOR को^से दर्शाया जाता है। - The
sizeofoperator returns size in bytes.sizeofऑपरेटर आकार को बाइट्स में बताता है। =is an assignment operator, whereas==is a relational operator.=असाइनमेंट ऑपरेटर है, जबकि==रिलेशनल ऑपरेटर है।++is the increment operator and--is the decrement operator.++इंक्रीमेंट तथा--डिक्रीमेंट ऑपरेटर है।
Operators are an essential part of C programming. They help programmers perform arithmetic calculations, compare values, evaluate conditions, manipulate individual bits, assign values, and access memory or data members. A proper understanding of C operators is essential for learning programming logic, conditional statements, loops, arrays, pointers, and data structures.
C प्रोग्रामिंग में ऑपरेटर्स का महत्वपूर्ण स्थान है। इनकी सहायता से गणितीय गणनाएँ, मानों की तुलना, कंडीशन का मूल्यांकन, बिट्स में बदलाव, मान असाइन करना तथा मेमोरी और डेटा मेंबर तक पहुँचना संभव होता है। C ऑपरेटर्स की अच्छी समझ Programming Logic, Conditional Statements, Loops, Arrays, Pointers तथा Data Structures सीखने के लिए आवश्यक है।
Comments
Post a Comment