-1

I am learning C from a book and the author is explaining using 1 for true and 0 for false with while loops. He explained with the following the sample code but I couldn't understand it.

INPUT

#include<stdio.h>
int main(void)
{
    int n = 3;

    while (n)
        printf("%2d is true\n", n--);
    printf("%2d is false\n", n);


    n = -3;
    while (n)
        printf("%2d is true\n", n++);
    printf("%2d is false\n", n);


    getchar();
    return 0;
}

OUTPUT

3 is true
2 is true
1 is true
0 is false
-3 is true
-2 is true
-1 is true
0 is false
dhiman007
  • 95
  • 5
  • 6
    What did you not understand? And the correct wording is: `0 is false, everything else is true`. – RedX Jul 20 '15 at 12:46
  • 0 is false, any other number is true. – m0skit0 Jul 20 '15 at 12:46
  • That's not exact. 0 means false but everything but 0 means true in a if statement or a while loop. – VDarricau Jul 20 '15 at 12:47
  • 3
    True is 1, but not everything that is true is 1. – Kerrek SB Jul 20 '15 at 12:47
  • Guys I am new to C so sometimes I don't know the correct phrase. – dhiman007 Jul 20 '15 at 12:48
  • 2
    Get a more recent book. C has `_Bool` (mapped to the C++ names `bool` with `stdbool.h`, etc.) since C99. So do not use magic values (although `_Bool` actually _is_ an integer type with the correct conversions). – too honest for this site Jul 20 '15 at 12:49
  • Seconding @Olaf: you should seriously consider using the modern `_Bool` type and the `stdbool.h` header. See [*Is bool a native C type?*](http://stackoverflow.com/questions/1608318/is-bool-a-native-c-type/1608350#1608350). – Lynn Jul 20 '15 at 12:51
  • @Olaf The book is recent (2013) but the author was explaining how things worked in earlier days. – dhiman007 Jul 20 '15 at 12:51
  • @dhiman007 Please elaborate which part you do and do not understand. E.g : Increment operator `n++`, decrement operator `n--`, condition in the loop `while(n)` etc – Spikatrix Jul 20 '15 at 12:52
  • 2
    In "earlier days" we had to go to the river to wash our clothes. You should not learn how it was done back then, but how to do it now. Tempora mutantur. – too honest for this site Jul 20 '15 at 12:53
  • And note: "true" is not only `1`, but `!0`: any value other than `0`. Which is exactly what you are relying on. – too honest for this site Jul 20 '15 at 12:55
  • @dhiman007 it works exactly the same nowadays, unless you use `_Bool` type – phuclv Jul 25 '15 at 07:05

4 Answers4

7

May be if you add braces you can understand the code properly.

#include<stdio.h>
int main(void)
{
    int n = 3;

    while (n)
    {
      printf("%2d is true\n", n--);
    }
    printf("%2d is false\n", n);

    n = -3;
    while (n)
    {
      printf("%2d is true\n", n++);
    }
    printf("%2d is false\n", n);


    getchar();
    return 0;
}

The code that you posted and the above code are same. If there are no braces the next statement after the while will be executed.

This:

printf("%2d is true\n", n--);

in the first while will continue to execute until while(n) is false. while(n) is equivalent to while(n!=0). while(something) is false only when something is 0.

So, when n=3 the condition in the first loop is true and then n--(n=n-1) makes n 2 and for 2 the condition is true and so on. When n is 0 the condition becomes false and exits the loop. The program then prints false and similarly the second while loop is executed.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
Minions
  • 1,273
  • 1
  • 11
  • 28
5

There are several places in C where any expression are used a Boolean values deciding the program's flow:

  • Conditional statement if
  • Loop constructs for, while, and do-while
  • Conditional expression ?-:

In all these places where a Boolean value is consumed, an implicit != 0 is added, so

while (n)

means

while (n != 0)

Hence, all values other than zero are considered true.

There are other places in the language where a Boolean value is produced by a logical expression, e.g. the logical NOT ! operator. In this case, the values produced are confined to the 0, 1 set. Specifically, a logical NOT of a zero is 1.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Okay I got that true and false part of the loop but I can't understand the things inside the loop. – dhiman007 Jul 20 '15 at 12:55
  • 1
    `printf("%2d is true\n", n--);` prints `n` followed by `is true\n` and then, when `n` was printed, decrements `n` value by 1. You can read more [here](https://www.eskimo.com/~scs/cclass/notes/sx7b.html) or Google. – zrzka Jul 20 '15 at 13:00
3

In fact C treats 0 as FALSE and non-0 as TRUE.

m0skit0
  • 25,268
  • 11
  • 79
  • 127
timrau
  • 22,578
  • 4
  • 51
  • 64
  • I got it that 0 for false and 1 for true but why it produces the following output? – dhiman007 Jul 20 '15 at 12:47
  • 1
    `while` loops continue as long as the condition in them is *false*. The numbers 3, 2, and 1 are all "true" values, so we keep looping; as soon as the loop hits 0, it terminates. – Lynn Jul 20 '15 at 12:48
2

The first time it reaches while(n) the value of n is 3. Because only 0 counts a false, 3 counts are true and that statement amounts to while(3) that is "while true" so the following line gets executed:

printf("%2d is true\n", n--);

That line outputs the value of n. If you don't understand why that is going to output that value go back a chapter or two and read up on printf(.). I don't know what book you're reading but I assume it follows a logical order and introduced output pretty early on.

That line also subtracts one from n (that's what n-- means). If you don't understand that, again go back a bit. That expression reduces n by one but returns its value before the subtraction.

That results in the first line of output:

3 is true

Then the program loops back to the while condition. Now n is 2. But 2 still counts as true so the in-loop code (the first printf) gets executed again giving:

2 is true

And ends with n holding the value 1. That leads to one further execution of the in-loop code giving

1 is true

Now that execution leaves with n having the value 0. That counts as false so the loop condition while(n) is while(0) which means "while false".

So execution bypasses the in-loop code and executes:

printf("%2d is false\n", n);

The leads to the line

0 is false

The rest of the program does a similar thing in reverse starting with n having the value -3 and then incrementing it (by n++) till again it hits false (n being 0).

That gives the lines

-3 is true
-2 is true
-1 is true
0 is false
Persixty
  • 8,165
  • 2
  • 13
  • 35