Reputation: 1088
Here is the program that I am trying to compile
#include<stdio.h>
int main()
{
int i = 0;
rec(i);
}
int rec(int i)
{
i = i + 1;
if (i == 10){return true;}
rec(i);
printf("i: %d\n", i);
}
I am getting this output
$ gcc -o one one.c
one.c: In function ‘rec’:
one.c:10:24: error: ‘true’ undeclared (first use in this function)
one.c:10:24: note: each undeclared identifier is reported only once for each function it appears in
As far I believe is Boolean true evaluates to 1 in c. If so why am I getting an error?
Upvotes: 6
Views: 41389
Reputation: 882786
There are no true
or false
keywords in C. There are some library definitions for those values in stdbool.h
(starting in C99, I think) but oft times most C programmers will just use 1
and 0
.
If you don't want to use stdbool.h
, or you're working on a compiler that doesn't support it, you can usually define the constants yourself with something like:
#define FALSE (1==0)
#define TRUE (1==1)
or you can use 1
and 0
directly - the method above is for those who don't want to have to remember which integer values apply to which truth values (a).
0
is false, any other value is true. So your code would look something like (fixing up the return value problem as well though I'm not sure why that's even there since it always returns true.):
#include <stdio.h>
int rec (int i) {
i = i + 1;
if (i == 10)
return 1;
printf ("i: %d\n", i);
return rec (i);
}
int main (void) {
int rv, i = 0;
rv = rec (i);
printf ("rv: %d\n", rv);
return 0;
}
which gives you:
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
rv: 1
I should also mention that recursion is a solution best used when the search space is reduced quickly, such as in a binary tree search where the search spaces halves on each recursive call. It's not usually a good fit for something where you just increment a value on each call although, in this case, it's probably okay since you limit it to about ten levels.
(a): Keep in mind the caveat that, although the given definition of TRUE will most be 1, any non-zero value will be treated so. That means that the two statements:
if (isValid)
if (isValid == TRUE)
do not mean the same thing. There are a large number of possible isValid
values which will be pass the first test but fail the second. That's usually not a problem since it's almost always a bad idea to compare boolean variables with boolean constants anyway.
Upvotes: 13
Reputation: 156
If your C doesn't use the C99 standard, then there is neither true
nor false
. When designing a function that returns a boolean, i.e.
short isOdd(long);
I would use 1 and 0 to represent true
and false
respectively. This is ok because if you try the following snippet with i=1 (or any non-zero ints), it prints T; with i=0, it prints F.
if (i)
putch('T');
else
putch('F');
Here's the example of an function that tests if a number is odd:
short isOdd(long num)
{
return num%2; // evals to 1 if num is odd; 0 otherwise
}
Hope that helps.
Upvotes: 0
Reputation: 1023
You can put a simple 'if' statement in it
#include<stdio.h>
int main()
{
int i = 0;
rec(i);
}
int rec(int i)
{
i = i + 1;
if (i == 10){
return 1;
}
j = rec(i)
if (j == 1){
printf('true')
}
printf("i: %d\n", i);
}
Upvotes: 1
Reputation: 206929
If you want to use true
/false
, you can put your compiler in C99 mode (std=c99
for GCC) (or C11), and include <stdbool.h>
.
(Or you'll need to define true and false using preprocessor directives or global constants.)
Upvotes: 2
Reputation: 2372
c does not have a boolean proper. You could have a define for it though
#define true 1
#define false 0
Upvotes: 0