Passing paramters to another program in C ?

Posted on 2007-10-02
Last Modified: 2010-04-15
I have the fallowing program with code something like this


int main()
  char test[255];
  printf("write here: ");
  scanf("%s", test);
  return 0;

My question is how to pass some data into test variable from another program

I now how to do it when program use argv/argc, with system("./program blabla-data"), but my question is how to pass it into the test variable.

It must work under Linux.

Thank you
Question by:triz99
    LVL 45

    Expert Comment

    Hi triz99,

    You can't directly pass data from one program to another by setting variable in memory.  Each program has its own memory and doesn't allow other programs to write into it.

    You'll have to open a link between the two programs and exchange information that way.  You can get as involved as a full client-server implementation, but that's clearly overkill.

    Pipes will do nicely.  Check out the pipe() and popen() functions.  They can establish a comm link between programs so that you can use fgets() and fputs() to exchange data between the programs.

    Good Luck,
    LVL 53

    Accepted Solution

    Depending on what you want to do, it could be as simple as this :

            program1 > out.txt
            program2 < out.txt

    The first program writes its output to the file called out.txt, and the second program reads its input from that same file.

    If your operating system supports pipes, it becomes even easier :

            program1 | program2

    Note that this means that the standard output is for the first program, and the standard input for the second program are diverted ...
    LVL 9

    Expert Comment

    do you know how to call system commands?

    Here is how.

    Now what you can do it, compile your other program as first.out and then  you can call that program from another program like this
    #include <stdio.h>
    #include <stdlib.h>

    int main ()
      int res;

      puts ("Trying to execute command DIR");

      res = system ("./first.out arg1 arg2");

      if (res == -1)
          puts ("Error executing DIR");
          puts ("Command successfully executed");

      return 0;

    In your first program you can get these arg1 and arg2 and assign it to test variable.
    LVL 8

    Assisted Solution

    You could use shared memory under linux.

    Here is a link to "Advanced Linux Programming": (see chapter 5, if you are interested)

    The source code examples:

    LVL 8

    Expert Comment

    Chapter 5 discusses more than just shared memory. It is all about interprocess communications.
    LVL 5

    Expert Comment

    You can use one of the following:
    (1)  File
    (2) Shared memory
    (3) messaging..

    Expert Comment

    Ex for using argc/argv in a program

    main(int argc,
            char *argv[])
        char test[255];
        if (argc >= 2) {
            strcat(test, argv[1]);
        printf("%s", test);

    I check if argc is equal or larger  the 2 to make sure that there are arguments into the program (argv[0] is equal to the program that is started)

    As for launching other programs you can use execv(char *path, char *argv[])
    path is the application you want to start.
    argv is a list of arguments that will end up in argv for that program (remember that argv[0] should always contain the program you are launching in path)

    ex. of launching process in linux

    main(int argc,
             char *argv[])
        char *next_program_args[3];
        next_program_args[0] = strdup("/usr/bin/less");
        next_program_args[1] = strdup("/etc/passwd");
        next_program_args[2] = NULL; /* This is to indicate to next progrma that there are no more args*/

        execv(next_program_args[0], next_program_args);

    As for includes needed for these examples use man for the different calles to get the info

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now