Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




The GNU C Programming Tutorial - The form of a C program

Node:The form of a C program, Next:, Previous:Using a compiler, Up:Top

The form of a C program

What goes into a C program? What does one look like?

The basic building block of a C program is the function. Every C program is a collection of one or more functions. Functions are made of variable declarations and statements, or complex commands, and are surrounded by curly brackets ({ and }).

One and only one of the functions in a program must have the name main. This function is always the starting point of a C program, so the simplest C program is a single function definition:

main ()

The parentheses () that follow the name of the function must be included. This is how C distinguishes functions from ordinary variables.

The function main does not need to be at the top of a program, so a C program does not necessarily start at line 1, but wherever the function called main is located. The function main cannot be called, or started, by any other function in the program. Only the operating system can call main; this is how a C program is started.

The next most simple C program is perhaps a program that starts, calls a function that does nothing, and then ends.

/*                                                    */
/* Program : do nothing                               */
/*                                                    */

main()                          /* Main program */


do_nothing()                 /* Function called */

(Any text sandwiched between /* and */ in C code is a comment for other humans to read. See the section on comments below for more information.)

There are several things to notice about this program.

First, this program consists of two functions, one of which calls the other.

Second, the function do_nothing is called by simply typing the main part of its name followed by () parentheses and a semicolon.

Third, the semicolon is vital; every simple statement in C ends with one. This is a signal to the compiler that the end of a statement has been reached and that anything that follows is part of another statement. This signal helps the compiler diagnose errors.

Fourth, the curly bracket characters { and } outline a block of statements. When this program meets the closing } of the second function's block, it transfers control back to main, where it meets another }, and the program ends.

  Published under free license. Design by Interspire