A Simple C Program
C language doesn't use line numbers. They are for reference purposes only and are not a part of the code. Below is a brief overview of some of the basic parts of a C program. These will all be discussed in detail later in the class.
Line 1
#include is a preprocessor directive that includes the contents of a header file (stdio.h) in this source file.
Line 3
#define is a preprocessor directive used here to create a text substitution label. Anywhere below this line where the text "PI" is encountered, it will be replaced with "3.14159".
Line 5-13
This is the main() function. Every C program must have one, and only one main() function. This is where your application code resides and is the first thing to run after the C Runtime Environment setup code completes.
Line 7
Here, two floating point variables are declared. In C, a variable must be declared before it can be used.
Line 9
This line is a comment.
Line 10
This line is an assignment statement.
Line 11
This line is also an assignment statement whose value is that of the arithmetic expression.
Line 12
This is a call to the printf() function. It prints the value of the variable area in a terminal window on a PC or in the UART1I/O window (if configured) when using the simulator in MPLAB® X IDE. We will make extensive use of this feature in this class.
2
3
4
5
6
7
8
9
10
11
12
13
#define PI 3.14159
int main(void)
{
float radius, area;
//Calculate area of circle
radius = 12.0;
area = PI * radius * radius;
printf("Area = %f", area);
}