A Simple C Program

Last modified by Microchip on 2023/11/10 11:00

1 #include <stdio.h>
2
3 #define PI 3.14159
4  
5 int main(void)
6 {
7    float radius, area;
8
9    //Calculate area of circle
10    radius = 12.0;
11    area = PI * radius * radius;
12    printf("Area = %f", area);
13 }

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.