Click to See Complete Forum and Search --> : stoopid C problem...


labwerx
02-15-2006, 06:37 PM
I'm currently learning C from various books and typed this little program in:

#include <stdio.h>

int main(int argc, char *argv[])
{
int arg;

for(arg = 0; arg < argc; arg++) {
if(argv[arg][0] == '-')
printf("option: %s\n", argv[arg]+1);
else
printf("argument %d: %s\n", arg, argv[arg]);
}
exit(0);
} When I compile it, I get the following error from GCC:

stuff.c: In function ‘main’:
stuff.c:13: warning: incompatible implicit declaration of built-in function ‘exit’

What's the problem? I've typed it as it appears in the book and get an error. I'm really new to C and don't quite have the syntax down yet.

(edit) Looks like the formatting has been destroyed by the board.

truls
02-15-2006, 07:49 PM
#include <stdlib.h>

You are missing the include file that defines the exit() function. "Implicit declaration" generally means that gcc did not find a definition for a function that you are calling. This usually means that you are missing an include file. To find out which include file defines a system function use: "man 3 <function>" which will list the include file under the "Synopsis" chapter.

labwerx
02-15-2006, 07:53 PM
that did it, thanks a lot.


You would figure that something like that would have been included in the book's code.

bwkaz
02-15-2006, 08:13 PM
Not all books follow the C89 and/or C99 standards completely. (AFAIK the location of the exit() prototype is defined by the standard.) Some C compiler environments include stdlib from stdio (and I would suspect that the person writing the book used one of these environments), but you should never depend on that. Always check the documentation for the functions you're using, and include the appropriate headers.

(Side note: The board isn't exactly the problem with your code, it's more HTML that's the problem. Web browsers collapse all whitespace by default, unless you tell them not to with a CSS rule. Adding and tags around your code will put your code into a scrollable region that has this CSS rule, so the whitespace won't be removed by browsers. I've added the tags to your post so you can see how to use them. :))

truls
02-16-2006, 12:57 AM
It would of course help a bit if ANSI sold the C99 standard for something less than the $281 they are charging now. Why the C++ standard can be downloaded for $20 or thereabouts while the C99 standard has to cost a fortune I do not know.

labwerx
02-16-2006, 12:07 PM
Here's the kicker: the book in question is "Beginning Linux Programing" by Richard Stones and Neil Matthew... It's a frickin LINUX book... It should have had the line in there.


Thanks for the advice on code snippets, bwkaz. I have the feeling that I'm going to post more code in the coming weeks. They're trying to "convert" me from sysadmin to programmer on the job so I have to pick up C, Python, Java, and tcl as fast as possible.

bwkaz
02-16-2006, 08:36 PM
It would of course help a bit if ANSI sold the C99 standard for something less than the $281 they are charging now. Yes, yes it would. ;)

Basically, all I know about C99 I learned from gcc's -std=c99 and -std=gnu99 switches. :D I haven't actually read the standard; this means that it could be wrong if there are bugs in gcc. Hmm.

Anyway, the Linux manpages (in section 2 or 3) almost always tell you which header(s) you need for each function; most of the time, I think this follows the C standard (if it's a Standard C function), not the gcc implementation.