473,469 Members | 1,638 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Static Variables having Block scope

1 New Member
Hi.. can anyone tell wat error/warning I will get on compiling this code:

Fun1()

{

Static int i=5;

i=i+1; //suspicious line is this

print(i)

}
Sep 29 '08 #1
2 2599
JosAH
11,448 Recognized Expert MVP
Hi.. can anyone tell wat error/warning I will get on compiling this code: [ snip ]
Don't you have a compiler at your disposal? That code snippet is full of errors:

- no return type for the function
- 'Static' is not a reserved word
- 'print' is not a function
- no return value

kind regards,

Jos
Sep 29 '08 #2
donbock
2,426 Recognized Expert Top Contributor
Are you asking because you saw somebody else's code using the "static" keyword for the definition of a variable with block scope?

It is entirely legal in C (and I assume C++) for a block-scope variable to be declared "static". Such a variable declaration has the following implications:

1. If there is an explicit initializer, then the variable is assigned that value only once, before any part of your program gets to run. Compare this behavior to an explicitly initialized automatic variable -- the automatic variable is initialized each time you enter the block.

2. If there is no explicit initializer, then the variable is assigned the value 0 (zero) only once, before any part of your program gets to run. Compare this behavior to an automatic variable lacking an initializer -- the automatic variable has an unpredictable initial value.

3. The variable can only be accessed from within the block. Each time you enter the block the variable has the same value it had the last time you left the block. Compare this behavior to an automatic variable -- the value of the automatic variable upon entry to the block depends on its initializer as described in #1 and #2 above.

4. In a multi-threaded application, all threads refer to a single common instance of this variable. (Compare this to automatic variables, where each thread has its own separate and independent set of automatic variables.) This can be useful, because it provides a way for the threads to communicate with each other; and it can be dangerous, because it provides a way for the threads to interfere with each other.

Sometimes I will use the following idiom to insure that certain one-time initialization takes place. With this approach I don't have to rely on the user to call an initialization function first. I probably wouldn't use this idiom for multi-threaded code.
Expand|Select|Wrap|Line Numbers
  1. void func(void) {
  2. static int isInitialized = 0;
  3. if (! isInitialized) {
  4.       funcInit();
  5.       isInitialized = 1;
  6.    }
  7.    <do something useful>
  8. }
Sep 29 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
9
by: vp | last post by:
Can I safely assume that all static variables are initialized as NULL or zero, depending on the types of the variables, no matter on which platform that app is compiled ? Thanks for your help, ...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
18
by: Jack | last post by:
Thanks.
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
6
by: vim | last post by:
hello guys plz tel me the differances between global static and local static. If possible with examples
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
28
by: Why Tea | last post by:
I seem to remember that in ANSI C, all static functions should have their function prototypes listed at the beginning of the file for better consistency (or something like that). I googled and...
41
by: simonl | last post by:
Hi, I've been given the job of sorting out a crash in our product for which we have the crash information and an avi of the event (which can't possibly match but more of that later...) (btw this...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.