Example of a Simple C Program

The source for a simple C program is shown below:

A Simple C Program
/**
 ** This is an example of a simple C program
 **/
#include <stdio.h>    /* standard I/O library header that
                         contains macros and function
                         declarations, ie printf used below  */
 
#include <math.h>     /* standard math library header that
                         contains macros and function
                         declarations, ie cos used below    */
 
#define NUM 46.0      /* Preprocessor directive             */
 
double x = 45.0;      /* External variable definitions      */
double y = NUM;
 
int main(void)        /* Function definition
                         for main function                  */
{
   double z;          /* Local variable definitions         */
   double w;
 
   z = cos(x);        /* cos is declared in math.h as
                               double cos(double arg)       */
   w = cos(y);
   printf ("cosine of x is %f\n", z); /* Print cosine of x  */
   printf ("cosine of y is %f\n", w); /* Print cosine of y  */
 
   return 0;
}

The program above defines main and declares a reference to the function cos. The program defines the global variables x and y, initializes them, and declares two local variables z and w.



Internal Structure of a C or C++ Program
Scope of Identifier Visibility
Statement Blocks


Example of a C Program Comprised of Two Source Files