C Compiling Error
Dear CLion Community,
I've been studying a programming book, titled: "Beginning C: 5th Edition" by Ivor Horton. In it, there's a brief tutorial that I can't seem to compile. I highly doubt the error is in the book, since this author is a well-regarded and well-respected.
Here it is:
// Program 3.3 Using nested ifs to analyze numbers
#include <stdio.h>
#include <limits.h> // For LONG_MAX
int main(void)
{
long test = 0L; // Stores the integer to be checked
printf("Enter an integer less than %ld:", LONG_MAX);
scanf(" %ld", &test);
// Test for odd or even by checking the remainder after dividing by 2
if(test % 2L == 0L)
{
printf("The number %ld is even", test);
// Now check whether half the number is also even
if((test/2L) % 2L == 0L)
{
printf("\nHalf of %ld is also even", test);
printf("\nThat's interesting isn't it?\n");
}
}
else
printf("The number %ld is odd\n", test);
return 0;
}
_______________________________
Yet, the compiler is giving me these errors.
/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/frederickpina/Library/Caches/CLion12/cmake/generated/6fd947b6/6fd947b6/Debug --target all -- -j 2
Scanning dependencies of target HelloWorld
[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/main.cpp.o
/Users/frederickpina/ClionProjects/HelloWorld/main.cpp:13:24: warning: if statement has empty body [-Wempty-body]
if(test % 2L == 0L);
^
/Users/frederickpina/ClionProjects/HelloWorld/main.cpp:13:24: note: put the semicolon on a separate line to silence this warning
/Users/frederickpina/ClionProjects/HelloWorld/main.cpp:24:5: error: expected expression
else
^
1 warning and 1 error generated.
make[2]: *** [CMakeFiles/HelloWorld.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/HelloWorld.dir/all] Error 2
make: *** [all] Error 2
Can anyone please help ? Thanks !!!!
Attachment(s):
Screen Shot 2015-12-21 at 3.18.16 PM.png
Please sign in to leave a comment.
you don't need a semicolon after IF STATEMENT
remove the semicolon and try again
Hi ! I don't see any semicolons after the If Statement, beyond those semicolons in the nested print statements. I'm confused. Did you mean the first if statement, or the one that's actually listed in the nested print statements, by the shown red error lines ? You weren't very clear on your correction. Please clarify.
I'm sorry for mistaking the warning for an error.
I copied your code and try,It got no wrong and ran well.
in the picture you attached,there is a semicolon after the first if() (line 13),try to remove the semicolon