Programming Languages
A Dave Hillman Project

[Home] [Contact Me] [Site Map] [Search] [References]

C Language

 

[Up]

[Language Basics]
[C Language]
[C++]
[C#]
[Java]
[JavaScript]
[Visual Basic 6.0]
[VBScript]
[VB.Net]
[.Net Technology]

 

 

Introduction

bulletDeveloped in the early 70's by Dennis Ritchie at Bell Labs
bulletStandard established in 1978 by Ritchie and Dennis Kernighan
bulletC is considered a highly portable language because of it's simplicity.
bulletC is a relatively small language with few hardware-specific elements.
bulletInput/output and memory management are handled via C library-based functions.
bulletMachine code generated in C is considered highly efficient.
bulletSome consider the language to be a high-level assembly (but it is not assembly) because of it's terseness.
bulletC compilers have been developed for Unix, Linux, DOS, Windows, mainframes, and many other platforms.

Language Fundamentals

Program Structure and Organization

bulletC programs consist of:
bulletBuilding blocks called functions which can call each other.
bulletInclude both programmer and system-based functions.
bulletA "main()" function must be included and is the starting point for the program.
bulletFunction "prototypes" are used to declare the existence of a function prior to it being formally defined.
bulletFunctions are declared as data types and may return values or null.
bulletProgram structure:
bulletComments: multi-line /* .... */, single line: // to end of line
bulletSemi-colons are used to terminate a line or statement.

 

/*
Simple hello world program.
by Dave Hillman
*/
Comments and program file header
#include <stdio.h>
 
Pre-Processor Directives
main(void)
{
  printf("Hello World \n"); /* output something to the screen */
  printf("\n\tPress any key to end program."); /* let the user know how to end the program */
  getchar(); /* look for input from the keyboard */
  return 0; /* return control to the OS */
}
 
Main function - starting point for the program

May be placed anywhere in a source file.

void otherfun(void)
{
....

}
 

Other functions

 

bulletLanguage/character sets: C has two character sets:
  1. Source character - what the program is written in.
  2. Execution character - character set for delivery.
bulletEach contains a basic 26 upper and 26 lower alphabet characters, ten digits, 29 graphic characters, and 5 white space characters (space, tab, etc.).
bulletAlso null character, \0 and escape control characters: e.g.,  \n  - new line
bulletIdentifiers and keywords:
bulletFunction, variable, etc. contain letters, digits, and underscore.
bulletFirst character cannot be a digit.
bulletAre case sensitive.
bulletNo restrictions on length, only first 31 characters are usually significant.
bulletSource code management (files, etc.)
bulletSource files have .c extension
bulletHeader files have .h extension,
bulletProgram delivery:
bulletC programs are compiled.
bulletVarious intermediate files are created:
bulletSource
bulletStripped source (comments, etc.0
bulletObject
bulletCompiled executable.
bulletPrograms are limited to operating systems they are compiled for.
bulletCommercial compilers include:
bulletMiracle C: http://www.c-compiler.com/
bulletICC-Win32: http://www.cs.virginia.edu/~lcc-win32/
bullet Google Listing

Data Types

bulletBasic data types:
bulletchar - character
bulletint - an integer, in the range -32,767 to 32,767
bulletlong int - larger integer (up to +-2,147,483,647)
bulletfloat - floating-point number
bulletdouble - floating-point number, with more precision and greater range than float
bulletDoes not have an explicit Boolean
bulletCharacters and arrays of characters (strings) are represented with single or double quoting.
bulletDeclaring variables:
bulletint i;  -  initializes a variable of type integer called i
bulletint i = 3;  - initializes and i and sets it equal to 3
bulletint i1, i2;  -  initializes two variables: i1, and i2
bulletVariables are case sensitive.
bulletArrays:
bulletAll data types support array declarations.
bulletExample: char line[81];  - declares a character array with 81 positions
bulletFilling an array:  line[2] = 'A'
bulletMultidimensional arrays are possible:  e.g., short array_name[50][20][10] creates an array with 10,000 elements.
bulletVariable scope:
bulletVariables declared in functions live within the function.
bulletstatic int i;  - creates a variable scoped within the file.
bulletextern int i; - creates a variable scoped within an application.

Expressions and Operators

bulletMath operators (in order of precedence)
bullet* multiplication
bullet/ division
bullet% modulus (remainder)
bullet+ addition
bullet- subtraction
bullet+x value of x
bullet-x negative value
bullet++ increment
bullet-- decrement
bulletAssignment operator: =
bulletRelational operators
bullet<, <=, >, >=
bullet== is equal to
bullet!= is not equal to
bulletLogical operators
bullet&& - logical and
bullet|| - logical or
bullet! - logical not
bulletOther operators:
bullet() - function call, e.g., pow(x,y)
bullet(type) - cast, e.g., (long)x - the value of x is set with type long
bulletsizeof - size in bytes
bullet?:  -  conditional evaluation
bullet, - sequence operator

Control Statements

bulletIf-then
bulletEvaluate statements if expression is true for if condition or subsequent else conditions.
bulletIncludes if, if-then logic; complex Boolean evaluation.
bulletShort-circuit if first logical expression is found false.
bulletExamples:

if (expression) statement;

if (expression)  {
    statement;
    statement;
    ....  }

if (expression)  {
    statement;
    statement;  }
else
     statement;

if (expression)  {
    statement;
    statement;  }
else  {
    statement;
    statement;  }

if (expression)  {
    statement;
    statement;  }
else if (expression)
     statement;

bulletSwitch
bulletEvaluate expression based on truth of condition.
bulletExpression is a value to be evaluated in subsequent cases.
bulletExamples:

switch (expression) statement

switch ( command )
    case 'A': statement
    case 'B': statement

 

bulletDo/While loop
bulletEvaluate until expression evaluates to true.
bulletDo/while evaluates following the loop.
bulletWhile loops evaluate prior to the loop start.
bulletExamples:

do {
    statement;
    statement;
    ...  }
while (expression)

while (expression) {
    statement;
    statement;
    ...  }

 

bulletFor loop
bulletLoop based upon initial, goal, and modification.
bulletExample and syntax:

for (expression1; expression2; expression3)
        statement...

expression1 - initial value
expression2 - goal value
expression3 - modify value or function

 

bulletGo To
bulletSyntax: goto label_name;
bulletA label is a name followed by a colon.
bulletLabel must be contained in same function.
bulletContinue
bulletOnly used in a loop; jumps over remainder of a loop to the test condition.
bulletBreak
bulletUsed in a loop to exit the loop.
bulletReturn
bulletSyntax: return (expression)
bulletEnds a function, returning value to the calling function.

C Libraries

C depends on a set of libraries (ANSI C Library) to handle a variety of common and advanced functions, macros, and data types.  The libraries are based upon standard sets of tasks including:

bulletStandard Input/Output (stdio.h)
bulletMath functions (math.h)
bulletString handling functions (string.h)
bulletTime and date functions (time.h)
bulletMiscellaneous library functions (stdlib.h)

These libraries are included in various C source files to provide the desired support functions for a program.

Input/Output

Given the above and other libraries, C input/output is handled by a variety of functions including some of the following (there are more than this):

bulletStandard Input and Output - C "assumes" that standard devices are used for input (keyboard) and output (command line display)
bulletKeyboard input
bulletscanf() - reads a standard input (from a keyboard)
bulletgets() - reads a string from standard input
bulletScreen output
bulletprintf() - handles output to the screen
bulletFile I/O
bulletfopen() - opens a file for access, returns a handle for processing
bulletfclose() - closes an open file
bulletfgetc() - reads a character from a file
bulletfgets() - reads a string from a file

Resources

References

bulletGoogle Directory: http://directory.google.com/Top/Computers/Programming/Languages/C/
bulletC reference guide: http://www.acm.uiuc.edu/webmonkeys/book/c_guide/
bulletLink to a variety of references (in other words, more links): http://www.lysator.liu.se/c/

Tutorials

bullet http://www.eskimo.com/~scs/cclass/notes/top.html
bullet http://www.strath.ac.uk/IT/Docs/Ccourse/
bullet http://www.aasted.org/GC/c-tut.php
bulletFrom How Stuff Works, a nice tutorial on C: http://computer.howstuffworks.com/c1.htm

And just to prove I have a sense of humor, the following songs about C are done to Beatles tunes: http://www.indigo.org/humor/beatles.html (enjoy)!

 

[Home] [Language Basics] [C Language] [C++] [C#] [Java] [JavaScript] [Visual Basic 6.0] [VBScript] [VB.Net] [.Net Technology]

Copyright © 2003-2005 by Dave Hillman