Tuesday, 1 October 2013

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
	3	   This program was written by Ima Programmer on some date
	5	   This program does something useful . . .
	7	*/
	9	#include <stdlib.h>       // For accessing the standard library functions
	10	#include <stdio.h>        // For standard input and output functions
	13	int main( void ) {
	15		// Declare variables to be used
	17		// Explain to the user what the program does
	19		// Get user input ( and check it if possible )
	21		// Perform necessary calculations
	23		// Report results ( in a complete and well-formed format. )
	25		system( "pause" );	// Only needed with certain environments, e.g. Dev C++
	27		return 0;
	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.
    • EVERY well-written program should begin with a large block comment specifying the name of the file, the author of the program, when it was written, and a brief description of what the program does and why. In large projects the opening comment may include reference to external documents ( software requirements documentation ), and may include a revision history of who modified the program when and for what purposes.
  2. The #include statement informs the compiler of what libraries are needed.
    • stdlib.h is a header file containing necessary definitions for the use of the standard C library functions. Many systems automatically include this one, but there are exceptions, and it never hurts to include it again, just to be sure.
    • stdio.h contains the definitions of standard input and output functions. It is very rare that a C program would not need this header file.
    • More advanced programs may need to include other libraries, such as cmath for using trigonometric functions.
    • The double slash symbol, //, indicates a comment that extends to the end of the current line.
  3. Line 13 says that we are starting a function named main, which is the starting point for all C programs.
    • The keyword int says that main returns an integer value as its return type. ( See line 27. )
    • The open brace indicates the beginning of a block of code, which must be matched by a closing brace ( on line 29. )
    • The keyword void expressly states that main takes no arguments. In other programs you may see the following alternatives:
      • int main ( ) { - Does not expressly say what arguments main takes.
      • int main( int argc, char *argv[ ] ) { - Beyond the scope of this introduction.
      • int main( int argc, char *argv[ ], char ** envp ) { - Beyond the scope of this introduction.
  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.
    • Because main was declared to have an integer return type in line 13, this function must return an integer.
    • The return value from main is typically interpreted as an error code, with a value of zero indicating successful completion.
    • Negative 1 is a commonly used return value used to indicate an unspecified error of some kind.
  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:

  • This simple program adds three numbers and reports the total.
	1	/* addThree.c
	3	   This program was written by John Bell in January 2013 for CS 107
	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.
	9	*/
	11	#include <stdlib.h>		// For accessing the standard library
	12	#include <stdio.h>		// For standard input and output
	14	int main( void ) {
	16		// Declare variables to be used
	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
	22		// Explain to the user what the program does
	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" );
	28		// Get user input ( and check it if possible )
	30		printf( "Please enter the first floating point number > " );
	31		scanf( "%f", &fpNumber );
	33		printf( "\nPlease enter the second floating point number > " );
	34		scanf( "%lf", &dpNumber );
	36		printf( "\nPlease enter the integer > " );
	37		scanf( "%d", &number );
	39		// Perform necessary calculations
	41		total = fpNumber + dpNumber + number;
	43		// Report results ( in a complete and well-formed format. )
	45		printf( "\nThe total of %f plus %f plus %d is %f\n", fpNumber,  
	46				dpNumber, number, total );
	48		system( "pause" );	// Only with certain environments, e.g. Dev C++
	50		return 0;
	52	} // main

Explanation of Specific Lines

  1. Line 18 declares that this program uses a variable named "number" that is of type "int".
    • All variables in C must be declared befrore they can be used.
      • Traditional C compilers requried that all variable declarations be made before any executable statements.
      • Most C programmers continue to follow this convention, even though the restriction has been relaxed in modern compilers, and there are sometimes good reasons for declaring variables later on in the program.
    • In this example, "number" is not assigned any initial value, so it will start out with an unknown random value.
    • Note the semicolon at the end of the line, which is how C knows that a statement has ended.
  2. Line 19 declares a variable named "fpNumber" of type float, the most basic of floating point data types.
    • This variable has been given an inital value of 0.0.
    • The letter "f" following the 0.0 indicates that this number is to be interepreted to be of type "float", as opposed to the default of "double".
  3. Line 20 declares two variables of type "double", a floating-point data type with twice the numerical precision of the data type "float".
    • Scientific and engineering programs typically use doubles instead of floats, unless there is a specific reason to do otherwise.
    • These variables are also both initialized to zero.
  4. "prntf" is the standard library function for formatted printing
    • A simple text string can be enclosed in double quotes, as shown.
    • The \n indicates a new line character. Without this, all the printout would appear on a single line. Multiple \n characters yield blank lines.
    • Every well-written program should begin by explaining to the user what the program does.
  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.
    • The % symbol is a format specifier, and indicates what type of value to read in. %f specifies to read in a float data type.
    • Note that the variable name must have an ampersand in front of it in a scanf statement. The exact reason for this will be explained in the course section on pointers.
  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.
    • The plus sign, +, peforms addition, and yields the sum of its two arguments.
  10. Format specifiers for printing numbers are similar to those used when reading them in.
    • The first argument to printf when printing numbers is a quoted string, with % format specifiers inserted everywhere that a numerical value is desired.
    • The quoted string is followed by a comma-separated list of values ( variables and/or expressions ) to be printed, one per format specifier.
    • Note that both the float and the double type use "%f" as their format specifier when printing, as opposed to the "%f" for floats and "%lf" for doubles when scanning.
    • Note that the output of the results is a complete sentence, and includes an echoing back of the user's input.
    • Note also that a line of code may span over multiple lines in the file. C/C++ only recognizes the semi-colon as the end of a line.


  • A variable is a named storage location, where data may be stored and later changed.
  • An identifier is a more general term for a named location, which may contain either data or code.
    • Identifiers must begin with a letter or an underscore, preferable letters for user programs.
    • The remaining characters must be either alphanumeric or underscores.
    • Identifiers may be of any length, but only the first 31 characters are examined in most implementations.
    • Identifiers are case sensitive, so "NUMBER", "number", and "Number" are three different identifiers.
    • By convention ordinary variables begin with a lower case letter, globals with a Single Capital, and constants in ALL CAPS.
      • Multi-word variables may use either underscores or "camel case", such as "new_value" or "newValue".
    • Integers are usually assigned variable names beginning with the letters I, J, K, L, M, or N, and floating point variables are usually assigned names beginning with other letters.
    • Identifiers may not be the same as reserved words. ( See below for a full list. )
  • All variables must be declared before they can be used.
    • In K&R C, all variables must be declared before the first executable statement of the program.
    • Modern C allows variables to be declared any time before they are used, but it is still normally good practice to declare all variables at the beginning of the program, unless there is a very good reason to do otherwise.
      • ( Exceptions: Loop counter variables are often declared as part of the loop structure. Occasionally it is beneficial to declare variables within a reduced scope, to be discussed later. )
  • Variables may be given an initial value at the time they are declared. This is called "initialization", or "initializing the variables".
    • Initialization in C is done using an equals sign:
    • Example: double x1 = 0.0, x2 = 0.0;
  • Variables may be declared "const", meaning that their values cannot be changed.
    • const variables MUST be initialized at the time they are declared.
    • By convention, const variables are named using ALL CAPS.
    • Examples:
      • const double PI = 3.14159;
      • const int MAXROWS = 100;
    • Note: K&R C did not have the const keyword, and so the #define pre-processor macro was used to define constant values. The const qualifier is a better approach when it is available, because it allows the compiler to perform type checking among other reasons. For CS 107 we will defer the discussion of #define until we get to the chapter on the pre-processor.


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

Naming Conventions

In addition to the variable naming rules imposed by the compiler, there are certain conventions that are commonly followed:
  • Ordinary variables normally begin with lower case letters:
int number, nStudents
  • Global variables ( to be covered later ) normally begin with a single capital letter:
double Coordinate, Salary;
  • Defined constants normally are in all upper case:
const double PI = 3.14159;
const int MAXROWS = 100;
  • Variable names consisting of more than one word typically capitalize successive words. This is known as "camel case":
double totalOld, totalNew, sumOfAllDigits;
  • Alternatively, where camel case is awkward or undesired, underscores may be used to indicate the start of words:
double totalOfX_coordinates, total_of_Y_coordinates
  • Integer numbers typically begin with letters I, J, K, L, M, or N. ( Remeber the first two letters in INteger. )
  • Floating point numbers typically begin with other letters:
int nStucents; // Not just students

double average, total, standardDeviation;
  • In any case, variable names should be meaningful and easily understood. ( Think complete words, not just letters, and make sure the words are meaningful and not just temp1, temp2, temp3, etc. )

No comments:

Post a Comment