Thread: Passing return value from one main to other main function?

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    8

    Passing return value from one main to other main function?

    I wonder if it is possible to pass a return value from one main function as input value to another main function.

    For example:

    Program that passes a value:
    Code:
    //program-1.c
    
    int main(void)
    {
      return 10;
    }
    
    Program that receives the value returned by the previous program:
    Code:
    //program-2.c
    #include <stdio.h> int main(int argc, char* argv[]) { printf("%i", argv[1]); }
    On the console I wrote down:
    Code:
    program-1.c | program-2.c
    But the output is 0, not 10.

    Is it possible that program two returns the value 10 from program 1?
    What must be done so that the return value of program 1 is received by program 2 and displayed on screen by printf ()?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > program-1.c | program-2.c
    I assume you meant the compiled versions of the two programs.

    Anyway, given this
    Code:
    int main(void)
    {
      return 10;
    }
    You'll find your 10 in the exit status.
    Code:
    ./program1
    echo $?
    > What must be done so that the return value of program 1 is received by program 2 and displayed on screen by printf ()?
    Maybe...
    Code:
    ./program1
    echo $? | ./program2
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest that you don't use the return value from main for this. Rather, write the value to standard output in the first program and read the value from standard input in the second program. Then invoke both programs such that the output of the first is piped as input to the second.
    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

  4. #4
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Thank you!

    You're right, I was referring to placing

    Code:
    program-1.exe | program-2.exe
    .
    What I see is that the option you gives me is for linux (or Unix-based operating systems). Do you know what the command line for Windows would look like?

  5. #5
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Thanks for the reply!
    But my query is to know if there is a possibility of passing a returned value from one program to another program.
    In other words, a main returns a value that is captured by another main.
    Or if that possibility does not exist.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    The return value from main is different from return values from other functions.
    Main's return value is called the program's "exit status".
    It is non-portable to use this value for anything but determining the success or failure of the program.
    So in general, no, you cannot "return" a value from one program and use it in another.
    There are many other ways for programs to communicate.
    Pipes and the file system are perhaps the most common.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GisiNA View Post
    You're right, I was referring to placing

    Code:
    program-1.exe | program-2.exe
    That is exactly what you would do to "invoke both programs such that the output of the first is piped as input to the second".

    Quote Originally Posted by GisiNA
    But my query is to know if there is a possibility of passing a returned value from one program to another program.
    In other words, a main returns a value that is captured by another main.
    Or if that possibility does not exist.
    According to your previous post, you're asking asking about how to implement programs such that you can pipe I/O, so what I described about one program printing to standard output and the other program reading from standard input is exactly what you should do.
    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

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Do you know what the command line for Windows would look like?
    Errorlevel - Windows CMD - SS64.com
    But be prepared for even more crustyness.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jun 2019
    Posts
    8
    Thanks to all for the answers!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 05-08-2014, 10:57 AM
  2. Replies: 6
    Last Post: 12-16-2011, 07:27 AM
  3. Replies: 6
    Last Post: 09-23-2008, 07:27 PM
  4. Any way to return to the top of main from a function
    By countchocula in forum C Programming
    Replies: 17
    Last Post: 04-29-2008, 08:33 PM
  5. getting a function to return to main??
    By Neildadon in forum C++ Programming
    Replies: 7
    Last Post: 12-16-2002, 10:24 AM

Tags for this Thread