Re: [Hampshire] C dialects

Top Page

Reply to this message
Author: Gordon Scott
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] C dialects
On Mon, 9 Jul 2007, Chris Liddell wrote:

> Date: Mon, 09 Jul 2007 13:24:48 +0100
> From: Chris Liddell <cjl@???>
> Reply-To: Hampshire LUG Discussion List <hampshire@???>
> To: Hampshire LUG Discussion List <hampshire@???>
> Subject: Re: [Hampshire] C dialects
>
> At 13:19 09/07/2007 +0100, you wrote:
>
> > <SNIP>
> > For instance Dev is happy with
> >
> > main()
> > {
> > code
> > }
> >
> > but gcc won't have that, it insists on
> >
> > main (void)
> > {
> > code
> > }
> >
> > A small difference - but most irritating if you're very new to C as I am.
>
> Both are /wrong/!


Not strictly true. The former was early K&R style, where the compiler
would allow some outrageous assumption. IMHO a good compiler should at
least protest a little about it though, as it's poor style and
dangerous.

>
> The main() function is:
>
> int main (int argc, char **argv)
>
> or
>
> int main (int argc, char *argv[])


Those of course are ANSI-valid.

ANSI is, IMHO, the way to work as it gives the compiler all the right
hints and helps to avoid embarrasing mistakes.

> If you're new to C, it would be best to get into good habits from the start!


I'd agree wholeheartedly with that! C _can_ be a good safe language if
used correctly and defensively. It can also be a blunderbus with which
to blow away your legs and everybody else's legs if you abuse it.

Defensive programming uses tricks like:

if( 0 == MyVar)
{
}

Which is fine, but would flag an error if you did:

if( 0 = MyVar )
{
}

where

if( MyVar = 0 )
{
}

Can compile fine, but fails possibly subtly. Some compilers will warn of
probable accidental assignments.

ATB,
    Gordon.
-- 
Gordon Scott                  http://www.gscott.co.uk


        Linux ... Because I like to *get* there today.