explain this output of this program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rampraveen
    New Member
    • Apr 2010
    • 37

    #1

    explain this output of this program

    *************** *************** *************** *********
    program for sharing memory
    *************** *************** *************** *********
    #include"stdio. h"
    #include"conio. h"
    #include"file12 .c"
    int a,b,c;
    void main()
    {
    clrscr();
    printf("\nvalue of a,b and c before extern");
    printf("\nADDRE SS of a--> %x",&a);
    printf("\nADDRE SS of b--> %x",&b);
    printf("\nADDRE SS of c--> %x",&c);
    printf("\n\nval ue of function function()--> %d",function()) ;
    printf("\n\nval ue of function div()--> %d",div());
    printf("\n\nval ue of function mul--> %d",mul());
    printf("\n\nval ue of a,b and c after extern");
    printf("\nADDRE SS of a--> %x",&a);
    printf("\nADDRE SS of b--> %x",&b);
    printf("\nADDRE SS of c--> %x",&c);
    getch();
    }
    int function()
    {
    a=24;
    b=a*a;
    return(b);
    }/*completed*/
    *************** *************** *************** *************
    "file12.c"
    *************** *************** *************** *************
    #include"stdio. h"
    #include"conio. h"
    extern int a,b,c;

    int mul()
    {
    a=40;
    b=30;
    c=a+b/4;
    return(c);
    }
    int div()
    {
    c=73;
    b=24;
    a=c/b;
    return(a);
    }/*completed*/
    *************** *************** *************** *************** ****
    output of this program
    *************** *************** *************** *************** ******
    ADDRESS of a,b and c before extern

    ADDRESS of a--> 582
    ADDRESS of b--> 584
    ADDRESS of c--> 586

    value of function function()--> 576
    value of function MUL()--> 47
    value of function DIV--> 3

    ADDRESS of a,b and c after extern
    ADDRESS of a--> 582
    ADDRESS of b--> 584
    ADDRESS of c--> 586

    *************** *************** *************** *************** *
    i am having MY QUESTION
    *************** *************** *************** *************** *
    1)In this output THE ADDRESS OF a,b,c having 582,584,586.... .....how this number comes.
    2)then in that second program (file12.c) having extern int a,b,c...in one book the author told ,"this extern is used for sharing memory". so only the value of a,b,c are not changed in the output..but one time am executing this program without extern...at tat time also it shows the same value as output...i changed the extern from this program to that program and also vice versa..whatever i did the output come as it is ...what is the use of extern....
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    The extern means that the variable is EXTERNAL to the .c file. Usually when you do not create a variable before using it the compiler givews you an error:

    Code:
    var = 10;
    However, if you know there is a var that is a global variable in another .c file andf you want to to share it you code:

    Code:
    extern int var;
    var = 10;
    The compiler marks var as unresolved bit does not give you an error. Later the linker will locate that global variable and connect addresses so you can share it.

    You really need the extern other wise you have to:
    Code:
    int var;
    var = 10;
    and this creates a var in this file in addition to the var in the other file. You may now crash your link by having var defined more than once.

    Comment

    Working...