* [9fans] C question - completely OT, but I'd like to know the answer
@ 2011-09-08 9:41 Winston Kodogo
2011-09-08 9:57 ` dexen deVries
2011-09-08 10:44 ` dexen deVries
0 siblings, 2 replies; 3+ messages in thread
From: Winston Kodogo @ 2011-09-08 9:41 UTC (permalink / raw)
To: 9fans
Sorry to bother the list, but I thought I might get a sensible answer
here from the few remaining people in the world who actually
understand C.
The following bit of code seems to be more or less syntactically OK:
switch (nurdge)
{
int nigel = 1;
case 0:
if(nigel == 1)
printf("nigel is one.\n");
else
printf("nigel is not one.\n");
default:
printf("The value of nigel is %d", nigel);
}
Something close to this compiles under my C compiler, and yet the
variable "nigel" is not initialised, and the test inside the first
case test is pretty much certain to print "nigel is not one". Although
my C++ compiler does complain about an uninitialised variable.
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [9fans] C question - completely OT, but I'd like to know the answer
2011-09-08 9:41 [9fans] C question - completely OT, but I'd like to know the answer Winston Kodogo
@ 2011-09-08 9:57 ` dexen deVries
2011-09-08 10:44 ` dexen deVries
1 sibling, 0 replies; 3+ messages in thread
From: dexen deVries @ 2011-09-08 9:57 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
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 ))
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [9fans] C question - completely OT, but I'd like to know the answer
2011-09-08 9:41 [9fans] C question - completely OT, but I'd like to know the answer Winston Kodogo
2011-09-08 9:57 ` dexen deVries
@ 2011-09-08 10:44 ` dexen deVries
1 sibling, 0 replies; 3+ messages in thread
From: dexen deVries @ 2011-09-08 10:44 UTC (permalink / raw)
To: Fans of the OS Plan 9 from Bell Labs
On Thursday 08 of September 2011 11:41:05 Winston Kodogo wrote:
> (stuff with switch/case)
btw., the semantics of switch/case in C are quite elastic; there's a neat use
of it:
http://en.wikipedia.org/wiki/Duffs_device
do/while interleaved with switch/case.
--
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 ))
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2011-09-08 10:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-08 9:41 [9fans] C question - completely OT, but I'd like to know the answer Winston Kodogo
2011-09-08 9:57 ` dexen deVries
2011-09-08 10:44 ` dexen deVries
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).