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

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

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  /* 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.

Replies

  • saurabh2486
    saurabh2486
    Re: C program doubt..

    i think yes its possible
  • sherya mathur
    sherya mathur
    Re: C program doubt..

    I will also go with saurabh
    yes it is possible in c language to call another program from main program
  • gaurav.bhorkar
    gaurav.bhorkar
    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?
  • ms_cs
    ms_cs
    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
  • bharathkumarp
    bharathkumarp
    Re: C program doubt..

    will you please provide me simple program code....
  • gaurav.bhorkar
    gaurav.bhorkar
    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;
    }
  • bharathkumarp
    bharathkumarp
    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????
  • gaurav.bhorkar
    gaurav.bhorkar
    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.
  • bharathkumarp
    bharathkumarp
    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....
  • mayank1055
    mayank1055
    yup it's not working
  • gaurav.bhorkar
    gaurav.bhorkar
    It works perfectly in Visual C++ Express Edition 2008.
  • bharathkumarp
    bharathkumarp
    ok, i'll check it out... Thanks for the code...
  • silverscorpion
    silverscorpion
    Did you create the hello.exe file? Do you have it in the same directory?
  • gaurav.bhorkar
    gaurav.bhorkar
    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").
  • bharathkumarp
    bharathkumarp
    ya i put the file inside the same directory,but it is not working.....
  • gaurav.bhorkar
    gaurav.bhorkar
    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//Debug.

    Now paste the hello.exe into this directory.

You are reading an archived discussion.

Related Posts

hello . . . I wanted to upload and download file using JSF in my java web application. I have tried using apache tomhawk but facing problem when try to...
hi.... i'm a final yr student. i'm dng ma electronics nd communication engg... i need to do a project based on embedded system. i need topic to b sujjested along...
hi friends, i read "greedy algorithm always make the choice that look best at the moment it may be the optimal solution or may not be". . suppose we have...
😛 i m in 4th sem, b.tech. in eee branch. i want to have some additional course in this summer vacation . please suggest me some course which will be...
Can someone explain the difference between Electric Flux and Magnetic Flux in easy to understand language?