Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can int return-type function return true?

Tags:

c

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?

like image 713
sarsarahman Avatar asked May 06 '12 06:05

sarsarahman


People also ask

Can the return type of the main function be int True or false?

Explanation: True, The default return type for a function is int.

Can you return true or false in C?

true and false are C++. C doesn't have them. … unless you #include <stdbool.

What returns true in C?

Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

Can int be return type?

A return type may be a primitive type like int, float, double, a reference type or void type(returns nothing). The type of data returned by a method must be compatible with the return type specified by the method. For instance, if the return type of some method is boolean, we can not return an integer.


1 Answers

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.

like image 72
paxdiablo Avatar answered Sep 24 '22 18:09

paxdiablo