← Back to C Course | Chapter 3: Operators | Lesson 8 of 9

C का Ternary Operator

Ternary operator एक question के आधार पर दो answers में से चुनने का एक one-line तरीका है, जैसे 'अगर बारिश हो तो umbrella ले लो, नहीं तो sunglasses'। यह एक short if-else है।
Syntax
c
variable = (condition) ? value_if_true : value_if_false;

Ternary Operator क्या है?

Ternary operator condition ? valueIfTrue : valueIfFalse syntax इस्तेमाल करके एक simple if-else को एक expression में condense करता है, जो खासकर तब handy है जब आपको statements के अलग blocks चलाने की बजाय सिर्फ दो values में से एक चुनना हो।

उदाहरण: What is the Ternary Operator?

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare age and set it to 20
	int age = 20;
	char *status = (age >= 18) ? "Adult" : "Minor";
	// Print formatted text to the screen
	printf("%s", status);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Maximum ढूंढना

दो numbers में से बड़ा ढूंढना एक line में max = (a > b) ? a : b; की तरह लिखा जा सकता है, दो values के बीच fundamentally simple choice के लिए एक पूरा four-line if-else block बनाने से बचते हुए।

उदाहरण: Finding the Maximum

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	int a = 7, b = 12;
	// Declare max and set it to (a > b) ? a : b
	int max = (a > b) ? a : b;
	// Print formatted text to the screen
	printf("%d", max);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Pass या Fail Check

pass/fail जैसे binary outcomes के लिए, ternary operator आपको सीधे एक result compute करने देता है: result = (score >= 50) ? "Pass" : "Fail";, logic को एक single assignment में compact और readable रखते हुए।

उदाहरण: Pass or Fail Check

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare score and set it to 65
	int score = 65;
	char *result = (score >= 50) ? "Pass" : "Fail";
	// Print formatted text to the screen
	printf("%s", result);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Even या Odd

n % 2 == 0 को ternary operator के साथ combine करना आपको एक number को even या odd के तौर पर एक single expression में classify करने देता है, जैसे label = (n % 2 == 0) ? "Even" : "Odd";, बिना एक अलग if statement के।

उदाहरण: Even or Odd

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to 7
	int n = 7;
	char *label = (n % 2 == 0) ? "Even" : "Odd";
	// Print formatted text to the screen
	printf("%s", label);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Nested Ternary

आप दो से ज़्यादा outcomes handle करने के लिए ternary operators nest कर सकते हैं, लेकिन nesting का हर level expression को एक नज़र में पढ़ना मुश्किल बना देता है -- दो conditions के बाद, एक regular if-else ladder आमतौर पर ज़्यादा clear होता है।

उदाहरण: Nested Ternary

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare score and set it to 75
	int score = 75;
	char *grade = (score >= 90) ? "A" : (score >= 70) ? "B" : "C";
	// Print formatted text to the screen
	printf("%s", grade);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. कई ?: operators को इतना गहरा nest करना कि code if-else chain के मुकाबले unreadable हो जाए।
  2. a + b > c ? x : y जैसे बड़े expression में parentheses भूल जाना, जहाँ precedence एक unexpected grouping देती है।
  3. अलग types produce करने वाली branches इस्तेमाल करना, जैसे एक string और एक int, जो compile नहीं होता या warnings देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.