Exercise 8. If, Else-If, Else

In C, there really isn’t a Boolean type. Instead, any integer that’s 0 is false or otherwise it’s true. In the last exercise, the expression argc > 1 actually resulted in 1 or 0, not an explicit True or False like in Python. This is another example of C being closer to how a computer works, because to a computer, truth values are just integers.

However, C does have a typical if-statement that uses this numeric idea of true and false to do branching. It’s fairly similar to what you would do in Python and Ruby, as you can see in this exercise:

ex8.c

 1   #include <stdio.h>  2  3   int main(int argc, char *argv[])  4   {  5       int i = 0;  6  7       if (argc == 1) {  8           printf ...

Get Learn C the Hard Way: A Clear & Direct Introduction To Modern C Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.