Basic Concept

This is a discussion on Basic Concept within the C Programming forums, part of the General Programming Boards category; In the statement: int a; please explain me whether here 'a' is simply an alias given to the memory location ...

  1. #1
    Unregistered
    Guest

    Question Basic Concept

    In the statement:
    int a;
    please explain me whether here 'a' is simply an alias given to the memory location allocated for this int variable or something else.

    and will the memory be allocated in the above statement....or it will be allocated only when we assign some integer value to it.

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    41
    If i'm right memeory is allocated for int a; when the program is compiled and run. Because a int "integer" varable is 2 bytes in size. This varys on diffrent OS "operating systems". So memory allocation happens before anything is placed in it. I dont really know how to explain the other part of your question.
    Shouldent NULL be, 78, 85, 76, 76, or just 0 or, 4E, 55, 4C, 4C

  3. #3
    Registered User char's Avatar
    Join Date
    Apr 2002
    Posts
    31
    In C we have Declarations and Definitions. Declaration is like an alias, as you say. Definition is the process of allocating memory for that variable.

    When you declare a variable you are just telling the compiler that you are going to use that variable, that is defined somewhere else. When you define a variable you allocate memory for it (why I am feeling that I am telling this twice?)

    In most cases (like in your example) you both define and declare a variable. You will see situations where a variable is defined somewhere and declared somewhere else. A variable is defined once (allocated once) but can be declared many times.

    Consider this piece of code:

    Code:
    int main (void)
    {
      ...;
      ...;
    
      ...;
    
    }
    
    void fun1 (void)
    {
      ...;
    }
    
    void fun2 (void)
    {
      extern int a;
    
      ...;
    }
    
    int a;
    
    void fun3 (void)
    {
      ...;
      ...;
    }
    extern int a; is a declaration of a variable that is defined somewhere else.

    int a; is its definition.

    Only functions fun2 and fun3 may use variable a. But this is another story.

    By the way, you dont need to compile the code above

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [ANN] New script engine (Basic sintax)
    By MKTMK in forum C++ Programming
    Replies: 1
    Last Post: 11-01-2005, 10:28 AM
  2. what are your thoughts on visual basic?
    By orion- in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 09-22-2005, 05:28 AM
  3. linked list - basic concept.
    By Vber in forum C Programming
    Replies: 4
    Last Post: 04-11-2003, 03:30 PM
  4. basic doubt in pointer concept
    By sanju in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 12:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21