Sei sulla pagina 1di 6

Basic C programming

Typically, tutorials like this start you out with a "Hello, World" program, a very simple program that just prints 'Hello, World" or something like it on your screen. I'm going to do it a bit differently. I am going to start you off with mistakes, and show you what happens? Why? Because you are going to make some or all of these mistakes anyway, and they will frustrate and annoy you. I can't possibly imagine every mistake you might make, but I can remember most of the screwups I have made, so that's at least a good beginning? Ready for some code that won't work? Let's do it:

Hello World
Create this file with a text editor:
echo "Hello World";

You've learned the very first thing about C - lines always end with a ";". Well, that's not quite true, but this kind of line does. Call it "hello.c" (because that's what these are always called).

Compiling
You have created a C "source" file - the human readable source for your program. It needs to be "compiled" - turned into machine language that your cpu can actually use. There are two basic ways you can do that: use "gcc" (or "cc", which is usually the same thing) or "make". Ordinarily, "make" requires a "makefile" to tell it what to do, but for very simple files like this, "make" is smart enough to do it without help - just "make hello" is all it needs. Notice that your source file is "hello.c" but you would say "make hello" - no ".c". Make would actually run gcc, and specifically it would do "gcc -o hello hello.c". You could also do "gcc hello.c", but (assuming it were successful), that would create an executable (a program ready to use) called "a.out" rather than "hello". That -o option to gcc tells it what you want your program to be called. You could say "gcc -o mybigbadprogram hello.c" if you wanted to. What's with the "a.out"? There has always, since the early days of Unix, been a header file called a.out.h which described the format that an executable needed to be for the kernel to use it. I lied a little bit when I said compiling creates machine language - the kernel needs a bit more structure around that machine language in order to know how to recognize it as an executable at all, and then how to properly set it all up in memory somewhere. That's all deep voodoo stuff though, and we are a long way from worrying about that. But that's why gcc creates "a.out" if you don't tell it otherwise. So now, simply type "make hello". You should see:
cc hello.c -o hello hello.c:1: syntax error before string constant make: *** [hello] Error 1

Hmmm. What's wrong? Well, lot's of things, but you don't know that yet. And what you really could use right now is something that used to come with Unix systems, but usually gets left out of Linux. That something is called "lint" and you can go get it from http://linux.maruhn.com/sec/lclint.html
Commented [1]: google_ad_section_end

Commented [2]: google_ad_section_start

I'll wait here while you go get that. For my RedHat box, I got the "lclint-2.5q-3.i386.rpm" and just did an "rpm -iv lclint*". If you went searching for "linux lint" ('cuz you don't trust me, do you?), I bet you found a page or two that told you "gcc -Wall is roughly equivalent to lint". Yeah. And I'm roughly equivalent to Robert Redford, too. But just to give 'em a chance, let's try it while your lclint is downloading:
$ gcc -Wall hello.c hello.c:1: syntax error before string constant $

Much more helpful, wasn't it? OK, let's give lclint a crack at it:
lclint hello.c LCLint 2.5q --- 26 July 2000 hello.c:1:19: Parse Error: Inconsistent function declaration: echo : int. (For help on parse errors, see lclint -help parseerrors.) *** Cannot continue.

OK, so that's not all that helpful. Actually, "gcc -Wall" is pretty good at being helpful - it is a lot more like lint than I am like Robert Redford. Trust me, though, you'll be glad to have lint later, because sometimes it can give you a different view of the bonehead mistakes you will make. It's always nice to have a second opinion, isn't it?. And it did give us a little more information here that gcc did not. Actually, we have some real problems. First, , we can't just start off writing code like this. This isn't bash or Perl, this is C. There are RULES, and there are a lot more than "end every line with a semicolon". Let's change it a bit:
foo() { echo "Hello World"; }

Now THAT looks a lot closer to a real C program. Or a bash function. It's still a useless pile of bytes to the compiler, though:
$ gcc hello.c hello.c: In function `foo': hello.c:3: `echo' undeclared (first use in this function) hello.c:3: (Each undeclared identifier is reported only once hello.c:3: for each function it appears in.) hello.c:3: syntax error before string constant $ lclint hello.c LCLint 2.5q --- 26 July 2000 hello.c: (in function foo) hello.c:3:1: Unrecognized identifier: echo Identifier used in code has not been declared. (-unrecog will suppress message) hello.c:3:19: Parse Error. (For help on parse errors, see lclint -help parseerrors.) *** Cannot continue.

Do you need to be hit over the head? Obviously the problem is that "echo". There's no "echo" in C! What idiot told you to use "echo"? Oh. Yeah, that was me. OK, right, C has "printf". Shells use echo, Perl uses "print" and "printf", but C uses "printf". Got that? Let's try again, shall we?
$ cat hello.c foo() { printf "Hello World"; } $ gcc hello.c hello.c: In function `foo': hello.c:3: `printf' undeclared (first use in this function) hello.c:3: (Each undeclared identifier is reported only once hello.c:3: for each function it appears in.) hello.c:3: syntax error before string constant $

Aww, c'mon! I KNOW printf is right! Wait a minute, I can even show you in the man page: "man 3 printf". Look, right near the top it shows how to use it:
int printf(const char *format, ...);

.. whatever that means. Actually, it means this: printf returns an integer, and requires at least one string argument ("char *" means string to you, at least for now). We gave it a string argument, but oops, we did leave out those parentheses, didn't we? Let's fix that and try again:
$ cat hello.c foo () { printf("Hello World"); } [tonylaw@kerio tonylaw]$ gcc hello.c /usr/lib/gcc-lib/i386-redhat-linux/3.2.3/../../../crt1.o(.text+0x18): In function `_start': : undefined reference to `main' collect2: ld returned 1 exit status

Oh, yeah, that helped a WHOLE bunch. What does "lclint" think?
$ lclint hello.c LCLint 2.5q --- 26 July 2000 hello.c: (in function foo) hello.c:4:2: Path with no return in function declared to return int There is a path through a function declared to return a value on which there is no return statement. This means the execution may fall through without returning a meaningful result to the caller. (-noret will suppress message) Finished LCLint checking --- 1 code error found

Hmm.. what about that "undefined reference to `main'"? In all the C examples I've ever seen, they started with "main", not "foo".

True enough. By the way, did you notice that we stopped using "make hello" and did "gcc hello.c" instead or did I slip that by you? Either one is fine, at least for what we are trying now. It's not like anything is working, anyway. But let's try the "main" thingy.
$ cat hello.c main () { printf("Hello World"); } $ make hello cc hello.c -o hello

Holy Compilers, Robin, the darn thing worked! Try "./hello":


$ ./hello Hello World$

Looks like it needs a line feed, but that's easy:


main () { printf("Hello World\n"); }

That fixes that. Actually, though, it shouldn't have. We missed something in the man page, and it could have been really important. Let's add something to the file that will break it. Never mind why we are doing this, but add "FILE *a;" as shown here::
$ cat hello.c main () { FILE *a; printf("%s\n","Hello World"); } $ make hello cc hello.c -o hello hello.c: In function `main': hello.c:3: `FILE' undeclared (first use in this function) hello.c:3: (Each undeclared identifier is reported only once hello.c:3: for each function it appears in.) hello.c:3: `a' undeclared (first use in this function) make: *** [hello] Error 1

The fix is quick and simple: one line at the top of the file. The man page for printf actually told us about this: "#include <stdio.h>":
#include <stdio.h> main () { FILE *a; printf("%s\n","Hello World"); }

"make hello" works fine with that. Why? Because the file /usr/include/stdio.h defines FILE (among a lot of other things). Here's a good rule to follow: you will almost ALWAYS need stdio.h.

Whenever the man page for a function you want mentions including some file, it's a pretty good bet that your program is not going to work without it. And there may even be more to do. Look at this:
#include <stdio.h> #include <math.h> main () { printf("pi = %.5f\n", 4 * atan(1.0)); }

We looked up the atan function and found it needs math.h. We've told the compliler to include it. Trust me, the program is right. But..
$ gcc -o pi pi.c /tmp/ccLmoMxc.o(.text+0x33): In function `main': : undefined reference to `atan' collect2: ld returned 1 exit status

What we need here is a special compiler flag:


gcc -o pi -lm pi.c

"lm" tells it to link in the math libraries. Math libraries? Sure, you didn't think C by itself knows how to do arc tangents, did you? C by by itself doesn't know much. Most of its functions - the things you can use - come from external libraries that get linked in. How would you know when you need to use a special library? It's just something you have to learn. Usually, if there is a special linker flag, the man page will have told you. In the case of atan, it does not, so you just need to remember that you need "-lm" when you see "#include <math.h>" mentioned.

Make files
With just this much knowledge, you can actually write C programs. You don't need make files (though make files are of course very useful for bigger programs). If the program needs no special compiler flags, "make" is smart enough to know what to do all by itself, as we saw earlier. As you write bigger programs, you'll find that "gcc -Wall" and "lclint" will help you spot your mistakes. But let's "make" something a bit more complicated. It's two files, and we'll be using a new compiler switch:
$ cat pi.c #include <math.h> mypi() { printf("pi = %.5f\n", 4 * atan(1.0)); } $ cat hello.c #include <stdio.h> main () { mypi(); } $ $ $ $ gcc -c pi.c gcc -c hello.c gcc -lm -o hello hello.o pi.o ./hello

pi = 3.14159

That's all good, and it works, but all those gcc's would get annoying real fast. This is where we use "make".
$ cat Makefile pi.o: pi.c gcc -c pi.c hello.o: hello: hello.c gcc -c hello.c hello.o pi.o gcc -lm -o hello hello.o pi.o

You need TABS after each ":" and before the gcc's on the lines that follow each "rule". Basically, a makefile says "to make x, you need these files, and you run this command

target : TAB files needed TAB command needed

Leave out tabs and make will complain:


Makefile:5: *** missing separator. Stop

But we got the tabs right, so now we can do "make hello" and it works:
$ make hello gcc -c hello.c gcc -c pi.c gcc -lm -o hello hello.o pi.o $ ./hello pi = 3.14159 $

You are going to make more mistakes. If you are like most of us, pointers and indirect pointers (char *this, char **that) are going to confuse you, and you'll misuse them. Lint and gcc -Wall will help you somewhat, but you'll still probably screw up. You'll forget that you are referencing something on a long overwritten stack. You'll blithely dereference past the end of an array and your programs will crash as they get killed off for trying to change somebody else's memory space. Don't feel badly, other people have stumbled and lurched down the same path before you. Buy a beginner's C book, and have at it. Here's some references that can help: Introduction to C Programming - Programming Errors Common C Programming Errors The Top 10 Ways to get screwed by the "C" programming language Common practices to help avoid errors in C programs (a bit more advanced; save this one for later)

Commented [3]: TEXT ENDS Commented [4]: google_ad_section_end

Potrebbero piacerti anche