-2

want to write two c programs. I want to use the value of the local variable of 1st c program(.c file) in my 2nd c program(.c file) also. How can I use the local variable of one program as global variables of another program? That means how can I make those local variables global so that I can use it in another file. Note that I have only 2 files both are .c files(suppose file1.c, file2.c). write a simple program for me to make local variable of one file to global variable of another file.

I want to put the variable inside the main function (int main()) in one program and these variables will work as a global variable in another program.

human
  • 1
  • 1
  • 2
  • 1
    So, we have to write a simple program for you.Is that you want? – krpra Feb 11 '18 at 23:31
  • Actually I have two files. In one file I have some variables inside the function. Now I want to use those variable in another function. I used extern function but it did not work as variables are inside the function. when I used extern it shows error "undefined reference to value x". So what is the solution? – human Feb 11 '18 at 23:55
  • Please explain a little about the "Why?". Also, read https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – Yunnosch Sep 11 '18 at 04:21

1 Answers1

3

There is no standard way to do this, because two processes are separate entities that do not share anything with each other.

However there are techniques (IPC, short for interprocess communication) that allow for processes to share memory and/or communicate with each other. This techniques however are not part of the Standard C Library and are dependent on the operating system, Windows, Linux, Mac will give you different options.

For linux and unix base systems in general there are for example: shared memory, sockets( native BSD sockets, network sockets, unix sockets, file sockets, pipes, etc), 0mq, etc.

I'm afraid your question is just too broad and every IPC option is different from the other and has up- and downsides. This cannot be answered broadly here, you have to be more specific here.

Pablo
  • 13,271
  • 4
  • 39
  • 59
  • Actually I have two files. In one file I have some variables inside the function. Now I want to use those variable in another file. I used extern function but it did not work as variables are inside the function. when I used extern it shows error "undefined reference to value x". So what is the solution? – human Feb 11 '18 at 23:55
  • 2
    @human not trying to be disrespectful, but try reading a C book first and read the chapter about variables. The `extern` keyword is used to tell the compiler that the a global variable has been declared in another compile unit, it is not used to make local variables visible to other functions. – Pablo Feb 11 '18 at 23:58
  • If you want that two different function use the same variable, then you have to pass the variable to the other function or use global variables. – Pablo Feb 12 '18 at 00:00
  • I am new in programming. ok I will read book. So which function I have to use to make local variables visible to other functions? – human Feb 12 '18 at 00:02
  • 2
    @human there is no such function that does that! It depends on **how** and **where** you declare you variables that dictates who "sees" them and who doesn't. Again, you lack basic information of how variables work in C, please read the chapter about variables more carefully. – Pablo Feb 12 '18 at 00:04