LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2010, 07:47 PM   #1
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Rep: Reputation: 15
C - Does a static declaration reinitate the value when recalled?


I'm guesing not, but would a code of something like:
Code:
static int foo = 0;
when recalled reset the value to zero? Because although it's already been initialized and that doesn't happen again, does the = 0 part still effect it? Like I said, it doesn't make sense to me that it would but I decided I'd ask. Thanks!
 
Old 07-10-2010, 09:30 PM   #2
easuter
Member
 
Registered: Dec 2005
Location: Portugal
Distribution: Slackware64 13.0, Slackware64 13.1
Posts: 538

Rep: Reputation: 62
When in doubt about small things like this in C, I like to write a quick program to test the problem (it generally saves tons more time than posting on a forum):

Code:
#include <stdio.h>

void test_static(int bar)
{
        static int foo = 0;
        printf("foo is: %d\n", foo);
        foo = bar;
        printf("foo is: %d\n", foo);
}

int main(void)
{
        test_static(2);
        test_static(100);

        return 0;
}
The output from that is:

Code:
foo is: 0
foo is: 2
foo is: 2
foo is: 100
I guess that answers your question
BTW, setting a static variable to zero is unnecessary, since K&R's book indicates that static variables get automatically initialized to zero.

Last edited by easuter; 07-10-2010 at 09:31 PM.
 
Old 07-10-2010, 11:32 PM   #3
golmschenk
Member
 
Registered: Nov 2009
Posts: 144

Original Poster
Rep: Reputation: 15
Awesome. Perfect thanks. Yeah, I had considered writing a small program to test it, but I was working on another one and decided that someone would know on here so I could be lazy and not do it myself. Thanks a ton! Very descriptive answer.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Java non-static variable cannot be referenced from a static context issue rickrvo Programming 2 02-12-2010 03:26 PM
How to initialize a static array of a class in a static member function lali.p Programming 9 02-16-2008 09:27 AM
Making a static lib from objects + static libs kovacsp Programming 2 12-19-2007 12:53 PM
Howto Assign Multiple Static Public IP Addresses under SBC's PPPoE Static Ip system o trekgraham Linux - Networking 8 04-17-2007 10:51 AM
FC2 Overriding static if in favor of dhcp system set for static pkraus109 Linux - Networking 8 09-21-2004 11:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration