Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Passing paramters to another program in C ?

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
2 Solutions
Kent OlsenData Warehouse Architect / DBACommented:
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,
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 ...
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.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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:


Chapter 5 discusses more than just shared memory. It is all about interprocess communications.
You can use one of the following:
(1)  File
(2) Shared memory
(3) messaging..
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now