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

main()
{
int A, B;
clrscr();
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);

getch();
}

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

Recommended Answers

All 3 Replies

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);
}

Thank you very much!

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)
    statement;
    statement;

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. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.