A Simple C Program


This section provides a simple example of how to create an executable program for Linux by using gcc. One of the shortest programs you can write in C for Linux is the quintessential "hello, world" program (popularized by Brian W. Kernighan and Dennis M. Ritchie in their 1978 book The C Programming Language).

To get started, type in the following text, using your favorite Linux text editor:

main(){     printf("hello, world.\n"); }


When you are finished, save the file as hello.c, and then compile the program by using the gcc compiler system, like this:

$ gcc -o hello hello.c


This command line creates an executable program named hello, using the file hello.c as input. You can then run the program like this:

$ ./hello hello, world.


The C program contains only one function, named main(). The gcc command line specifies the name of the output program (using the -o option) and the input source file, hello.c. Note that to run the new hello command, you must specify it explicitly, using the period and forward-slash characters, because the program is not installed in a normal command directory (such as /usr/bin or /usr/local/bin).



Red Hat Fedora 5 Unleashed
Red Hat Fedora 5 Unleashed
ISBN: 067232847X
EAN: 2147483647
Year: 2004
Pages: 362

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