Comma operator in C language

Introduction

C programming में operators का उपयोग expressions को बनाने और values पर operations करने के लिए किया जाता है। इन operators में से एक खास operator है Comma Operator (,), जिसका उपयोग एक ही statement में multiple expressions को लिखने और execute करने के लिए किया जाता है।

आमतौर पर हम हर statement को अलग-अलग लिखते हैं, लेकिन कुछ situations में हमें एक ही line में कई operations perform करने होते हैं। ऐसे cases में comma operator बहुत useful होता है।

Comma operator expressions को left से right execute करता है, लेकिन final result हमेशा last expression का होता है।

यह concept खासकर इन situations में useful होता है:

  • for loop में multiple variables handle करना
  • एक line में multiple assignments करना
  • compact expressions लिखना

यह operator simple दिखता है, लेकिन exam में इसके output-based questions अक्सर पूछे जाते हैं, इसलिए इसका behavior अच्छी तरह समझना जरूरी है।

Definition

Comma Operator (,) एक binary operator है जो multiple expressions को sequence में execute करता है और final result last expression का return करता है।

Syntax

expression1, expression2, expression3;

Important Points

  • Expressions left से right execute होते हैं
  • अंतिम (last) expression का result return होता है
  • एक ही statement में multiple operations करने के लिए use होता है
  • for loop में बहुत common है

Example 1: Multiple Assignments

#include <stdio.h>
int main()
{
int a, b;
a = (b = 5, b + 2);
printf("%d", a);
return 0;
}

Output:

7

Explanation:

  • पहले b = 5 execute हुआ
  • फिर b + 2 = 7 evaluate हुआ
  • final value 7 → a में assign हुई

Example 2: Simple Expression

#include <stdio.h>
int main()
{
int x;
x = (2, 4, 6);
printf("%d", x);
return 0;
}

Output:

6

Explanation:

  • 2 execute हुआ
  • फिर 4 execute हुआ
  • फिर 6 execute हुआ
  • last value 6 → x में assign हुई

Example 3: for Loop में Use

#include <stdio.h>
int main()
{
int i, j;
for (i = 1, j = 5; i <= 5; i++, j--)
{
printf("%d %d\n", i, j);
}
return 0;
}

Output:

1 5
2 4
3 3
4 2
5 1

Explanation:

  • i increase हो रहा है और j decrease
  • comma operator से दोनों updates एक साथ हो रहे हैं

Example 4: Expression with Output

#include <stdio.h>
int main()
{
int a = 1;
int result = (a++, a + 5);
printf("%d", result);
return 0;
}

Output:

7

Explanation:

  • पहले a++ → a = 2
  • फिर a + 5 = 7
  • last value 7 → result में assign हुई

Technical Understanding

Comma operator mainly इन situations में use होता है:

  • multiple variables update करना
  • compact code लिखना
  • for loop control में

लेकिन इसका excessive use code को confusing बना सकता है, इसलिए carefully use करना चाहिए।

Difference: Comma Operator vs Comma Separator

Comma OperatorComma Separator
Expression execute करता हैVariables को separate करता है
Value return करता हैValue return नहीं करता
Example: (a=1, a+2)Example: int a, b, c

Exam Points

  • Comma operator expressions को left to right execute करता है
  • Final result last expression का होता है
  • for loop में बहुत common है
  • Output-based questions में इसका use आता है
  • Comma operator और comma separator में difference पूछा जाता है

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top