Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.server -> Re: Compilation problems??

Re: Compilation problems??

From: Lawrence Kirby <fred_at_genesis.demon.co.uk>
Date: 1997/08/15
Message-ID: <871640622snz@genesis.demon.co.uk>#1/1

In article <33F359DD.4855_at_ici.net>

           carla_at_ici.net "Alicia Carla Longstreet" writes:

...

>> > int main()
>>
>> int main(void)
>
>This does the same thing as int main(), but is more visible as to your
>intent.

The 2nd form also has the advantage that it isn't obsolescent.

>In any event, the main function will get called with two arguments, the
>number of arguments and the command line.

That might be the case on many implementations but it certainly isn't guaranteed by the standard. An implementation can use different and incompatible calling conventions for fixed argument functions that take 0 and 2 arguments. In that case the implementation would have to ensure that the startup code uses the correct calling convention for the form of main() used by the program.

...

>> The standard allows exit to return EXIT_SUCCESS or
>> EXIT_FAILURE, both of which are defined in stdlib.h;
>> personally, I would simply change this to:
>
>There is NOTHING in the standard that requires a program to return
>either EXIT_SUCCESS or EXIT_FAILURE, the Standard allows ANY INTEGER
>VALUE to be returned, with either exit() or a simple return.

However, 0, EXIT_SUCCESS and EXIT_FAILURE are the only values that have portable meanings.

>My programs often use the exit code to indicate the nature of the
>failure, this way, shell scripts can do more than simply check for
>success or failure, they can take one of several actions based on the
>nature of the error.

That's fine as long as you recognise that doing that has made the program less portable. Consider, for example, OSs where the program result code takes the form of a string. The C implementation will be able to convert 0, EXIT_SUCCESS and EXIT_FAILURE into appropriate strings to indicate success and failure since it knows what they mean. It is anybody's guess what it will do with other values.

>As for using a simple return rather than exit() this is foolish advice.
>exit() offers some excellent features, notably atexit() which allows the
>programmer to define one or more functions to called by exit(). You can
>specify up to 32 functions.

If a return from an initial invocation of main() doesn't call the atexit() registered functions your compiler is badly broken.

-- 
-----------------------------------------
Lawrence Kirby | fred_at_genesis.demon.co.uk
Wilts, England | 70734.126_at_compuserve.com
-----------------------------------------
Received on Fri Aug 15 1997 - 00:00:00 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US