9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
From: dexen deVries <dexen.devries@gmail.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] C question - completely OT, but I'd like to know the answer
Date: Thu,  8 Sep 2011 11:57:07 +0200	[thread overview]
Message-ID: <201109081157.08110.dexen.devries@gmail.com> (raw)
In-Reply-To: <CAFiGbxi4tuz18VvmnEXe2fxcRaSn3UfVJJwMXuEi-tC6=rrK2Q@mail.gmail.com>

On Thursday 08 of September 2011 11:41:05 Winston Kodogo wrote:
> (...)
> I'm puzzled as to why the line "int nigel = 1;" is syntactically OK,
> and although it seems to have declared the variable "nigel" - else the
> following code would fail to compile - has failed to give it the
> initial value of 1, as requested.

consider the `switch' statement a switchboard that jumps to one of the 
`case's.

nigel is not getting initialized because this code path is not executed -- as 
it is not part of any `case' taken by the switch. 

an `automatic' (non-static) variable is initialized by a piece of code, the 
code gets executed whenever flow of control reaches that particular place.

a static variable (`static int inigel = 1') is initialized by static data, 
before any code gets executed -- and only once. static will do what you 
expect.

this is why you can place a static variable outside of any function and it 
still gets initialized.

both kinds have their uses, in different situations.

for reference:

for (int i = 0; i < 8; ++i) {
  int nigel = 1; // this emits some code that gets executed for every loop and 
resets the var to `1'
  printf("nigel: %d,", nigel);
  ++nigel;
}

will print: nigel: 1,nigel: 1,nigel: 1, ...

on the other hand: 

for (int i = 0; i < 8; ++i) {
  static int nigel = 1; // this var is initialized just once before main() 
begins and doesn't reset
  printf("nigel: %d,", nigel);
  ++nigel;
}

will print: nigel: 1,nigel: 2,nigel: 3, ...


-- 
dexen deVries

[[[↓][→]]]

For example, if the first thing in the file is:
   <?kzy irefvba="1.0" rapbqvat="ebg13"?>
an XML parser will recognize that the document is stored in the traditional 
ROT13 encoding.

(( Joe English, http://www.flightlab.com/~joe/sgml/faq-not.txt ))



  reply	other threads:[~2011-09-08  9:57 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-09-08  9:41 Winston Kodogo
2011-09-08  9:57 ` dexen deVries [this message]
2011-09-08 10:44 ` dexen deVries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=201109081157.08110.dexen.devries@gmail.com \
    --to=dexen.devries@gmail.com \
    --cc=9fans@9fans.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).