Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are no <code>true</code> or <code>false</code> keywords in C. There are some <em>library</em> definitions for those values in <code>stdbool.h</code> (starting in C99, I think) but oft times most C programmers will just use <code>1</code> and <code>0</code>.</p> <p>If you don't want to use <code>stdbool.h</code>, or you're working on a compiler that doesn't support it, you can usually define the constants yourself with something like:</p> <pre><code>#define FALSE (1==0) #define TRUE (1==1) </code></pre> <p>or you can use <code>1</code> and <code>0</code> directly - the method above is for those who don't want to have to remember which integer values apply to which truth values <sup>(a)</sup>.</p> <p><code>0</code> 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.):</p> <pre><code>#include &lt;stdio.h&gt; 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; } </code></pre> <p>which gives you:</p> <pre><code>i: 1 i: 2 i: 3 i: 4 i: 5 i: 6 i: 7 i: 8 i: 9 rv: 1 </code></pre> <p>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.</p> <hr> <p><sup>(a)</sup>: Keep in mind the caveat that, although the given definition of TRUE will most be 1, <em>any</em> non-zero value will be treated so. That means that the two statements:</p> <pre><code>if (isValid) if (isValid == TRUE) </code></pre> <p>do <em>not</em> mean the same thing. There are a large number of possible <code>isValid</code> values which will be pass the first test but fail the second. That's usually not a problem since it's almost always <a href="https://stackoverflow.com/questions/404838/do-you-prefer-if-var-or-if-var-0/404846#404846">a bad idea to compare boolean variables with boolean constants anyway</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload