Your First C Program


Here is your first C program, which prints "Hello world" to the screen and exits. Type it in, and give it the name Hello-World.c

 #include <stdio.h> /* PURPOSE:  This program is mean to show a basic */ /*           C program.  All it does is print     */ /*           "Hello World!" to the screen and     */ /*           exit.                                */ /* Main Program */ int main(int argc, char **argv) {  /* Print our string to standard output */  puts("Hello World!\n");  /* Exit with status 0 */  return 0; } 

As you can see, it's a pretty simple program. To compile it, run the command

 gcc -o HelloWorld Hello-World.c 

To run the program, do

 ./HelloWorld 

Let's look at how this program was put together.

Comments in C are started with /* and ended with */. Comments can span multiple lines, but many people prefer to start and end comments on the same line so they don't get confused.

#include <stdio.h> is the first part of the program. This is a preprocessor directive. C compiling is split into two stages - the preprocessor and the main compiler. This directive tells the preprocessor to look for the file stdio.h and paste it into your program. The preprocessor is responsible for putting together the text of the program. This includes sticking different files together, running macros on your program text, etc. After the text is put together, the preprocessor is done and the main compiler goes to work.

Now, everything in stdio.h is now in your program just as if you typed it there yourself. The angle brackets around the filename tell the compiler to look in its standard paths for the file (/usr/include and /usr/local/include, usually). If it was in quotes, like #include "stdio.h" it would look in the current directory for the file. Anyway, stdio.h contains the declarations for the standard input and output functions and variables. These declarations tell the compiler what functions are available for input and output. The next few lines are simply comments about the program.

Then there is the line int main (int argc, char **argv). This is the start of a function. C Functions are declared with their name, arguments and return type. This declaration says that the function's name is main, it returns an int (integer - 4 bytes long on the x86 platform), and has two arguments - an int called argc and a char ** called argv. You don't have to worry about where the arguments are positioned on the stack - the C compiler takes care of that for you. You also don't have to worry about loading values into and out of registers because the compiler takes care of that, too.

The main function is a special function in the C language - it is the start of all C programs (much like _start in our assembly-language programs). It always takes two parameters. The first parameter is the number of arguments given to this command, and the second parameter is a list of the arguments that were given.

The next line is a function call. In assembly language, you had to push the arguments of a function onto the stack, and then call the function. C takes care of this complexity for you. You simply have to call the function with the parameters in parenthesis. In this case, we call the function puts, with a single parameter. This parameter is the character string we want to print. We just have to type in the string in quotations, and the compiler takes care of defining storage and moving the pointers to that storage onto the stack before calling the function. As you can see, it's a lot less work.

Finally our function returns the number 0. In assembly language, we stored our return value in %eax, but in C we just use the return command and it takes care of that for us. The return value of the main function is what is used as the exit code for the program.

As you can see, using high-level languages makes life much easier. It also allows our programs to run on multiple platforms more easily. In assembly language, your program is tied to both the operating system and the hardware platform, while in compiled and interpreted languages the same code can usually run on multiple operating systems and hardware platforms. For example, this program can be built and executed on x86 hardware running Linux , Windows , UNIX , or most other operating systems. In addition, it can also run on Macintosh hardware running a number of operating systems.

Additional information on the C programming language can be found in Appendix E.




Programming from the Ground Up
Programming from the Ground Up
ISBN: 0975283847
EAN: 2147483647
Year: 2006
Pages: 137

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net