C में Booleans
In this page:
#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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
_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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Conditions में Booleans इस्तेमाल करना
एक bool variable को explicitly true से compare किए बिना सीधे एक if या while condition में इस्तेमाल किया जा सकता है -- if (isValid) लिखना if (isValid == true) से ज़्यादा clear और idiomatic है, जो redundant है और गलती से reverse होने पर गलत होना आसान है।
उदाहरण: Using Booleans in Conditions
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- पुराने C standards में
#include <stdbool.h>के बिनाboolइस्तेमाल करना, ताकि यह undeclared रहे। - यह मान लेना कि
trueऔरfalseहर C standard में keywords हैं, जब C23 से पहले वेstdbool.hके macros हैं। - एक
boolको एक non-zero value assign करना और इसे वापस पाने की उम्मीद करना, जब यह 1 बन जाता है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- C Introduction
- C History & Features
- C Environment Setup
- C First Program
- C Syntax & Structure
- C Statements
- C Comments
- C Keywords & Identifiers
- C Data Types
- C Character Data Type
- C Numeric Data Types
- C Decimal (Floating-Point) Numbers
- C sizeof Operator
- C Extended Data Types
- C Type Conversion
- C Booleans
- C Variables
- C Changing Variable Values
- C Multiple Variables
- C Constants
- C Fixed-Width Integers