-1

To begin, the code that I have got up to this point is below.

The calculator does everything I want it to, minus the fact that it still includes negative numbers in its calculations. I would like for it to do nothing if the number presented as the radius is less than zero, yet still calculate if the number is non-negative. However, I am having issues with using an if statement. I have not used these before, as I am really just a beginner. I just need a push in the right direction. Do I need to have an "else"? Any help is greatly appreciated!

#include <stdio.h>

int radius, area;

main()
{
    printf("Enter radius: ");
    scanf("%d", &radius);
    {
    if (&radius > -1);  {
         area = (int)(3.14159 * radius * radius);
         printf("Area = %d\n", area);}
    else {
       return(0); }
    }
   return(0);
}
6
  • Stray semicolon: if (&radius > -1); { --> if (&radius > -1) {
    – dbush
    Feb 4, 2019 at 19:14
  • Not related to the problem, but return is not a function. You don't need to put the return value in brackets. And what is the purpose of the block enclosing the if-else?
    – Gerhardh
    Feb 4, 2019 at 19:16
  • OT: for ease of readability and understanding: 1) follow the axiom: *only one statement per line and (at most) one variable declaration per statement. Note treat the closing brace as a separate statement. Feb 6, 2019 at 3:50
  • regarding the statement: main() some old compilers (and visual studio) will allow that. However, the function main() only has two valid signatures: int main( void ) and int main( int argc, char *argv[] ) Feb 6, 2019 at 3:52
  • regarding: if (&radius > -1); { this has two errors: 1) do not take the address of radius what you actually want is the contents of radius 2) the embedded semicolon ';' ends the if() statement. However, you do not want to do that here. Here is what that statement should look like: if( radius > -1 ) { however, it would be clearer as: if( radius >= 0 ) { Feb 6, 2019 at 3:56

3 Answers 3

5

Remove semi-colon

remove semi-colon this lines

if (&radius > -1);  {  

should be

if (radius > -1)  {

Should do this for easier tracking if-else statement

change these lines

printf("Area = %d\n", area);}

return(0); }

to

printf("Area = %d\n", area);
}

return(0); 
}

Here is style for if-else statement, I think it's easier for you to track your code

if (condition) {
    statements;
} 
else if (condition) { 
    statements;
}
else {
    statements;
}
0
1

The C compiler doesn't care about formatting, in theory you can do whatever you like, and there's no consensus for what is/isn't "proper formatting".

However, most programmers stick to a specific style so that it's easier to read the source code; and if you're working in a team then it's nice if all programmers on the team use the same style so that all of the source code is consistent. To achieve that, there may be a formal "style guide", and if there is you should follow it.

Beyond that, there are some common rules that almost everyone follows:

  • nested blocks that are delimited by braces are indented somehow (with either "N space characters" or "N tab characters") relative to the parent block

  • cases of a switch statements will be exceptions to the indentation rules. Typically each case's statements are indented even though there's no braces; and the case keyword itself may or may not be indented by the parent switch's braces.

  • either all braces are always on a line by themselves; or starting braces are at the end of the line and ending braces may be at the start of a line containing a related statement

  • when a block consists of a single statement; either it always uses braces and is takes up a line by itself, or it never uses braces and shares the same line as its parent.

  • the else if pair is always an exception to the "block consists of a single statement" rule (the if is a single statement that is never treated as a separate block, and people pretend else if is a single elseif keyword).

What this means is that (depending on who got their way when arguing about it) this might acceptable:

int main() {
    int area;

    printf("Enter radius: ");
    scanf("%d", &radius);

    switch(radius) {
    case 0:
        return 0;
    case 1:
        return 1;
    }

    if (&radius > -1) {
        area = (int)(3.14159 * radius * radius);
        printf("Area = %d\n", area);
    } else return -1;
    return area;
}

..and this might also be acceptable:

int main()
{
    int area;

    printf("Enter radius: ");
    scanf("%d", &radius);

    switch(radius)
    {
        case 0:
            return 0;
        case 1:
            return 1;
    }

    if (&radius > -1)
    {
        area = (int)(3.14159 * radius * radius);
        printf("Area = %d\n", area);
    }
    else
    {
        return -1;
    }
    return area;
}
0

if (&radius > -1);

Lose the & before radius and lose the semicolon:

if ( radius > -1 ) { ... }

Do I need to have an "else"?

In this particular case, no. You're not doing anything in the else branch that you aren't already doing unconditionally (i.e., the return statement). You'd only need an else branch if you needed to take a specific action only when the condition fails, such as printing an error message:

if ( radius > -1 )
{
  ...
}
else
{
  printf( "Radius needs to be non-negative!\n" );
}
return 0;

As for how to format, there are multiple styles with their own advantages and disadvantages. The style I prefer has the { and } on their own lines:

if ( condition )
{
  // if branch
} 
else if ( another condition )
{
  // else if branch
}
else
{
  // else branch
}

This style is easiest for me to read, makes it clearer where blocks begin or end, etc. It also increases the use of vertical space, so you get less code in a window, meaning you may have to scroll back and forth a bit more.

There's what's known as the K&R style as it was popularized by Kernighan & Ritchie, where the opening { is on the same line as the if or else:

if ( condition ) {
  // if branch
} 
else if ( condition ) {
  // else if branch
}
else {
  // else branch
}

Not quite as easy to pick out where a block starts, but not too bad, and somewhat more compact, so you can fit more lines of code into a limited space.

Then there's a style I flat-out hate, which puts the closing } on the same line as the else or else if:

if ( condition ) {
  // if branch
} else if ( condition ) {
  // else if branch
} else {
  // else branch
}

I find this style very hard to read and follow. But, it's the most compact style, and commonly used in situations where screen real estate is very limited (such as in a PowerPoint slide in a lecture or tutorial or something).

Note that the compiler doesn't care which style you use, as long as the opening and closing braces match up properly. You could put everything on a single line with almost no whitespace like:

if(radius>-1){area=(int)(3.14159*radius*radius);printf("Area = %d\n", area);}else{print("Radius needs to be non-negative!\n");}

Formatting only matters for you and anyone who has to read/maintain your code. Whatever style you pick, be consistent with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.