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:

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:

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).


Was this article helpful?

0 0

Post a comment