Thread: How to Share Variable Value from one source file to other isuing Header File

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Mumbai, Maharashtra, India, India
    Posts
    14

    How to Share Variable Value from one source file to other isuing Header File

    I wanted to share the value of a variable from Sender Program to Receive after program and want to calculate difference between send and receive.
    After studying Header file concept I program following three.
    Now I am struck. How to to compile? I link all these file.
    I used following method:
    Code:
      gcc Sender.c  -o Sender Sender.h 
    gcc Receiver.c -o Receiver Student.h
    Than I run Sender and after that Receiver.I per my knowledge, Receiver should give difference but it gives error :
    Code:
      Receiver.c: In function ‘main’:
    Receiver.c:10:42: error: ‘Send’ undeclared (first use in this function)
      printf(" Total Receive is %d\n",Receive-Send);
    Help !!

    Code:
        Sender.c
    
    #include <stdio.h>
    
    int Send ;
    void main()
    {
      printf("How Much you want to Send \n");
      scanf("%d",&Send);
      printf(" Total Send is %d\n",Send);
    }
    Sender.h
    Code:
      #ifdef  _STUDENT_H
    #define _STUDENT_H
    
    extern int Send;
    extern int Receive;
    
    #endif
    Receiver.c
    Code:
      #include <stdio.h>
    #include "Sender.h"
    
    int Receive=100;
    
    void main()
    {
     printf("How Much you Received \n");
     printf(" Total Receive is %d\n",Receive);
     printf(" Total Receive is %d\n",Receive-Send);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You cannot have two main functions in the same program. Are you trying to share values between two different programs? If so, perhaps writing to a file would be an appropriate approach.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by SimplySud View Post
    Now I am struck. How to to compile? I link all these file.
    I used following method:
    Code:
      gcc Sender.c  -o Sender Sender.h 
    gcc Receiver.c -o Receiver Student.h
    That's the wrong method. Header files do not need to be specified on the compiler's command line.

    That aside, those two commands will produce two distinct executables.

    Quote Originally Posted by SimplySud View Post
    Than I run Sender and after that Receiver.I per my knowledge, Receiver should give difference but it gives error :
    Code:
      Receiver.c: In function ‘main’:
    Receiver.c:10:42: error: ‘Send’ undeclared (first use in this function)
      printf(" Total Receive is %d\n",Receive-Send);
    Sorry, but I don't believe you.

    Firstly, that error message is not what you would get if you ran the programs. It is a compilation error which would prevent the executables being built.

    Second, given the code you showed for Receiver.c and Sender.h, the compiler would not give that error message either.

    That leads me to suspect the code you posted in the forum differs from the code you are working with. Modifying the Receiver.c (as you have shown it here) to remove the "#include "Sender.h"" preprocessor directive would result in that error.



    More generally, extern variables (at file scope) allow sharing static variables between functions within a single executable. They do not provide a means of sharing variables between different executables.

    As laserlight said, a program can only have one main() function. If you attempt to build a program with two main() functions then the linker will refuse to build your program, and probably complain about duplication.

    Lastly, main() returns int. Not void.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 10-04-2011, 10:17 PM
  2. Replies: 11
    Last Post: 09-25-2011, 12:22 AM
  3. Replies: 3
    Last Post: 02-10-2009, 02:51 PM
  4. Class methods in header or source file?
    By TriKri in forum C++ Programming
    Replies: 13
    Last Post: 09-17-2007, 05:23 AM
  5. compiling source files and header file at once?
    By 1jackjack in forum C Programming
    Replies: 10
    Last Post: 05-04-2007, 11:06 AM

Tags for this Thread