473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Static Variables having Block scope

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 2595
JosAH
11,448 Expert 8TB
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 Expert 2GB
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.