Using Conditional Statements in C

Download PDF

Conditional Statements in C

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 and additionally making users happy.

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, additionally, -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 and also that 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. Taking input arguments, running calculations and as a result returning the answer. 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. To control the output ourselves we should add in the required options and as a result, create better code..

Returning Values From Main

int main ( int argc, char **) {
  .....
  return 0;
}

Firstly, 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. Finally, 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.

As a result of our coding intervention we can compile the code without any warnings now. Using 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. Using a prompt withing the program additionally allows a level of interaction to the program whilst it is running.

char name[100];
printf("Enter your name: ");
scanf("%s", name);

Analyzing the lines we have added to the hello.c file:

  • char name(100);  Firstly 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 and array of characters which is what a string is.
  • scanf(“%s”, name);  Secondly, 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;
}

Firstly we see that 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 the command lines arguments have been supplied we look for < 2 NOT <1. There is always one argument, the program name therefore what we see as the first argument is in fact the 2nd argument. As a result of omitting the argument then we prompt for user input.

Additionally we use the keyword else to maintain the the alternative block of code for what should happen as a result of supplying arguments. Here we use the function strcpy to copy the string from argv[1] to the variable name. Therefore 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 before we have a C program that makes use of conditional statements. Next blog looks at using getopt to pass options to a program.