简体   繁体   中英

Can we pass variables from one c program to another c program?

So I want to pass a variable from one c program to another c program.

For example:

main()
{
char str[]="Hello,there!";
system("program2.exe");
}

And I want to use str[] in program2.exe . Is there a way to pass a variable to another program?

I used files to write data from first program and read data from second program but I want to know is there any other way to do this?

Is it good to use files for passing data from program to another?

You can't literally pass a variable between two processes because each process on a system will generally have it's own memory space - each variable belongs to a process and therefore can't be accessed from another process (or so I believe). But you can pass data between processes using pipes .

Pipes are buffers that are implemented by the OS and are a much more effective method of sharing data between processes than files (yes, you can use files for inter-process communication). This is because files must be written to a disk before being accessed which makes them slow for inter-process communication. You'd also have to implement some kind of method for ensuring the two processes don't corrupt a file when reading and writing to it.

Also, pipes can be used to ensure continuous communication between two processes, making them useful in many situations. When using half duplex pipes (linked above), you can have a pipe for each process to establish a communication channel between them (ie a one way communication channel for each).

you can:
1) pass arguments to a program. 2) use sockets to communicate between processes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM