Introduction to C Programming
Basic Structure and Fundamentals

The basic form of a simple C program is as shown below. ( Line numbers have been added for future reference. )

	1	/* Filename.c
	2
3 This program was written by Ima Programmer on some date 4 5 This program does something useful . . . 6 7 */ 8 9 #include <stdlib.h> // For accessing the standard library functions 10 #include <stdio.h> // For standard input and output functions 11 12 13 int main( void ) { 14 15 // Declare variables to be used 16 17 // Explain to the user what the program does 18 19 // Get user input ( and check it if possible ) 20 21 // Perform necessary calculations 22 23 // Report results ( in a complete and well-formed format. ) 24 25 system( "pause" ); // Only needed with certain environments, e.g. Dev C++ 26 27 return 0; 28 29 } // main

Explanation of Specific Lines

  1. The symbols /* and */ enclose a block comment, which may extend over any number of lines, or which may enclose only part of a line.
  2. The #include statement informs the compiler of what libraries are needed.
  3. Line 13 says that we are starting a function named main, which is the starting point for all C programs.
  4. Some IDEs ( Integrated Development Environments ) execute programs in a separate window, and then immediately close the window when the program is completed. In this case, the system( pause ) statement holds the window open so that the results can be seen, until the user presses a key to release it. ( Dev C++ is one of those IDEs. ) Note that CodeLab does not permit the use of system( pause ), so make sure to leave it out when submitting CodeLab solutions.
  5. Every function must have a return statement, which causes the function to end and return control to whoever called it.
  6. It is good practice to comment the closing brace of every function, and any other closing brace that is more than a few lines away from its matching opening brace. This makes it much easier to read more complex programs, which may have MANY sets of nested and consecutive braced blocks.

A Simple Sample:

	1	/* addThree.c
	2
	3	   This program was written by John Bell in January 2013 for CS 107
	4
	5	   This program asks the user for two floating point numbers and 
	6	   an integer, and reports their total.  Note that one of the floating
	7	   point numbers is stored as a double precision data type.
	8
	9	*/
	10
	11	#include <stdlib.h>		// For accessing the standard library
	12	#include <stdio.h>		// For standard input and output
	13
	14	int main( void ) {
	15
	16		// Declare variables to be used
	17
	18		int number;                             // The integer
	19		float fpNumber = 0.0f;                  // The floating point number
	20		double dpNumber = 0.0, total = 0.0;     // The double and the total
	21
	22		// Explain to the user what the program does
	23
	24		printf( "This program adds together two floating point numbers\n" );
	25		printf( "and an integer.\n" );
	26		printf( "Written January 2009 by John Bell for CS 107.\n\n" );
	27
	28		// Get user input ( and check it if possible )
	29
	30		printf( "Please enter the first floating point number > " );
	31		scanf( "%f", &fpNumber );
	32
	33		printf( "\nPlease enter the second floating point number > " );
	34		scanf( "%lf", &dpNumber );
	35
	36		printf( "\nPlease enter the integer > " );
	37		scanf( "%d", &number );
	38
	39		// Perform necessary calculations
	40	
	41		total = fpNumber + dpNumber + number;
	42
	43		// Report results ( in a complete and well-formed format. )
	44
	45		printf( "\nThe total of %f plus %f plus %d is %f\n", fpNumber,  
	46				dpNumber, number, total );
	47	
	48		system( "pause" );	// Only with certain environments, e.g. Dev C++
	49
	50		return 0;
	51
	52	} // main

Explanation of Specific Lines

  1. Line 18 declares that this program uses a variable named "number" that is of type "int".
  2. Line 19 declares a variable named "fpNumber" of type float, the most basic of floating point data types.
  3. Line 20 declares two variables of type "double", a floating-point data type with twice the numerical precision of the data type "float".
  4. "prntf" is the standard library function for formatted printing
  5. Note that because this line has no "\n" at the end, the curser will be left at the end of the line with the question.
  6. scanf is the standard library function for reading data in from the keyboard.
  7. The format specifier for a double precison number is %lf", as opposed to %f". ( A double is essentially a "long" float. )
  8. The format specifier for an integer is "%d". ( "d" stands for "decimal" integer, as opposed to octal or hexadecimal. )
  9. The equals symbol, =, as used here is an assignment, which takes the value from the right side and stores it in the left side.
  10. Format specifiers for printing numbers are similar to those used when reading them in.

Variables ( Also covered under Data Types )

Keywords

The following words are reserved, and may not be used as identifiers:

auto
break
case
char
const
continue
default
do
double
else
enum
extern
float
for
goto
if
inline
int
long
register
restrict
return
short
signed
sizeof
static
struct
switch
typedef
union
unsigned
void
volatile
while
_Bool
_Complex
_Imaginary



 

Naming Conventions

In addition to the variable naming rules imposed by the compiler, there are certain conventions that are commonly followed:

int number, nStudents
double Coordinate, Salary;
const double PI = 3.14159;
const int MAXROWS = 100;
double totalOld, totalNew, sumOfAllDigits;
double totalOfX_coordinates, total_of_Y_coordinates
int nStucents; // Not just students

double average, total, standardDeviation;
 

Another Example ( Enhanced version needs to be converted from C++ to C )

Exercises