About Variable declaration in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • udhay
    New Member
    • Sep 2006
    • 2

    #1

    About Variable declaration in C

    when will be the memory allocated for a variable?when it declared or defined
  • anitajose
    New Member
    • Aug 2006
    • 11

    #2
    Originally posted by udhay
    when will be the memory allocated for a variable?when it declared or defined
    The memory will be allocated for a variable when it is declared.

    Comment

    • ssehgal2010
      New Member
      • Aug 2006
      • 10

      #3
      hi memory to a veriable allocated the time it is declered
      i.e. int a[100] allocates 100 integer memory locations to this array
      even if u r not using all.

      thats why the concept of dynamic memory allocation came into picture to utilise the memory efficiently.

      Comment

      • gkrishgkm
        New Member
        • Aug 2006
        • 3

        #4
        memory is allocated only when it is defined,
        this is given in yashvant kanetkar's c book

        Comment

        • pukur123
          New Member
          • Sep 2006
          • 61

          #5
          What do you mean by variable declaration and variable definition?

          Even if we declare a variable like

          int a;

          still it has some value (which we are saying normally as garbage).

          So I think in terms of variable, definition and declaration do not matter.

          Yes but in terms of functions, declaration means prototype and definition means the actual code of the function. Here only definition will occupy the space.

          In case of structure also, unless you declare a variable of that structure it does not take any space in memory.

          Comment

          • Loving rascal
            New Member
            • Jul 2007
            • 1

            #6
            Originally posted by udhay
            when will be the memory allocated for a variable?when it declared or defined
            Memory will be allocated only when a variable is defined.
            When a variable is declared it just tells the attribute of the variable either int, float or char..... to the compiler and no memory will be allocated.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              The distinction between declaration and definition does matter: a declaration
              just tells the compiler that a variable exists somewhere. It's up to the linker to
              find it and adjust whatever it has to adjust. A definition tells the compiler "here
              is the variable; do something with it, like allocating storage for it". A definition
              whithout a preceeding declaration is a declaration (and a definition) by itself.
              A 'tentative' definition is a declaration that *can* be a definition, as in 'int x;'

              Multiple tentative definitions are considered declarations. The 'extern' keyword
              isn't obsolete.

              kind regards,

              Jos

              Comment

              Working...