Booleans in C Language
Booleans in C Language
In the C programming language, booleans are a data type used to represent truth values. The boolean type in C is not explicitly defined, but it is commonly implemented using integers, where 0 represents false and any non-zero value represents true. The standard library in C introduced the `<stdbool.h>` header, which provides a more convenient way to work with booleans.
To use booleans in C, you typically include the `<stdbool.h>` header at the beginning of your program. This header defines the `bool` type as well as the constants `true` and `false`. Here's an example:
#include <stdbool.h>
#include <stdio.h>
int main() {
bool isTrue = true;
bool isFalse = false;
if (isTrue) {
printf("This statement is true.\n");
}
if (!isFalse) {
printf("This statement is also true.\n");
}
return 0;
}
In the above code, we include the `<stdbool.h>` header and declare two boolean variables `isTrue` and `isFalse`. We then use `if` statements to check the truth value of these variables. If the condition evaluates to true, the corresponding code block is executed.
C booleans are primarily used in control flow statements like `if` statements, `while` loops, and `for` loops to make decisions based on certain conditions. They allow you to write more expressive and readable code by directly representing true/false values.
It's important to note that C does not have a dedicated boolean output in its standard I/O functions like `printf()`. Instead, `printf()` uses integers, where `0` represents `false` and any non-zero value represents `true`. However, when reading input, you can use the `%d` format specifier to read an integer and interpret it as a boolean value.
In summary, C booleans are typically implemented using integers, with `0` representing `false` and non-zero values representing `true`. The `<stdbool.h>` header provides a more convenient way to work with booleans, defining the `bool` type and the constants `true` and `false`. Booleans are commonly used in control flow statements to make decisions based on conditions.
हिंदी:
C प्रोग्रामिंग भाषा में, बूलियन एक डेटा टाइप है जिसका उपयोग सत्य मानों को प्रदर्शित करने के लिए किया जाता है। C में बूलियन टाइप स्पष्ट रूप से परिभाषित नहीं है, लेकिन यह सामान्यतः पूर्णांकों का उपयोग करके प्रदर्शित किया जाता है, जहां 0 नकारात्मक को और 0 के बाहर के कोई भी अवैध संख्या सत्य को प्रदर्शित करती है। C में प्रमाण पुस्तकालय ने `<stdbool.h>` हेडर उपयोगी तरीके से बूलियन के साथ काम करने के लिए पेश किया है।
बूलियन का उपयोग C में करने के लिए, आप आमतौर पर अपने प्रोग्राम की शुरुआत में `<stdbool.h>` हेडर शामिल करते हैं। यह हेडर `bool` टाइप और `true` और `false` निर्धारित करता है। यहां एक उदाहरण है:
#include <stdbool.h>
#include <stdio.h>
int main() {
bool isTrue = true;
bool isFalse = false;
if (isTrue) {
printf("यह कथन सत्य है।\n");
}
if (!isFalse) {
printf("यह कथन भी सत्य है।\n");
}
return 0;
}
उपरोक्त कोड में, हम `<stdbool.h>` हेडर को शामिल करते हैं और दो बूलियन चरों `isTrue` और `isFalse` की घोषणा करते हैं। फिर हम `if` वाक्यांशों का उपयोग करके इन चरों के सत्य मान की जांच करते हैं। अगर स्थिति सत्य होती है, तो संबंधित कोड ब्लॉक को क्रियान्वयित किया जाता है।
सी बूलियन प्रायः नियंत्रण प्रवाह वाक्यों में उपयोग होते हैं जैसे कि `if` वाक्यांशों, `while` लूप और `for` लूप में निर्णय लेने के लिए। ये आपको निश्चित स्थितियों पर आधारित निर्णय लेने की अनुमति देते हैं।
यह ध्यान देने के लिए महत्वपूर्ण है कि C में इसके प्रमाणित I/O समारोहों में एक विशेष बूलियन आउटपुट नहीं है जैसे कि `printf()`। बजाय इसके, `printf()` पूर्णांकों का उपयोग करता है, जहां `0` नकारात्मक को प्रदर्शित करता है और कोई भी गैर-शून्य मान सत्य को प्रदर्शित करता है। हालांकि, इनपुट पठने के लिए, आप `%d` प्रारूप निर्देशक का उपयोग कर सकते हैं ताकि आप इसे बूलियन मान के रूप में पढ़ सकें।
संक्षेप में, सी बूलियन्स सामान्यतः पूर्णांकों का उपयोग करके अंकीय रूप में प्रदर्शित किए जाते हैं, जहां `0` नकारात्मक को प्रदर्शित करता है और गैर-शून्य मान सत्य को प्रदर्शित करता है। `<stdbool.h>` हेडर बूलियन के साथ काम करने के लिए अधिक सुविधाजनक तरीके प्रदान करता है, जिसमें `bool` टाइप और `true` और `false` निर्धारित होते हैं। बूलियन्स नियंत्रण प्रवाह वाक्यांशों में आमतौर पर उपयोग किए जाते हैं स्थितियों पर निर्णय लेने के लिए।
Post a Comment