Anatomy of a C program

The basic outline of a C program consists of:

  • Comments
  • Preprocessor commands
  • Type definitions
  • Function prototypes
  • Variables
  • Functions
  • All programs must have a main() function
  • All variables and functions must be declared before they are used in main()

 

Comments
Use // for a single line
Use /* for

Multiple lines
*/

 

Preprocessor
#include <stdio.h>
Brings in all the code from the specified file <stdio.h>, so that all of its functions are available to the program

 

The main() function is reserved in C. All C programs must have a main() function. Its where execution begins. Requires a return type, in this case int, which can be seen in the last line of the code block returning a value of 0.

 

The { curly braces } indicate the start and end of the code block for the function.
Statements within the code block are terminated with a semi-colon ;

 

The printf() function is made available via #include <stdio.h> to send the arguments within its parentheses to the standard output (usually the screen). Other data can be included as arguments, such as variables, utilising format specifiers within the output string, as required for the type of variable, sent to the screen.

 

Whitespace, to make code more readable

 

return 0 ; causes the function to return an error code of 0 (no error) to the shell that started execution.

Leave a Reply