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

C के Relational Operators

Relational operators दो values compare करते हैं, यह पूछने जैसा कि कौन लंबा है। जवाब या तो true है या false।
Syntax
c
result = value1 == value2;  // also != > < >= <=

Equal (==) और Not Equal (!=)

== test करता है कि दो values equal हैं या नहीं और != test करता है कि वे अलग हैं या नहीं; दोनों true के लिए 1 या false के लिए 0 produce करते हैं, और == को single = assignment operator से मिलाना सबसे common C bugs में से एक है।

उदाहरण: Equal (==) and Not Equal (!=)

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

Greater Than (>) और Greater Than or Equal (>=)

> और >= test करते हैं कि left operand right operand से strictly greater है, या greater than or equal है -- आमतौर पर upper bounds check करने के लिए इस्तेमाल होता है, जैसे कि क्या कोई score एक passing threshold से ज़्यादा है।

उदाहरण: Greater Than (>) and Greater Than or Equal (>=)

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;
	// Print formatted text to the screen
	printf("%d %d", score > 70, score >= 75);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Less Than (<) और Less Than or Equal (<=)

< और <= test करते हैं कि left operand right operand से strictly less है, या less than or equal है -- > और >= की mirror image, अक्सर इनके साथ pair होकर यह check करने के लिए कि कोई value एक range के अंदर आती है या नहीं।

उदाहरण: Less Than (<) and Less Than or Equal (<=)

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

Relational Outputs को समझना

C के original standard में कोई dedicated boolean type नहीं है -- relational और logical expressions बस integer 1 (true के लिए) या 0 (false के लिए) में evaluate होते हैं, यही कारण है कि आप उनके results सीधे arithmetic में या एक if condition की तरह इस्तेमाल कर सकते हैं।

उदाहरण: Understanding Relational Outputs

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

Conditional Expressions

Relational operators ही conditional logic को possible बनाते हैं -- एक C program में हर if statement, while loop condition, और validation check आखिरकार इनमें से किसी एक operator के एक true/false result produce करने पर depend करता है।

उदाहरण: Conditional Expressions

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;
	// Check whether age >= 18
	if (age >= 18) {
		// Print formatted text to the screen
		printf("Adult");
	}
	// 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. a == b की बजाय if (a = b) लिखना, जो compare करने की बजाय assign करता है।
  2. 1 < x < 10 जैसे comparisons को chain करना, जो हमेशा true है।
  3. floating-point values को == से compare करना, जहाँ rounding errors surprising results cause करती हैं।

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.