+ 6
How to use boolean function in c?
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
+ 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
+ 5
Then what is the use of stdbool. h statement
+ 5
Thank you
+ 5
You're welcome, happy coding 👍
+ 1
Okok
0
😋😋