Defination of a varibale/ function in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sushant khandebharad
    New Member
    • May 2010
    • 2

    Defination of a varibale/ function in C

    While reading on difference between declaration and definition of variable ; I encountered following statement-
    "In Definition the compiler actually reserves memory from the program's image."
    I didn't get what exactly does it mean.
    Which memory it is referring to ? Is it primary memory ?
    But primary memory (RAM) will come into picture when the program is running and compiler is far before that.

    I would really appreciate if somebody could please elaborate on this.

    Thanks.
  • Dheeraj Joshi
    Recognized Expert Top Contributor
    • Jul 2009
    • 1129

    #2
    During declaration the memory is allocated according to the type.

    If char 1 byte, if int 4 byte etc.
    While definition the value of that variable is stored in that address.

    Regards
    Dheeraj Joshi

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      A declaration informs the compiler that a variable exists. This only has an effect on the internal symbol table used by the compiler while it translates your source code. Declarations do not appear in the compiled object code that is the primary output of the compiler.

      A definition is a directive to the compiler that a variable has to be created here. This affects the internal symbol table, but the compiled object code will also contain the necessary instructions to create the variable. The precise sequence of instructions varies depending on what kind of variable was defined.

      Comment

      • sushant khandebharad
        New Member
        • May 2010
        • 2

        #4
        Hi Dheeraj and donbock,
        thanks for your reply...!

        So, when the declaration statement is getting compiled , no compile object code is generated, rather, declaration statement is used to get the compiler know that this particular identifier exists, and it has its its effect on symbol table.
        However, while compiling 'definition' statement, compiler generates some compiled object code statement that will eventually lead to memory allocation for variable during execution of program.

        donbock, could you please verify my understanding.

        Thanks,
        -Sushant.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Here's a declaration:

          Code:
          extern int val;
          This declares that an integer named val exists but it's not in this file. This tell the compuiler to not worry about not hav in actual variable that the linker will sort it out.

          Here's another declaration:

          Code:
          struct Data
          {
               int val;
               int anotherVal;
          };
          This says that should I define a variable of type data that this is how it is to be constructed. The actual definition is:

          Code:
          Data myVariable;     //memory for a struct Data actually allocated.
          It is is declaration that goes in the header file and the definition that goes in the implementation file.

          Comment

          Working...