How to Use the if Function in C Programming

By Dan Gookin

The if keyword in the C programming language is used to make decisions in your code based upon simple comparisons. It’s the same concept humans use in making decisions based on the question “what if?”

Here’s the basic format:

if(evaluation)
{
  statement;
}

The evaluation is a comparison, a mathematical operation, the result of a function or some other condition. If the condition is true, the statements (or statement) enclosed in braces are executed; otherwise, they’re skipped.

  • The if statement’s evaluation need not be mathematical. It can simply be a function that returns a true or false value; for example:

    if(ready())

    This statement evaluates the return of the ready() function. If the function returns a true value, the statements belonging to if are run.

  • Any non-zero value is considered true in C. Zero is considered false. So this statement is always true:

    if(1)

    And this statement is always false:

    if(0)
  • You know whether a function returns a true or false value by reading the function’s documentation, or you can set a true or false return value when writing your own functions.

  • You cannot compare strings by using an if comparison. Instead, you use specific string comparison functions.

  • When only one statement belongs to an if comparison, the braces are optional.

Exercise 1: Rewrite the code from A Simple Comparison, removing the braces before and after Line 12. Build and run to ensure that it still works.

A SIMPLE COMPARISON

#include <stdio.h>
int main()
{
  int a,b;
  a = 6;
  b = a - 2;
  if( a > b)
  {
    printf("%d is greater than %dn",a,b);
  }
  return(0);
}

How to compare values in various ways

The C language employs a small platoon of mathematical comparison operators.

Operator Example True When
!= a != b a is not equal to b
< a < b a is less than b
<= a <= b a is less than or equal to b
== a == b a is equal to b
> a > b a is greater than b
>= a >= b a is greater than or equal to b

Comparisons in C work from left to right, so you read a >= b as “a is greater than or equal to b.” Also, the order is important: Both >= and <= must be written in that order, as must the != (not equal) operator. The == operator can be written either way.

VALUES ARE COMPARED

#include <stdio.h>
int main()
{
  int first,second;
  printf("Input the first value: ");
  scanf("%d",&first);
  printf("Input the second value: ");
  scanf("%d",&second);
  puts("Evaluating...");
  if(first<second)
  {
    printf("%d is less than %dn",first,second);
  }
  if(first>second)
  {
    printf("%d is greater than %dn",first,second);
  }
  return(0);
}

Exercise 2: Create a new project by using the source code shown in Values Are Compared. Build and run.

The most common comparison is probably the double equal sign. It may look odd to you. The == operator isn’t the same as the = operator. The = operator is the assignment operator, which sets values. The == operator is the comparison operator, which checks to see whether two values are equal. (See Get “Is Equal To” into Your Head.)

Pronounce == as “is equal to.”

Exercise 3: Add a new section to the source code from Values Are Compared that makes a final evaluation on whether both variables are equal to each other.

GET “IS EQUAL TO” INTO YOUR HEAD

#include <stdio.h>
#define SECRET 17
int main()
{
  int guess;
  printf("Can you guess the secret number: ");
  scanf("%d",&guess);
  if(guess==SECRET)
  {
    puts("You guessed it!");
    return(0);
  }
  if(guess!=SECRET)
  {
    puts("Wrong!");
    return(1);
  }
}

Exercise 4: Type the source code from Get “Is Equal To” into Your Head into a new Code::Blocks project. Build and run.

Take note of the value returned by the program — either 0 for a correct answer or 1 for a wrong answer. You can see that return value in the Code::Blocks output window.

The difference between = and == in C programming

One of the most common mistakes made by every C language programmer — beginner and pro — is using a single equal sign instead of a double in an if comparison.

ALWAYS TRUE

#include <stdio.h>
int main()
{
  int a;
  a = 5;
  if(a=-3)
  {
    printf("%d equals %dn",a,-3);
  }
  return(0);
}

Exercise 5: Type the source code shown in Always True into a new project. Run the program.

The output may puzzle you.

-3 equals -3

That’s true, isn’t it? But what happened?

Simple: In Line 9, variable a is assigned the value -3. Because that statement is inside the parentheses, it’s evaluated first. The result of a variable assignment in C is always true for any non-zero value.

Exercise 6: Edit the source code from Always True so that a double equal sign, or “is equal to,” is used instead of the single equal sign in the if comparison.

Where to put the semicolon in C programming

Semicolon Boo-Boo is based upon Always True, taking advantage of the fact that C doesn’t require a single statement belonging to an if comparison to be lodged between curly brackets.

SEMICOLON BOO-BOO

#include <stdio.h>
int main()
{
  int a,b;
  a = 5;
  b = -3;
  if(a==b);
    printf("%d equals %dn",a,b);
  return(0);
}

Exercise 7: Carefully type the source code from Semicolon Boo-Boo. Pay special attention to Line 10. Ensure that you type it in exactly, with the semicolon at the end of the line. Build and run the project.

Here’s the output:

5 equals -3

The problem here is a common one, a mistake made by just about every C programmer from time to time: The trailing semicolon (Line 10) tells the program that the if statement has nothing to do when the condition is true. That’s because a single semicolon is a complete statement in C, albeit a null statement. To wit:

if(condition)
  ;

This construct is basically the same as Line 10. Be careful not to make the same mistake — especially when you type code a lot and you’re used to ending a line with a semicolon.