Mail Archives: djgpp/1996/09/14/08:54:38
Samuel Vincent wrote:
> > main(argc,argv)
> > int argc;
> > char *argv[];
> > {
>
> This is very old C.. I would recommend using ANSI C as follows:
>
> int main(int argc, char *argv[]) {
Presently, traditional-style function declarations are still supported in ANSI
C; that is, curious as it is to say, it is ANSI C compliant to use them. (At
some point in the future, though, they may disappear, but exclusion is
unlikely to become an official part of the standard for a long time to come.)
> > FILE *fp,*fopen();
>
> The fopen function is declared in stdio.h I believe. 1) You don't need to
> declare it further here.
Right.
> 2) You seem to have declared it incorrectly
> as taking no arguments...
In C++, yes; in C, no. In C, a function prototype declaration with nothing
but whitespace inside the function parentheses indicates an undefined (i.e.,
unchecked) number and types of arguments. (In a function _definition_, it
means no arguments.)
In C++, the restriction is changed to be the same as declaration a function
taking no arguments.
> Your call to fopen should be: fopen(argv[1], "wb")
> The b specifies binary mode.
>
> > for(i=0; i < 256; ++i) fputc((char)i,fp);
>
> This will put 256 bytes into the file. (I foresee possible
> problems with your definition of char buf[255] only being able to
> handle 255 characters.)
Yes. Plus a minor nitpick -- the "character" given to fputc is an int, not a
char, so the cast is wasteful.
> That exit() should really just be: return 0;
There's nothing wrong with using exit to terminate a program, even if you're
in main.
--
Erik Max Francis, &tSftDotIotE http://www.alcyone.com/max/ max AT alcyone DOT com
San Jose, California ICBM 37 20 07 N 121 53 38 W R^4: the 4th R is respect
"Out from his breast/his soul went to seek/the doom of the just." -- _Beowulf_
- Raw text -