query about undeclared identifier and static variable

Posted on 2004-11-01
Last Modified: 2008-02-01
        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
Question by:software_elico
    LVL 5

    Expert Comment

    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,
    LVL 5

    Expert Comment

    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.

    Author Comment

    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

    LVL 5

    Expert Comment

    >> 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
            /* 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,


    Author Comment

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

    LVL 5

    Accepted Solution

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

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Title # Comments Views Activity
    How do I recover data from my iphone 3g 4 31
    scanf and assignment suppression 13 122
    C++ vs C compilers 13 145
    Grammars for C C++ and java 1 81
    Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

    875 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now