Thread: If Else Statement Goes Error!!!

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You are welcome!!!

    Greetings to Indonesia!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by loserone+_+ View Post
    really that i can return one thing only?
    Yes. The comma operator does not change return semantics. What will happen instead is that the left expression will be evaluated followed by the right.

    Code:
    #include <stdio.h>
    
    int x = 1;
    
    int foobar(void) {
        int y = 0;
    
        return x *= 4, y;
    }
    
    int main(void) {
        int rv;
    
        printf("x before foobar() = %d\n", x);
        rv = foobar();
        printf("foobar() returned = %d\n", rv);
        printf("x after foobar() = %d\n", x);
        return 0;
    }
    As in this program. As the author may have intended to return both the new value of x and 0, it doesn't really work, and in any case, the main() function was not prepared to receive 2 things. Instead, the new value of x is a side effect of invoking foobar(). Which is bad because you want functions to have clear expectations of input and output.

    If you pack variables into a structure, you can return as many values as you want, but you are still returning a single object.

    hmmm, i already change that and compile again
    still had errors
    Some logical errors, like this one, cannot be stopped by the compiler as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. syntax error in my if statement
    By UCFuser in forum C Programming
    Replies: 13
    Last Post: 04-01-2011, 01:57 PM
  2. Error proofing my switch statement.
    By Shamino in forum C++ Programming
    Replies: 10
    Last Post: 01-04-2008, 04:38 PM
  3. Replies: 3
    Last Post: 10-02-2007, 09:12 PM
  4. error when compiling if statement
    By robasc in forum C Programming
    Replies: 2
    Last Post: 07-08-2005, 08:20 PM
  5. an if statement with error
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-22-2002, 03:52 PM