www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/14/22:45:20

From: "John M. Aldrich" <fighteer AT cs DOT net>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: 2 quick questions
Date: Fri, 14 Aug 1998 22:36:33 -0400
Organization: Two pounds of chaos and a pinch of salt.
Lines: 72
Message-ID: <35D4F431.F5D956B8@cs.net>
References: <35D4F001 DOT 4A3E2B2D AT xyz DOT net>
NNTP-Posting-Host: ppp102.cs.net
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Bjorn Hansen wrote:
> 
> 1]  what does the exit code tell you?

The exit code is a tool used by the operating system to get information
from programs.  On a DOS system, the exit code is a 1 byte value that is
stored in the %ax register when your program terminates.  This code is
made available by the operating system to the application that called
your program.

Examples of use:

  In DOS batch files, the "IF ERRORLEVEL ##" command allows you to test
the exit code of a program to do different things.  One common use is
with the 'choice' utility that comes with DOS 6.0 and higher.

  It is a standard convention to use the exit code to indicate if a
program ran successfully.  Programs written according to this convention
return a zero exit code if they ran correctly, and a non-zero exit code
to indicate some form of failure.

  Applications can then test the exit code to determine what to do
next.  A prime example is GNU Make, which, by default, will abort
running a makefile if any command returns a nonzero exit code.

Now, how does it work?  In C, the return value of the main() function is
used as the exit code of a program.  The ANSI standard states that
main() must be declared to return an integer.  It is then conventional
to terminate your program with a 'return 0;' (many prefer 'return
EXIT_SUCCESS;' for its greater clarity - that macro along with
EXIT_FAILURE is defined in <stdlib.h>) statement to indicate that it ran
without problems.  If an error does occur, such as failure to allocate
memory, you may terminate your program with the exit() function, passing
it the exit code to use.  The common convention is 'exit(1);' or
'exit(EXIT_FAILURE);'.  This can be done anywhere in your program.

It's common for C/C++ books and instructors to gloss over the exit code
and return value of main() because it's "too advanced" or "not relevant"
or can be skipped as a "shortcut."  In fact, it's a required part of the
C language.

> 2] why does the compiler have a warning message if you don't use a
> caste? when you do something like this
> 
> int i;
> int *ip;
> ip=i;
> 
> instead of this
> 
> int i;
> int *ip;
> ip=(int *)i;

It complains because assigning any non-pointer type to a pointer is very
likely to be a mistake.  It is only in very rare cases that it's
actually necessary to do, and in those cases you should use a typecast
to let the compiler know that it's ok.

The function of most warnings is to let you know when you're doing
something that is not precisely incorrect, but could (or is likely to)
cause abnormal or erroneous program behavior.

Hope this helps!

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Money is truthful.  If a man speaks |
|       aka Fighteer I       | of his honor, make him pay cash."    |
|   mailto:fighteer AT cs DOT net   |                                      |
| http://www.cs.net/fighteer/|               - Lazarus Long         |
---------------------------------------------------------------------

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019