Link to home
Start Free TrialLog in
Avatar of software_elico
software_elico

asked on

query about undeclared identifier and static variable

Hello
        i have two simple queries

1.if i have an undeclared identifier in a program and i assign it any type say an int or a string,like this

avv="expert"; //declaration without type
void main()
{
  printf("%s",avv);
}

does the compiler check the type and assign it implicitly or is it dependant on the host processor?

the above code compiles and prints successfully.

2.If i have a static variable say static int a; declared in a header file say file.h

now if this header file is used in multiple source files,does each source file hold an exclusive copy of the static or is it just one copy,also is there a problem if i do the above?can i change the static variable in any source file and use it?

thanking you
elico
Avatar of van_dy
van_dy

1)  the type assigned to avv is int.
     it is not changed, the address of "expert"
      is stored in avv. (you compiler must
       issue a warning about converting pointer to an integer).
       However the program outputs correct result becase:
          printf("%s" .... requires a pointer argument, the integer
        supplied , avv, is treated as pointer and the string stored
         at the memory address contained in avv is printed out successfully.

2)  static variables are local to translation units. so various static variables introduced
      in the program due to inclusion of you header in various source files(translation units)
    will be different.

hope this helps,
van_dy
Regarding 1)

     it is possible to lose information in such conversions.(like pointer to int)
for example if the address of your string were beyond the value representable
by an integer on your host, then the program wont print correctly.
Avatar of software_elico

ASKER

about query 1 iam clear thanks

about 2:say i have a

header file.h
and declare

static int a;

1)do i have to initialize this variable?
2)say i initialized it :
static int a=10;

and two source files
file1.c  and file2.c are having file.h

can i change the value of variable a in file1.c and if i can then do i have to use

::a to change it?

thanks again



>> do i have to initialize this variable?

static variables are initialised to 0 by default.

the scope of the static variable a in file1.c is restricted to that
file itself, similarily the scope of the static variable a in file2.c is restricted
to file2.c itself.  Yes you can change the value of a in file1.c as and when required

consider

file.h
#include <stdio.h>
static int a =  10;

file1.c
#include "file.h"

int main()
{
          int state  =  0;                // some variable which may change
        /* ...you can use 'a' here and change it as well without
          using ::a.     :: is used in c++ to refer to a variable
          that is in global scope. it is not a part of C.  Now if
          you have another block in main as follows ...*/

           printf("%d\n", a*10);
                 // perform some calculation
          if(state > 0){
                   int a = 5;   // this 'a' is local to this if() block, any change you do to a here will not effect static int a declred via file.h.
                   ......
          }

          a = 20;             // this will effect the static int a declared by file.h, we are back in the scope of previous declaration of a.
               
}

Hope this helps,
van_dy


so basically its just one copy?but having different access privileges in different source files.


ASKER CERTIFIED SOLUTION
Avatar of van_dy
van_dy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks