Thread: How to call main of one program in other

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    How to call main of one program in other

    hi

    i just want to know that if i am using the following code in file a.c then how can i save/use the value returned by the main() function of this file in any other file say b.c
    Code:
    int main()
    {
    int s=10;
    return s;
    }
    this fn. will return 10 to the console but how to use it?

  2. #2
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    I am not sure about saving it in the environment, as in in your shell. However, you can print it to stdout, and let b.c read from stdin. Then, you can pipe the output of a to stdin for b, with the pipeline character, like this: a | b.

    -edit: You can also do it in the shell. $? is the variable for the exit value of the last command executed. Thus, , you can on the shell do this:

    Code:
    a
    //a finishes
    b $? //gives b the exit status of a as a paramter in argv.
    Google shell scripting for more info.
    Last edited by Silfer; 11-28-2005 at 11:52 AM.
    -S

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    itsme@itsme:~/C$ cat exitcode.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/wait.h>
    
    int main(void)
    {
      int status;
    
      status = system("./exitcode2");
      if(status == -1)
      {
        puts("system() call failed");
        return 1;
      }
    
      printf("Program 'exitcode2' returned code %d.\n", WEXITSTATUS(status));
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ cat exitcode2.c
    int main(void) { return 10; }
    Code:
    itsme@itsme:~/C$ ./exitcode
    Program 'exitcode2' returned code 10.
    itsme@itsme:~/C$
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User TactX's Avatar
    Join Date
    Oct 2005
    Location
    Germany.Stuttgart
    Posts
    65
    This could work, but is not guaranteed by the standard.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >This could work, but is not guaranteed by the standard.
    The desired functionality is beyond the scope of the standard, so naturally any viable solution will be as well.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return to main or restart program
    By behzad_shabani in forum C Programming
    Replies: 5
    Last Post: 06-29-2008, 04:58 AM
  2. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  3. a program that will call an execute but with arguments
    By what3v3r in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2006, 09:44 PM
  4. Simple Timer, Call external program,
    By bliss in forum C++ Programming
    Replies: 2
    Last Post: 06-02-2005, 11:21 PM
  5. How to call another program?
    By Unreg1stered in forum C++ Programming
    Replies: 4
    Last Post: 07-21-2002, 12:03 AM