Board

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Anatomy of a C program

/* example_one.c
* (It has a while-loop with just two comments in it.)
* You should identify your program and say what it does in
* a comment like this at the beginning of your program.
* (This demonstrates the syntax for a multi-line comment.)
*
*/

#include <stdio.h> preprocessor directives


#include “machinescience.h”

int main(){
while( 1 == 1 ){
// This is a single line comment.
// This program consists of an infinite loop doing nothing.
}
return 0;
}

At the very beginning of your program, it’s good programming style to write comments -like the
name of the program, what it does, etc. It’s not required in order for the program to run, but it’s a
good idea. You’ll thank yourself later.

Preprocessor directives come first in the program (after any comments) and tell the compiler
to include additional code from other libraries with your program. The .h files (sometimes called
“header” files) that are enclosed in “< >” are part of the built-in libraries that installed with the
compiler. Custom libraries can also be written and referenced with an include statement with
double-quotes.

C programs always have a “main” function which is where program execution starts.

C functions have a structure and syntax as follows:

The function name is followed by parentheses which


return value (if there is one) enclose any values required by the function when
function name it executes. each value has to have a type & name.

float custom_function(int value1, int value2) {


printf(“statements go here.”);
}

The body of a function is composed of C statements that


are enclosed in curly braces.

Note: In this example, the definition of “custom_function()” states that it requires two integers to
execute and that it will output a floating point value. If it was actually used in a program it would
look something like this: custom_function(7, 34);
Then it should do some calculation with the numbers 7 and 34 and output a floating-point
number. Instead it only has a statement calling the printf() function.

You might also like