Thread: return value

  1. #1
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272

    return value

    What will be the return value of a program that's missing a return statement?

    If we for example have

    Code:
    int main()
    {
        printf("hello, world\n");
    }
    And pass output of this program into another program, what will be the value passed to that program?

    Will it be trash value?

  2. #2
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Yes. It's undefined.

  3. #3
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    Im also interested about how i can use a return value of one program (for example hello world which returns 1) in another program?

    Could anyone demonstrate me a simple example?
    Last edited by Tool; 05-25-2010 at 11:18 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Quote Originally Posted by Tool
    And pass output of this program into another program, what will be the value passed to that program?
    In this particular example, assuming the program is compiled with respect to the 1999 edition of the C standard, then the value is 0, since this is a special exception for the main function. However, more generally, if the caller of a function with a non-void return type attempts to use the value returned by the function, but control reaches the end of the called function without encountering a return statement, then the behaviour is undefined.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jul 2009
    Location
    Croatia
    Posts
    272
    And how can i for example use that return value somewhere else?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,377
    Quote Originally Posted by Tool
    And how can i for example use that return value somewhere else?
    It depends on how you are invoking the program. If you are invoking that program using system() from another C program, typically the return value of system() would be that corresponding return value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    In theory, it always seems to me that system() should do this:

    Quote Originally Posted by man system
    The value returned is -1 on error (e.g. fork(2) failed), and the
    return status of the command otherwise.
    In practice, this does not work (try it). However, if you use fork/exec/waitpid the "status pointer" (2nd arg) might work.

    Here's a hack method that will do it:
    Code:
    !#/bin/bash
    
    ./test
    echo $?
    Here "test" is a local executable (eg, hello world), and the correct return value is reported. So you could run the process via such a script and grab the return value on stdout. Alternately you could export an environment variable and access that.

    But that is definitely an unportable hack method. Curious to see if other people have the same experience with system:
    Code:
    #include <stdio.h>
    
    int main(int argc, const char *argv[]) {
    	int x = system("./test");
    
    	printf("%d\n",x);
    	return 0;
    }
    I do not get a correct value, wheras the bash script does.
    Last edited by MK27; 05-25-2010 at 11:44 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM