Introduction to C Programming
Basic Structure and Fundamentals

This page shows two examples, in increasing order of complexity. It has details about types, naming variables, and printing. etc.

Hello World

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 * This program does something useful . . .
	5 */
	6
	7	#include <stdlib.h>       // For accessing the standard library functions
	8	#include <stdio.h>        // For standard input and output functions
	9
	10
	11	int main( ) { 			// every C program begins at main
	12
	13		printf("Hello world\n"); // this is how we print to the screen
	14		return 0;		// convention says return a code
	15
	16	} // 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. Additional stars at the beginning of the line remind the reader that it is still within a multi-line comment.
  2. The #include statement informs the compiler of what libraries are needed.
  3. Line 11 says that we are starting a function named main, which is the starting point for all C programs.
  4. "printf" is the standard library function for formatted printing
  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	 *  This program was written by John Bell in January 2013 for CS 107
	3	 *  This program asks the user for two floating point numbers and 
	4	 *  an integer, and reports their total.  Note that one of the floating
	5	 * point numbers is stored as a double precision data type.
	6	 */
	7
	8	#include <stdlib.h>		// For accessing the standard library
	9	#include <stdio.h>		// For standard input and output
	10
	11	int main(  ) 
	12	{
	13		// Declare variables to be used
	14		int number;                             // The integer
	15		float fpNumber = 0.0f;                  // The floating point number
	16		double dpNumber = 0.0, total = 0.0;     // The double and the total
	17
	18		// Explain to the user what the program does
	19		printf( "This program adds together two floating point numbers\n" );
	20		printf( "and an integer.\n" );
	21		printf( "Written January 2009 by John Bell for CS 107.\n\n" );
	22
	23		// Get user input ( and check it if possible )
	24		printf( "Please enter the first floating point number > " );
	25		scanf( "%f", &fpNumber );
	26
	27		printf( "\nPlease enter the second floating point number > " );
	28		scanf( "%lf", &dpNumber );
	29
	30		printf( "\nPlease enter the integer > " );
	31		scanf( "%d", &number );
	32
	33		// Perform necessary calculations
	34		total = fpNumber + dpNumber + number;
	35
	36		// Report results ( in a complete and well-formed format. )
	37		printf( "\nThe total of %f plus %f plus %d is %f\n", fpNumber,  
	38				dpNumber, number, total );
	39	
	40		return 0;
	41	} // main

Explanation of Specific Lines

  1. Line 14 declares that this program uses a variable named "number" that is of type "int".
  2. Line 15 declares and initializes a variable named "fpNumber" of type float, the most basic of floating point data types.
  3. Line 16 declares and initializes two variables of type "double", a floating-point data type with twice the numerical precision of the data type "float".
  4. "printf" 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 nStudents; // Not just students

double average, total, standardDeviation;