Video Coming Soon...
08: 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:
#include <stdio.h>
int main(int argc, char *argv[])
{
if (argc == 1) {
printf("You only have one argument. You suck.\n");
} else if (argc == 2) {
printf("Here's your arguments: %s %s\n", argv[0], argv[1]);
} else if (argc == 3) {
printf("Here's your arguments: %s %s %s\n", argv[0], argv[1], argv[2]);
} else if (argc == 4) {
printf("Here's your arguments: %s %s %s %s\n", argv[0], argv[1], argv[2], argv[3]);
} else {
printf("You have too many arguments. You suck.\n");
}
return 0;
}
The format for the if-statement is this:
if(TEST) {
CODE;
} else if(TEST) {
CODE;
} else {
CODE;
}
This is like most other languages except for some specific C differences:
- As mentioned before, the
TESTparts are false if they evaluate to 0, or otherwise true. - You have to put parentheses around the
TESTelements, while some other languages let you skip that. - You don't need the
{}braces to enclose the code, but it is very bad form to not use them. The braces make it clear where one branch of code begins and ends. If you don't include them then obnoxious errors come up.
Other than that, the code works the way it does in most other languages. You don't need to have either else if or else parts.
What You Should See
This one is pretty simple to run and try out:
View Source file ex8.sh-session Only
$ ./ex8
You only have one argument. You suck.
$ ./ex8 apple
Here's your arguments: ./ex8 apple
$ ./ex8 apple orange
Here's your arguments: ./ex8 apple orange
$ ./ex8 apple orange pear
Here's your arguments: ./ex8 apple orange pear
$ ./ex8 apple orange pear tomato
You have too many arguments. You suck.
$
You'll notice that if you give zero arguments is will say you only have one argument. This is because the first argument is actually the name of your program. In this case it's ex8. You can see this as it prints each argv[] out.
argv and Command Line Arguments
I'm purposefully avoiding explaining the int main(int argc, char *argv[]) line that you've been using because it relies on a lot of information I'll cover later. But, now that you know how an array works, and you've created arrays of number types (int, float, double, etc.) you can learn about the int argc and char *argv[] parts.
First int argc is the count of arguments given to your program on the command line. This inludes the name of the program as the first one, so if you do ex8 a b c then argc will be 4, not 3.
Next the char *argv[] is most easily understood as an "array of strings of any length." The char * is a string as you've use briefly in Exercise 06 and the argv[] is an array like in Exercise 07. The difference is there's no size in the [] which tells the compiler that it will point to an array without a known size. Remember this word "point" for later.
That means, if you want to get each argument of the command ex8 a b c you do this:
printf("%s\n", argv[0]); // ex8
printf("%s\n", argv[1]); // a
printf("%s\n", argv[2]); // b
printf("%s\n", argv[3]); // c
This is where the argc comes into play. Since argv[] doesn't have a size you have to use the argc to know how many elements it has. Also, remember that all arrays start with 0.
How to Beak It
This one isn't easy to break because it's so simple, but try messing up the tests in the if-statement:
- Remove the
elseat the end, and the program won't catch the edge case when you have more than 4 arguments. Try to fix it so that it'll print all the arguments. - Change the
&&to a||so you get an or instead of an and test and see how that works. - Try blasting past the end of
argvthe way you did inExercise 07.
Imposture Check
- You learned more operators in
Exercise 5so try the||and!operators. See if you can figure out what they do from experimentation. - Write a few more test cases for this program to see what you can come up with.
- Is the first test really saying the right thing? To you, the first argument isn’t the same first argument a user entered. Fix it.
Register for From Impostor to Programmer
Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.