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, as the program is not installed in a normal command directory (such as /usr/bin or /usr/local/bin).



Ubuntu Unleashed
Ubuntu Unleashed 2011 Edition: Covering 10.10 and 11.04 (6th Edition)
ISBN: 0672333449
EAN: 2147483647
Year: 2006
Pages: 318

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