Conditional Statements in C
Using conditional statements in C, such as the if statement above, we can easily create more flexible code. Having already created the hello.c code before that takes arguments to display information. We now need to investigate how to provide a prompt if the user does not supply arguments. Making the script easy to use with or without arguments.
Compiling Code with Warnings
When we compiled our code before using the GNU C compiler or gcc program we did not see any errors. Fabulous, this is what we want but there me problems lurking that we did not know about. Choosing to compile with warning options can tell us of any potential issues that exits. The option -W can enable the warning that we select. -Wall turns on most if not all warnings and is a good option to use.
gcc -Wall -o hello hello.c
Viewing the output from gcc we now see that two warning are produced. These both relate to the return value from the function main . We omitted to define that the function main returns an integer, not only that, we failed to specify any return value. We are C coding criminals! Functions can and often do return values, just think of a function to calculate the area of a rectangle. The result of the calculation needs to be returned. We will have both input and output arguments in this case. Any main function is required to return an integer, if we do not specify then it still works but with the return values of functions that run inside main. The control the output ourselves we should add in the required options.
Returning Values From Main
int main ( int argc, char **) { ..... return 0; }
Looking at the snippet of code we add the word int before the function to specify the data type of the return value. Int is an abbreviation for integer or any whole number. The last statement in the main function specifies the value to return. We return the value 0. 0 represents success and a value other than 0 represents a failure.
We can compile the code without any warnings now, either using -Wall or the specific warning of -Wreturn-value.
gcc -Wall -o hello hello.c gcc -Wreturn-value -o hello hello.c
Prompting for User Input
Waiting a while before we move onto the conditional statements in C we will look at how we can prompt for user input.
char name[100]; printf("Enter your name: "); scanf("%s", name);
Analyzing the lines we have added to the hello.c file:
- char name(100); We declare a variable name t be of type char. A char is a single character to we set the variable to be an array with indices from 0 to 99 [100]. There is no string data type so we set up an array of characters which is what a string is.
- scanf(“%s”, name); We have seen printf before and scanf is related to printf but instead of printing data it reads data. We collect keyboard data and store it to the name variable. We set up a string placeholder with %s to accept the data.
Allowing Choice
Having waited all this time this is now where we can add in conditional statements to the C program. We can use the if conditional to look at arguments that have been supplied.
//Program to prompt for user input #include <stdio.h> #include <string.h> int main ( int argc, char **argv) { char name[100]; if ( argc < 2 ) { printf("Enter a name: "); scanf("%s", name); } else { strcpy(name, argv[1]); } printf("Hello %s\n", name); return 0; }
The if statement looks for the argument count being less than 2, if (argc < 2 ). The number of supplied arguments are maintained in the integer variable argc. When looking to see if command lines arguments have been supplied we look for < 2 NOT <1. There is always one argument, the program name. So what we see as the first argument is in fact, the 2nd argument. If we haven’t supplied the argument then we prompt for user input. We use the keyword else to maintain the alternative block of code for what should happen if we have supplied arguments. Here we use the function strcpy to copy the string from argv[1] to the variable name. This way we can print the one variable name no matter if we prompt or pass the value into the program. To access strcpy, we include the library string.h.
We can now compile and test the program befor we have a C program that makes use of conditional statements.