Miscellaneous Operators in C Language with Examples (C भाषा में विविध ऑपरेटर्स)
C language provides several operators that perform special operations and do not belong to the common categories of arithmetic, relational, logical, bitwise, or assignment operators. These are commonly discussed as miscellaneous or special operators.
C भाषा में कुछ ऐसे Operators भी होते हैं जो विशेष प्रकार की संक्रियाएँ करते हैं तथा सामान्य Arithmetic, Relational, Logical, Bitwise और Assignment Operators की श्रेणी में नहीं आते हैं। इन्हें सामान्यतः Miscellaneous या Special Operators कहा जाता है।
Some important miscellaneous operators and related operators in C are:
sizeof– Size Operator&– Address-of Operator*– Pointer Dereference Operator,– Comma Operator.– Structure Member Access Operator->– Structure Pointer Member Access Operator[]– Array Subscript Operator()– Function Call Operator(type)– Type Cast Operator
1. Sizeof Operator (sizeof)
The sizeof operator returns the size of a variable or data type in bytes. The size depends on the implementation and the data type.
sizeof Operator किसी Variable या Data Type का Size Bytes में बताता है। इसका Size Compiler, System और Data Type के अनुसार अलग-अलग हो सकता है।
Syntax:
sizeof(variable);sizeof(data_type);
Example:
#include <stdio.h>int main(void){int a = 10;printf("Size of a = %zu bytes\n", sizeof(a));printf("Size of int = %zu bytes\n", sizeof(int));printf("Size of char = %zu bytes\n", sizeof(char));return 0;}
Possible Output:
Size of a = 4 bytesSize of int = 4 bytesSize of char = 1 bytes
Explanation (व्याख्या):
sizeof(a)returns the size of variablea.sizeof(int)returns the size of theintdata type.sizeof(char)always returns 1 byte in C.sizeof(a)Variableaका Size बताता है।sizeof(int)Integer Data Type का Size बताता है।C भाषा में
sizeof(char)का परिणाम हमेशा 1 Byte होता है।
Important Note: sizeof is an operator, not a function. The %zu format specifier is used to print the result of sizeof because its result has type size_t.
महत्वपूर्ण: sizeof एक Operator है, Function नहीं। इसके परिणाम को Print करने के लिए %zu Format Specifier का उपयोग किया जाता है क्योंकि इसका परिणाम size_t Type का होता है।
2. Address-of Operator (&)
The address-of operator (&) returns the memory address of a variable. It is commonly used with pointers.
Address-of Operator (&) किसी Variable का Memory Address प्रदान करता है। इसका उपयोग मुख्य रूप से Pointers के साथ किया जाता है।
Syntax:
&variable;Example:
#include <stdio.h>int main(void){int a = 10;printf("Value of a = %d\n", a);printf("Address of a = %p\n", (void *)&a);return 0;}
Possible Output:
Value of a = 10Address of a = 0x7ffd1234
Note: The memory address shown in the output is only an example. The actual address will vary from system to system.
Explanation (व्याख्या):
arepresents the value stored in the variable.&arepresents the memory address of variablea.%pis used to display a pointer address.aVariable में Stored Value को दर्शाता है।&aVariableaके Memory Address को दर्शाता है।Pointer Address को Print करने के लिए
%pका उपयोग किया जाता है।
3. Pointer Dereference Operator (*)
The dereference operator (*) is used to access the value stored at the memory address held by a pointer. It is also called the Indirection Operator.
Dereference Operator (*) का उपयोग Pointer में Stored Memory Address पर उपस्थित Value को प्राप्त करने के लिए किया जाता है। इसे Indirection Operator भी कहा जाता है।
Example:
#include <stdio.h>int main(void){int a = 10;int *p;p = &a;printf("Value of a = %d\n", a);printf("Address stored in p = %p\n", (void *)p);printf("Value using pointer = %d\n", *p);return 0;}
Possible Output:
Value of a = 10Address stored in p = 0x7ffd1234Value using pointer = 10
Explanation (व्याख्या):
int *pdeclares a pointer variablep.p = &astores the address of variableain pointerp.*paccesses the value stored at that address.int *pएक Pointer VariablepDeclare करता है।p = &aVariableaका Address Pointerpमें Store करता है।*pउस Address पर Stored Value को प्राप्त करता है।
Important Note: The * symbol has two common uses in C:
In a declaration,
int *p;declares a pointer.In an expression,
*pdereferences the pointer.
महत्वपूर्ण: C भाषा में * Symbol का उपयोग दो प्रकार से होता है:
Declaration में
int *p;Pointer Declare करता है।Expression में
*pPointer के Address पर Stored Value प्राप्त करता है।
4. Comma Operator (,)
The comma operator (,) evaluates multiple expressions from left to right. The value of the complete comma expression is the value of its rightmost expression.
Comma Operator (,) एक से अधिक Expressions का मूल्यांकन बाएँ से दाएँ करता है। पूरे Comma Expression का परिणाम सबसे दाईं ओर उपस्थित Expression का मान होता है।
Example:
#include <stdio.h>int main(void){int a, b, c;c = (a = 10, b = 20, a + b);printf("Value of a = %d\n", a);printf("Value of b = %d\n", b);printf("Value of c = %d\n", c);return 0;}
Output:
Value of a = 10Value of b = 20Value of c = 30
Explanation (व्याख्या):
First, a = 10 is evaluated, then b = 20, and finally a + b is evaluated. The final result 30 is assigned to c.
सबसे पहले a = 10, उसके बाद b = 20 तथा अंत में a + b का मूल्यांकन होता है। अंतिम परिणाम 30, Variable c को Assign किया जाता है।
Important Note: A comma used to separate function arguments or variable declarations is not necessarily the comma operator. For example, int a, b; uses a separator, not a comma expression.
महत्वपूर्ण: Function Arguments या Variable Declarations को अलग करने के लिए प्रयुक्त Comma हमेशा Comma Operator नहीं होता। उदाहरण के लिए int a, b; में Comma केवल Separator का कार्य करता है।
5. Structure Member Access Operator (.)
The dot (.) operator is used to access a member of a structure variable.
Dot (.) Operator का उपयोग Structure Variable के किसी Member को Access करने के लिए किया जाता है।
Example:
#include <stdio.h>struct student{int rollno;float marks;};int main(void){struct student s;s.rollno = 101;s.marks = 85.5f;printf("Roll Number = %d\n", s.rollno);printf("Marks = %.2f\n", s.marks);return 0;}
Output:
Roll Number = 101Marks = 85.50
Explanation (व्याख्या):
Here, s.rollno accesses the rollno member and s.marks accesses the marks member of structure variable s.
यहाँ s.rollno, Structure Variable s के rollno Member को तथा s.marks, marks Member को Access करता है।
6. Structure Pointer Member Access Operator (->)
The arrow (->) operator is used to access a structure member through a pointer to that structure.
Arrow (->) Operator का उपयोग Structure के Pointer की सहायता से उसके Member को Access करने के लिए किया जाता है।
Example:
#include <stdio.h>struct student{int rollno;float marks;};int main(void){struct student s = {101, 85.5f};struct student *p = &s;printf("Roll Number = %d\n", p->rollno);printf("Marks = %.2f\n", p->marks);return 0;}
Output:
Roll Number = 101Marks = 85.50
Explanation (व्याख्या):
Here, pointer p stores the address of structure variable s. The expression p->rollno accesses the rollno member through the pointer.
यहाँ Pointer p, Structure Variable s का Address Store करता है। Expression p->rollno Pointer की सहायता से rollno Member को Access करता है।
Important Note:
p->rollnois equivalent to:
(*p).rollno7. Array Subscript Operator ([])
The subscript operator ([]) is used to access individual elements of an array using their index.
Subscript Operator ([]) का उपयोग Array के Elements को उनके Index Number के आधार पर Access करने के लिए किया जाता है।
Example:
#include <stdio.h>int main(void){int a[3] = {10, 20, 30};printf("First Element = %d\n", a[0]);printf("Second Element = %d\n", a[1]);printf("Third Element = %d\n", a[2]);return 0;}
Output:
First Element = 10Second Element = 20Third Element = 30
Explanation (व्याख्या):
Array indexing in C starts from 0. Therefore, a[0] accesses the first element, a[1] the second element, and a[2] the third element.
C भाषा में Array Indexing की शुरुआत 0 से होती है। इसलिए a[0] पहले Element, a[1] दूसरे Element तथा a[2] तीसरे Element को Access करता है।
8. Function Call Operator ()
Parentheses () are used to call a function and pass arguments to it.
Parentheses () का उपयोग किसी Function को Call करने तथा उसमें Arguments Pass करने के लिए किया जाता है।
Example:
#include <stdio.h>int add(int a, int b){return a + b;}int main(void){int result;result = add(10, 20);printf("Sum = %d\n", result);return 0;}
Output:
Sum = 30Explanation (व्याख्या):
Here, add(10, 20) calls the function add and passes two arguments, 10 and 20. The function returns their sum.
यहाँ add(10, 20), Function add को Call करता है तथा उसमें दो Arguments 10 और 20 Pass करता है। Function इनका योग Return करता है।
9. Type Cast Operator ((type))
The type cast operator is used to explicitly convert a value from one data type to another data type.
Type Cast Operator का उपयोग किसी Value को स्पष्ट रूप से एक Data Type से दूसरे Data Type में Convert करने के लिए किया जाता है।
Syntax:
(data_type) expression;Example:
#include <stdio.h>int main(void){int a = 5, b = 2;float result;result = (float)a / b;printf("Result = %.2f\n", result);return 0;}
Output:
Result = 2.50Explanation (व्याख्या):
The expression (float)a converts the integer value of a into a floating-point value before division. Therefore, the result includes the fractional part.
Expression (float)a, Division से पहले Variable a के Integer Value को Floating-Point Value में Convert करता है। इसलिए परिणाम में दशमलव भाग भी प्राप्त होता है।
Important Points About Miscellaneous Operators (महत्वपूर्ण बिंदु)
sizeofreturns the size of a data type or object in bytes.sizeofकिसी Data Type या Object का Size Bytes में बताता है।&returns the address of a variable, while*can be used to access the value through a pointer.&किसी Variable का Address देता है, जबकि*Pointer के माध्यम से उस Address पर Stored Value प्राप्त करता है।The
.operator accesses structure members through a structure variable, whereas->accesses them through a structure pointer..Operator Structure Variable के माध्यम से तथा->Operator Structure Pointer के माध्यम से Members को Access करता है।Array indexing starts from 0 in C.
C भाषा में Array Indexing की शुरुआत 0 से होती है।
The comma operator evaluates expressions from left to right and produces the value of the rightmost expression.
Comma Operator Expressions का मूल्यांकन बाएँ से दाएँ करता है तथा सबसे दाईं ओर के Expression का परिणाम देता है।
Miscellaneous operators perform several special operations in C programming. They are useful for working with memory addresses, pointers, structures, arrays, functions, data type conversion, and object sizes. Understanding these operators is important for developing a strong foundation in C programming.
Miscellaneous Operators C Programming में कई विशेष कार्य करते हैं। इनका उपयोग Memory Addresses, Pointers, Structures, Arrays, Functions, Data Type Conversion तथा Variables के Size से संबंधित कार्यों में किया जाता है। C Programming की अच्छी समझ विकसित करने के लिए इन Operators का ज्ञान आवश्यक है।
Comments
Post a Comment