0

So I have been trying to learn C on my own. So just a couple of videos and articles and a a book by my side. Although it sounds like a simple concept (I'm sure it is), I dnt think the concept is clear to me.

Can you please quote an example when a variable is declared or defined?(together and separately)

Like in some articles or forums I read they say

Int x; ( x is declared)

Somewhere it's written

Int x; ( x is defined).

When will the memory be allocated to the variable? Again somewhere it is said that variable has to be defined first to get memory allocated and somewhere it's said it is allocated when a variable is declared?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Teaquila
  • 7
  • 3
  • 1
    `int x;` is always a definition – M.M Oct 23 '20 at 07:14
  • Basically ... *declare*: tell the compiler the variable exists and has a defined type, eg: `int foobar;`. The linker will find out **where** it exists. *define* tell the compiler the variable exists **here** and has the specific value, eg: `int foobar = -42;` – pmg Oct 23 '20 at 07:15
  • 1
    @pmg your two cases are both definitions; one has an initializer (providing an initial value makes no difference to the storage class or whatever) – M.M Oct 23 '20 at 07:15
  • Right, first one is a "tentative definition" though :) – pmg Oct 23 '20 at 07:16
  • @M.M if int x; is a definition, can u quote an example for declaration please? – Teaquila Oct 23 '20 at 07:24
  • @pmg I'm afraid I don't know what a tentative definition is. – Teaquila Oct 23 '20 at 07:25
  • When applied to functions, a definition has code (between `{` and `}`), a declaration does not... `int declaration(const char *p);` vs `int definition(const char *p) { return *p - '0'; }` – pmg Oct 23 '20 at 07:35
  • `extern int x;` – M.M Oct 23 '20 at 07:37
  • For reference: [C11, htmlized draft](http://port70.net/~nsz/c/c11/n1570.html) and [C2x PDF draft from WG14](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2478.pdf) – pmg Oct 23 '20 at 07:41
  • 1
    Many at forums mix up the words, so please be careful. If you want certainty, you need to read the standard. Many C books are of doubtful quality when it comes to these issues. – the busybee Oct 23 '20 at 10:30
  • @bolov....thanks a lot...great answer...yes it answered my question :D – Teaquila Oct 24 '20 at 15:09

2 Answers2

0

I hope this give you a small overview about when variables become declared, allocated, initialized, deallocated and vanished (destroyed).

/* global variable A declared and memory for int is allocated */
/* memory will only be unallocated at global program exit */
/* global variable A initialized/defined with value 10 */
int A = 10;

void test(int D){
    /* function test is called by main with argument test(5) */
    /* local variable D is declared, memory for int is allocated */
    /* and initialized with value 5 */
    
    /* add local variable D to global variable A */
    A = A + D;
    
    /* after this point, local variable D will be deallocated */
    /* and will vanish */
}

int main(int argc, char *argv[]){
    /* local variable B declared, memory for int allocated */
    /* and initialized with value 20 */
    int B = 20;
    
    {
        /* local scoped variable C declared, memory for int allocated */
        /* and initialized with value 30 */
        int C = 30;
        /* after this point, local scoped variable C will be */
        /* deallocated and will vanish */
    }
    
    /* 5 is a local scoped const of size int, memory will be allocated */
    test(5);
    /* after this point, local scoped const 5 will be deallocated */
    /* and will vanish */
    
    
    /* after this point, local variable B will be deallocated */
    /* and will vanish */
    return A;
}
paladin
  • 765
  • 6
  • 13
  • 2
    You make it seem like the program source code is processed sequentilally in chronological order, including lines outside of functions. I consider this misleading. Using the expression "allocate" for a global variable is questionable. The effect of settingup/preparing memory for global variables is done at load time, according to information determined by the linking phase, which takes place after compilation. (Downvote not by me however.) – Yunnosch Oct 24 '20 at 07:52
  • global variables will be declared/allocated and initialized before main(). So using this order is the right way. – paladin Oct 24 '20 at 07:58
  • @paladin not the DV but you are totally wrong, _global variables will be declared/allocated and initialized before main()_ that's not how the compiler works, allocated is a wrong term, do you mean filled? – David Ranieri Oct 24 '20 at 08:09
  • You are both right, but I wanted to keep it simple. Memory management is much more complicated than shown in my example. But I hope this gives OP an idea how it might work. – paladin Oct 24 '20 at 08:15
  • _but I wanted to keep it simple_, and this is good, but allocated/deallocated is very confusing, the process of destroying local objects is called "stack unwinding", consider "stacked up" for allocated. – David Ranieri Oct 24 '20 at 08:25
0

Like in some articles or forums I read they say Int x; ( x is declared) Somewhere it's written Int x; ( x is defined).

Actually the int x; declares and defines it at the same time so both are valid but incomplete.

The declaration shows the compiler the type of variable without creating it.

extern int x; // <- this is declaration

Definition creates the the object, if the object was declared before its definition must match the declaration.

extern int x;
int x;

Is valid but

extern int x;
double x; 

is not.

0___________
  • 60,014
  • 4
  • 34
  • 74