Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

query about undeclared identifier and static variable

Posted on 2004-11-01
7
Medium Priority
?
883 Views
Last Modified: 2008-02-01
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
0
Comment
Question by:software_elico
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 5

Expert Comment

by:van_dy
ID: 12470483
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
0
 
LVL 5

Expert Comment

by:van_dy
ID: 12470521
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.
0
 

Author Comment

by:software_elico
ID: 12470654
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



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:van_dy
ID: 12470773
>> 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


0
 

Author Comment

by:software_elico
ID: 12470911
so basically its just one copy?but having different access privileges in different source files.


0
 
LVL 5

Accepted Solution

by:
van_dy earned 300 total points
ID: 12470965
>>  so basically its just one copy?but having different access privileges in different source files.
no, please consider the following,

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


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


file2.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
0
 

Author Comment

by:software_elico
ID: 12471710
thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

610 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