+ 6

How to use boolean function in c?

3rd Apr 2019, 5:22 PM
Vicky
Vicky - avatar
8 odpowiedzi
+ 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ć
Danijel Ivanović - avatar
+ 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
program
program - avatar
+ 5
Then what is the use of stdbool. h statement
4th Apr 2019, 5:53 AM
Vicky
Vicky - avatar
+ 5
Thank you
4th Apr 2019, 7:14 AM
Vicky
Vicky - avatar
+ 5
You're welcome, happy coding 👍
4th Apr 2019, 7:15 AM
Ipang
+ 1
Okok
25th Apr 2019, 12:41 PM
Vicky
Vicky - avatar
0
😋😋
14th Feb 2021, 1:31 PM
‏‪‏‪‏‪root🙄‬‏
‏‪‏‪‏‪root🙄‬‏ - avatar