"Indika Bandara" <[EMAIL PROTECTED]> wrote:
> "peternilsson42" <peternilsson42@> wrote:
> > "Indika Bandara" <indikabandara19@> wrote:
> > > > > func(int i)
> > > > > {
> > > > > char buf[i];
> > > >
> > > > I don't see why they are not giving you compile errors
> > > > since the size of an array is supposed to be a const
> > > > or a literal, not a variable.
> > >
> > > so u are saying in declaration of a array size should be a
> > > compile time constant?
> >
> > For C90, the most common implementation of C, yes. C99 has
> > variable length arrays.
> >
> > Note that most compilers are not 'conforming' in their
> > default modes. Note also that high warning levels do not
> > make your compiler more conforming.
>
> u mean gcc doesn't comply to those standards??
To comply with C90, the implementation need only output
a required diagnostic, e.g. a warning. Because the construct
is not a feature of C90, the implementation can do what it
likes with the source code, including directly supporting
it as a non-standard extension.
Note that it is valid in C99, which means a conforming C99
implementation must observe the standard (C99) semantics.
Conformance relates to the behaviour of strictly conforming
code, and issuance of required diagnostics. The later
notwithstanding, the behaviour of non strictly conforming
source code does not affect an implementation's conformance.
In other words, implementations can support extensions so
long as the beep when necessary and don't change the behaviour
of strictly conforming programs.
> anyway is the behavior undefined??
In C90, yes.
> but as it seems, these work OK...
That's a perfectly valid form of 'undefined behaviour'.
UB means _anything_ can happen, from something very bad,
to something very... nice.
> did u notice the *nice* output gcc gives?
It's not relevant to describing the the behaviour of code.
What matters is the semantics you can _prove_, not the
output you happen to observe.
Not every piece of code needs to be maximally portable,
but if you write code that is non standard, it should
still conform to _some_ documentation. That documentation
and all restrictions should be documented as part of the
program (at least for non-trivial programs).
Now variable length arrays have proven useful enough to
actually make it into the C99 standard. Indeed, gcc isn't
the only C90 compiler to offer the extension. But that
doesn't mean that C90 compilers offer the _same_ extension.
A classic example is snprintf(). It was a noticable
absentee from C90. So much so that many C90 implementations
offered the function as a non-conforming extension. The
function was added to C99, but the specification was
actually different to how many C90 implementations had
implemented it!
So, if you use snprintf, particularly the return value,
then it's _very_ important to document which flavour
of snprintf that you are using!
It's perfectly possible to use snprintf() and never have
a problem with it, until someone tries to compile it with
another C90 or C99 implementation where the behaviour is
actually different to the behaviour you've taken for
granted.
--
Peter