C Program to Check Positive, Negative or Zero

C Program to Check Positive, Negative or Zero

A temperature reading can be above, below or exactly at zero. Two opposite strict comparisons leave zero as a distinct case.

Classify an integer into three disjoint sign categories using > 0, < 0 and an otherwise-zero branch.

Before this exercise, review operators and conversions and decisions and conditions. The worked program below applies those ideas to this specific problem.

  1. Explain

    Explain why positive, negative or zero uses this sequence of operations.

  2. Trace

    Trace the sample values and predict the printed result.

  3. Apply

    Use zero and predict which branch handles equality.

Positive, Negative or Zero: follow the data

  1. Given data

    A temperature reading can be above, below or exactly at zero. Two opposite strict comparisons leave zero as a distinct case.

  2. Rule

    Classify an integer into three disjoint sign categories using > 0, < 0 and an otherwise-zero branch.

  3. Observed result

    Negative

A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Understand the operation

The first true branch wins in this if/else-if chain. A negative value fails the positive test and then satisfies the negative test.

The last else is justified because an integer that is neither greater nor less than zero must equal zero. Do not label that branch negative simply because the positive test failed.

Read the complete C program

The input values are written into this complete C17 example so the calculation can be reproduced without an unavailable service. Change the named data, rebuild and compare the actual output with your prediction.

C17 · complete program

#include <stdio.h>
int main(void) {
  int value = -3;
  if (value > 0) puts("Positive");
  else if (value < 0) puts("Negative");
  else puts("Zero");
  return 0;
}

Expected output

Negative
Run in C compiler →

Follow the values through the code

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int value = -3;
  if (value > 0) puts("Positive");
  else if (value < 0) puts("Negative");
  else puts("Zero");
  return 0;
}

-3 > 0 is false.

-3 < 0 is true, so only Negative is printed.

Follow the worked calculation

  1. Start with the sample

    -3 > 0 is false.

  2. Apply the rule

    -3 < 0 is true, so only Negative is printed.

  3. Check the result

    Negative

    Negative
A guided model of this example. The illustration explains the process; run the complete C program to check its output.

Try a small variation

C17 · code used in this explanation

#include <stdio.h>
int main(void) {
  int value = -3;
  if (value > 0) puts("Positive");
  else if (value < 0) puts("Negative");
  else puts("Zero");
  return 0;
}

Use zero and predict which branch handles equality.

Worked answer

Both strict tests are false for zero. The final else produces the separate Zero label instead of treating the boundary as either sign.

C17 · complete program

#include <stdio.h>
int main(void) {
  int value = 0;
  if (value > 0) puts("Positive");
  else if (value < 0) puts("Negative");
  else puts("Zero");
  return 0;
}

Expected output

Zero
Run in C compiler →

Recognize the important boundaries

This example tests integers. Floating-point exceptional values such as NaN require a separate policy because ordinary ordering comparisons can both be false.

An integer’s sign is independent of whether it is even or odd; these are separate properties.

Check your understanding

Choose the answer, then compare the reasoning for every option.

Why is a third branch needed?
Choose one answer. You can try again.

Answer and explanation

Zero satisfies neither strict sign comparison.

Correct. The equality boundary is its own category.

Use the idea independently

Classify an integer into three disjoint sign categories using > 0, < 0 and an otherwise-zero branch.

Continue with testing the problem’s boundary cases.

Language reference: WG14 C11 committee draft. These examples use the C17-compatible subset of the explained language features.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: C Program to Check a Leap Year →