Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
C Programming
static variable in for loop init
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="Richard Bos, post: 2407747"] Note that in C99, implicit int is no longer legal; in that version of the language, you have to write int main(). int main(void) is better yet. Wherever do you get the idea that you _could_ see 5? There's nothing that gives i any value other than the default 0 that all static objects have. None whatsoever. The only differences, from the programmer's point of view, between a static and an automatic object are: 1. Statics are initialised to 0; automatic variables start out containing garbage unless you explicitly initialise them. This is relevant to the printf() above, since that relies on i _having_ a value, and not containing garbage; but it is of no importance to the for loop, since the first thing you do is give it another value. 2. Statics exist throughout the program's run, and retain their value between calls of the function they're in; automatics are destroyed and recreated every time their declarations are encountered, and get their initial value (or garbage) each time. Since i is in main(), and you don't call main() recursively, this is of no importance to your program; even if you did, it still wouldn't mean a thing to the for loop, since, again, you give i a new value first thing you do in the loop. Richard [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
C Programming
static variable in for loop init
Top