From: Ullrich von Bassewitz (uz_at_musoftware.de)
Date: 2005-04-01 12:45:53
On Fri, Apr 01, 2005 at 11:52:43AM +0200, Gabor Lenart wrote:
> ? Really? I've never seen this rule! In fact when I was learnt to code C it
> was told that uninitialized variables has TOTALLY UNDEFINED value, you can't
> assume anything.
[...]
> But as I've written: I was told that it's COMPLETLY illegal to use a variable which
> was not initalized before ... So in thoery you should not do this however. Or at least
> you can, but it is NOT PORTABLE code in C, if you try to compile it on other platform
> and/or compiler.
I can usually be trusted when it comes to portable C code:-) A lot of C
"programmers" have never read the standard, use try and error to find
something out and then believe it's the truth.
Fortunately, there's an official ISO C standard named "ISO/IEC 9899:1999 (E)"
and among a lot of other, very interesting things it says in chapter 6.7.8
"Initialization", paragraph 10:
------------------------------------------------------------------------------
10 If an object that has automatic storage duration is not initialized
explicitly, its value is indeterminate. If an object that has static
storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned)
zero;
- if it is an aggregate, every member is initialized (recursively)
according to these rules;
- if it is a union, the first named member is initialized (recursively)
according to these rules.
------------------------------------------------------------------------------
My suggestion for every C programmer is to keep a copy of the standard
available and have a look into it. It contains a lot of interesting things and
debunks a whole lot of myths many C "programmers" believe:
* Did you know that C does not require a char to have 8 bits? It does not
require ints to have 16, and longs to have 32 bits either.
* Did you know that a NULL pointer must not be represented by a binary zero,
and that - despite this fact - a compare like
if (p == 0) ...
does still test p for the NULL pointer? Even
if (p) ...
is a correct test for the NULL pointer, even on platforms where the NULL
pointer is represented by something like 0x12345678.
* Did you know that a main program declaration of
void main (void)
is non portable and may be refused by the compiler?
* Did you know that
char f = 5["abcdefg"];
is completely legal C?
It's always better to know instead of just believe. And the ISO C standard is
the way to knowledge for C programmers.
Regards
Uz
--
Ullrich von Bassewitz uz@musoftware.de
Message was sent through the cbm-hackers mailing list
Archive generated by hypermail pre-2.1.8.