CrazyEngineers
  • C Programming: How to Call Another Program from Main Program?

    Updated: Oct 27, 2024
    Views: 4.8K

    Hi CEans, in C language, is there any possibility to call another program from main program?

    To call another program from a C program, you can use the system() function which is available in the stdlib.h library. The system() function executes a system shell command. It creates a shell command process, executes the command, and then returns once the command has been completed.

    Here is a simple example demonstrating how to use the system() function to call another program:

    #include <stdlib.h> /* Including standard library header files */
    
    int main() {
    
        int return_value;
    
        /* The system() function takes a string argument that is the command to be executed. 
    
        In this case, the command is the path to the other program that you want to run. */
    
        return_value = system("/path/to/other/program");
    
        /* The system() function returns an integer. This value can be used to determine if the system call was successful.
    
        Usually, if the value is -1, it means that the system call failed. */
    
        if(return_value == -1) {
    
            /* Handle the error accordingly. Here, we just simply print out a message. */
    
            printf("System call failed");
    
        }
    
        return 0;
    
    }

    In the above example, replace /path/to/other/program with the path to the executable that you wish to run.

    Please be aware that this is a simple example. The system() function has limitations and potential security risks because it invokes a shell command.

    The shell can interpret special characters or environment variables, potentially leading to unexpected behavior if you pass in untrusted user input.

    For more controlled execution of another program, look into fork() and exec() functions. These functions give you more control over input, output, and error streams, and avoid invoking a shell. However, they're also more complex to use.

    Also, error handling for system() calls should be more robust in a production environment.

    Error messages should be descriptive and possibly logged for troubleshooting. The error handling in this example is kept simple for demonstration purposes.

    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • saurabh2486

    MemberMay 9, 2010

    Re: C program doubt..

    i think yes its possible
    Are you sure? This action cannot be undone.
    Cancel
  • sherya mathur

    MemberMay 9, 2010

    Re: C program doubt..

    I will also go with saurabh
    yes it is possible in c language to call another program from main program
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 10, 2010

    Re: C program doubt..

    saurabh2486
    i think yes its possible
    sherya mathur
    I will also go with saurabh
    yes it is possible in c language to call another program from main program
    Can we know how this is done?
    Are you sure? This action cannot be undone.
    Cancel
  • ms_cs

    MemberMay 10, 2010

    Re: C program doubt..

    Just make that file as header file and include it in the new file in which you want to use that file..thats it
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 10, 2010

    Re: C program doubt..

    will you please provide me simple program code....
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 10, 2010

    Re: C program doubt..

    Ah! now I remember,

    If you want to run a program within a terminal using a C program, you can use the standard library function system ().

    Suppose we want to run the program "hello.exe", here's how we can do this,
    int main ()
    {
        printf ("Before calling a program \n");
        system ("hello.exe");
        printf ("After calling the program \n");
        getch ();
        return 0;
    }
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 13, 2010

    Re: C program doubt..

    hi Gaurav, i executed the code which are provided by you.. It's executing only the printf statements but it not executing the block which are inside system.... Is there any header i need to include????
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 13, 2010

    Re: C program doubt..

    bharathkumarp
    hi Gaurav, i executed the code which are provided by you.. It's executing only the printf statements but it not executing the block which are inside system.... Is there any header i need to include????
    Yes, you have to include stdlib.h for system() to work.
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 13, 2010

    Re: C program doubt..

    HI Gaurav, this is also not working.. Only the printf statements are working...Without stdlib.h also it's showing the same output....
    Are you sure? This action cannot be undone.
    Cancel
  • mayank1055

    MemberMay 13, 2010

    yup it's not working
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 15, 2010

    It works perfectly in Visual C++ Express Edition 2008.
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 15, 2010

    ok, i'll check it out... Thanks for the code...
    Are you sure? This action cannot be undone.
    Cancel
  • silverscorpion

    MemberMay 16, 2010

    Did you create the hello.exe file? Do you have it in the same directory?
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 16, 2010

    silverscorpion
    Did you create the hello.exe file? Do you have it in the same directory?
    Yes the hello.exe file and our calling program's file should be in the same directory.

    The hello.exe must be created, it can have anything like printf ("Hello World \n").
    Are you sure? This action cannot be undone.
    Cancel
  • bharathkumarp

    MemberMay 16, 2010

    ya i put the file inside the same directory,but it is not working.....
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMay 17, 2010

    bharathkumarp
    ya i put the file inside the same directory,but it is not working.....
    Which compiler are you using?

    If you are using VC++, then your program's executable is in the debug directory. That is, My Documents/Visual Studio/Projects/<projectname>/Debug.

    Now paste the hello.exe into this directory.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register