+ 6

How to use boolean function in c?

3rd Apr 2019, 5:22 PM
Vicky
+ 14
NOTE: There is NO Boolean type in C!! Logical operators are usually used with conditional statements. TheΒ  two basic logical operators are: Β  &&Β  for logical AND,Β  | |Β  for logical OR. BewareΒ  &Β  andΒ  |Β  have a differentΒ  meaning for bitwise AND and OR. Logical operations which exist in C: == ,Β  != ,Β  | | ,Β  &&. [ Edited: ] Aruna, Here is an example of boolean as a subtype of int: https://code.sololearn.com/cIPbr4lmrEK9/?ref=app
3rd Apr 2019, 7:52 PM
Danijel IvanoviΔ
+ 6
The stdbool.h header file provides support for bool type in C language (which doesn't by default). However, a function returning int type value of either 1 or 0 is commonly used as well; considering C language assumes a zero to be false and anything nonzero to be true. Read the following link for more comprehensive explanation: https://stackoverflow.com/questions/1921539/using-boolean-values-in-c And here's a little sample using bool type with stdbool.h header, as well a function returning int as boolean: #include <stdio.h> #include <stdbool.h> int is_even(int n) { return n % 2 == 0; } int main() { bool yes = true; bool no = false; if(yes) puts("Yes"); if(!no) puts("No"); if(is_even(2)) printf("2 is even number\n"); if(!is_even(3)) printf("3 is odd number\n"); } Hth, cmiiw
4th Apr 2019, 6:49 AM
Ipang
3rd Apr 2019, 6:28 PM
// ΞΞ΅ΟΞΏ
+ 5
Then what is the use of stdbool. h statement
4th Apr 2019, 5:53 AM
Vicky
+ 5
Thank you
4th Apr 2019, 7:14 AM
Vicky
+ 5
You're welcome, happy coding π
4th Apr 2019, 7:15 AM
Ipang
+ 1
Okok
25th Apr 2019, 12:41 PM
Vicky
0
ππ
14th Feb 2021, 1:31 PM
ββͺββͺββͺrootπβ¬β