Here's the problem:
Write a program that will input values for A and B. Compare two values inputted and print which of the values is higher including the remark “Higher”.

So here's what i've done so far

int A, B;
printf("Input Value for A: ");
scanf("&d", &A);
printf("Input Value for B: ");
scanf("%d", &B);
if (A>B);
printf("%d is Higher\n", A);


The problem i'm having is when i input for example:
Input Value for A: 3
Input Value for B: 10
3 is Higher

2 Years
Discussion Span
Last Post by deceptikon

You have a semicolon after your if statement which terminates the statement. Try:

if (A > B) {
   printf ("%d is Higher\n", A);
} else if (B > A) {
   printf ("%d is Higher\n", B);

To elaborate a little bit, the if keyword and condition doesn't constitute a statement. To be described as a statement the body must be included. If the condition ends with a semicolon, you have an empty body that does nothing at all:

// An if statement
if (condition) body

// An incomplete if statement
if (condition)

// A pointless if statement that does nothing
if (condition);

// Equivalent to the above
if (condition)

The body can be either a single line statement in which a semicolon terminates it, or a compound statement surrounded by braces (and not terminated with a semicolon:

// Single line body if statement
if (condition) ...;

// Compound body if statement
if (condition) { ... }

Should your body only contains one statement, you can eschew braces, otherwise braces are required to make it a compound body.

General best practice is to always use braces though, because it avoids this fun little error where the second line is not part of the if body yet appears to be due to poor indentation:

if (condition)

It also makes it more convenient to add statements to a body (which happens often) and not worry about adding braces too. Minor yes, but frustrating nonetheless. :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.