• C

query about undeclared identifier and static variable

        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()

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
Who is Participating?
>>  so basically its just one copy?but having different access privileges in different source files.
no, please consider the following,

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

#include "file.h"
.....// rest of your file1.c

#include "file.h"
..........// rest of your file2.c

as i said earlier, the static int a in both the files (file1.c and file2.c) are 2 separate variables.
they are two seperate storage locations. the only restriction is that the functions in file1.c are
allowed to access only the 'a' which is in their scope and same holds for file2.c
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,
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.
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

software_elicoAuthor Commented:
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


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

#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,

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

software_elicoAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.