← Back to C Course | Chapter 1: Introduction & Basics | Lesson 16 of 21

C में Booleans

एक boolean एक light switch की तरह है जो सिर्फ on या off, true या false हो सकता है। पुराने C में इसके लिए कोई special type नहीं था, इसलिए C अब stdbool.h में एक handy type देता है।
Syntax
c
#include <stdbool.h>

bool variable_name = true;  // or false

int flag = 1;  // classic style: 0 is false, non-zero is true

stdbool.h से पहले Booleans

Classic C में कोई dedicated boolean type नहीं है -- conditions हमेशा plain int से express होती थीं, जहाँ value 0 का मतलब false और कोई भी non-zero value का मतलब true था, एक convention जो आज भी C के हर if और loop condition के evaluate होने के पीछे है।

उदाहरण: Booleans Before stdbool.h

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare isValid and set it to 1
	int isValid = 1;
	// Check whether isValid
	if (isValid) {
		// Print formatted text to the screen
		printf("True (non-zero)");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

stdbool.h से bool Type

<stdbool.h> include करना bool type के साथ true और false macros ले आता है, जो respectively 1 और 0 में expand होते हैं -- यह same underlying integer representation पर एक thin, readable layer है, genuinely एक नया machine type नहीं।

उदाहरण: The bool Type from stdbool.h

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

_Bool असल में क्या है

bool खुद बस _Bool का एक macro है, वह असली keyword जो C99 standard ने introduce किया -- _Bool को guarantee है कि यह सिर्फ 0 या 1 store करे, इसलिए इसे 10 जैसी कोई भी non-zero value assign करना automatically 1 तक normalize हो जाता है।

उदाहरण: What _Bool Actually Is

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

Conditions में Booleans इस्तेमाल करना

एक bool variable को explicitly true से compare किए बिना सीधे एक if या while condition में इस्तेमाल किया जा सकता है -- if (isValid) लिखना if (isValid == true) से ज़्यादा clear और idiomatic है, जो redundant है और गलती से reverse होने पर गलत होना आसान है।

उदाहरण: Using Booleans in Conditions

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include the bool type
#include <stdbool.h>
// Program execution starts in main()
int main() {
	// Declare isValid and set it to true
	bool isValid = true;
	// Check whether isValid
	if (isValid) {
		// Print formatted text to the screen
		printf("Valid");
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Function Return Values के तौर पर Booleans

yes/no question का जवाब देने वाले functions int की बजाय bool return करने के लिए declared होने पर कहीं ज़्यादा clearly पढ़े जाते हैं -- bool isPrime(int n) एक नज़र में function की intent document करता है, एक int-returning function के मुकाबले जहाँ 0 बनाम 1 का मतलब infer करना पड़ता है।

उदाहरण: Booleans as Function Return Values

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include the bool type
#include <stdbool.h>
bool isPrime(int n) {
	return n == 2 || n == 3 || n == 5 || n == 7;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", isPrime(5));
	// 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. पुराने C standards में #include <stdbool.h> के बिना bool इस्तेमाल करना, ताकि यह undeclared रहे।
  2. यह मान लेना कि true और false हर C standard में keywords हैं, जब C23 से पहले वे stdbool.h के macros हैं।
  3. एक bool को एक non-zero value assign करना और इसे वापस पाने की उम्मीद करना, जब यह 1 बन जाता है।

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.