www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/06/00:39:51

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199606060420.AA118394811@relay1.geis.com>
Date: Thu, 6 Jun 96 04:20:00 UTC 0000
To: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: Beginners srand error

Reply to message 0434077    from HALIBUT AT FALCO on 06/05/96 11:06AM


>Well, I'd like to see the original posts why void main() is bad
>programming. I'm grateful to learn all my errors; there have been
>quite a few in the one week I've been studying C.

Well, you can ftp the archived mailing list from... hold on... [flips back in
filing cabinet...] around 5/19 to 5/27, but here's a brief discussion:

The correct, full ANSI prototype for main is 'int main( int argc, char **argv
)'
(or 'char *argv[]', but that causes problems with some compilers, as Art
Kagel pointed out.).  This is based on the fact that main() is the basic
interface between the operating system and your program.

First, the 'int'.  Every program that is run on any computer, upon terminating,
returns a value to the operating system.  This is known as the program's
"return value."  With most programs, a return value of zero (0) indicates that
it ran successfully.  Other return values generally indicate varying types of
failure which are program-dependent.  (There are exceptions, of course.)
Anyway, in C, the value that main() returns becomes the program's return
value to the OS.  A normal program would end with 'return 0;' to indicate
success.

Second, the command-line arguments.  Whatever arguments you type in
on the command line when you run your program are passed as arguments
to main().  The 'int argc' stores how _many_ arguments (separated by spaces)
there are, and 'char **argv' is an array of strings which contains the actual
text of those arguments.  Under most operating systems, argument zero
(argv = 1), is the actual name of the program itself, and any additional
arguments
(argv > 1) are whatever you typed after the name of the program.  So, if you
invoke program 'foo' like this:

foo hello there "you guys"

then the values passed to main would look like this:

argc = 4
argv[0] = "foo.exe"  (or whatever)
argv[1] = "hello"
argv[2] = "there"
argv[3] = "you guys"    (text enclosed in quotes is a single argument)
argv[4] = NULL  (don't count on this - use argc to find how many args there
are)

If you don't want or need to use command line arguments, define main()
as 'int main( void )'.  Using the 'void' is not strictly necessary, but it is a
good
habit to get into, as an empty argument list looks far too much like
traditional
C.

>I have another question concerning make files.
>
>Either of my Dos text editor, edit or e, will not seemingly store
>the tab character that is needed at the beginning of a make file
>command line. I had to resort to using a windows app. Is there
>a way to configure either of these editors so I can write make files
>with them.

If you use the Ctrl-P,Ctrl-I combination, Edit will place a tab character
into your text file.  It will look like a small circle.  When you save and
reload the file, the tab character will be replaced by an actual tab.
From then on, every other tab you put in the file will be an actual tab.
We have Microsoft to thank for this wonderful creation.  :(

>Another question is to everyone: what is the best way to view
>and edit all of your program files and still have access to dos
>for the command line to make or run (aside from Rhide). What I've been doing
>is opening files from windows with notepad and then editing them, but
>the limitation of this is I can't save the changes easily. What is
>the best editor or editor of your choice to do this? (Also is there a djgpp
>command line option to make and run at the same time?)

Hmm... I use Edit to edit my programs, save and exit, and compile from
the command line.  Since Edit preserves the contents of the DOS screen,
it's easier to use to correct errors.  I was thinking of possibly trying out
RHIDE, but I don't have a whole lot of free space to work with.

As far as compiling and running, just insert a command in the makefile
to run the program.  Since make aborts if any errors occur, your program
won't actually be run unless you compile successfully.  A sample would
look something like this...

foo.exe : foo.c
        gcc -Wall -g -O -o foo.exe foo.c

run : foo.exe
        foo

Then just type 'make run' to compile and run your program.  (BTW, I know
my sample uses spaces instead of tabs - another misfeature of Genie's
editor is that tab moves you around the window instead of inserting a tab
character.)

Hope this helps!

John

- Raw text -


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