|
| |
Introduction
 | Developed in the early 70's by Dennis Ritchie at Bell Labs |
 | Standard established in 1978 by Ritchie and Dennis Kernighan |
 | C is considered a highly portable language because of it's simplicity. |
 | C is a relatively small language with few hardware-specific elements. |
 | Input/output and memory management are handled via C library-based
functions. |
 | Machine code generated in C is considered highly efficient. |
 | Some consider the language to be a high-level assembly (but it is not
assembly) because of it's terseness. |
 | C compilers have been developed for Unix, Linux, DOS, Windows, mainframes,
and many other platforms. |
Language Fundamentals
Program Structure and Organization
/*
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 |
 | Language/character sets: C has two character sets:
- Source character - what the program is written in.
- Execution character - character set for delivery.
 | Each contains a basic 26 upper and 26 lower alphabet characters, ten
digits, 29 graphic characters, and 5 white space characters (space, tab,
etc.). |
 | Also null character, \0 and escape control characters: e.g., \n
- new line |
|
 | Identifiers and keywords:
 | Function, variable, etc. contain letters, digits, and underscore. |
 | First character cannot be a digit. |
 | Are case sensitive. |
 | No restrictions on length, only first 31 characters are usually
significant. |
|
 | Source code management (files, etc.)
 | Source files have .c extension |
 | Header files have .h extension, |
|
 | Program delivery:
 | C programs are compiled. |
 | Various intermediate files are created:
 | Source |
 | Stripped source (comments, etc.0 |
 | Object |
 | Compiled executable. |
|
 | Programs are limited to operating systems they are compiled for. |
 | Commercial compilers include:
|
|

Data Types
 | Basic data types:
 | char - character |
 | int - an integer, in the range -32,767 to 32,767 |
 | long int - larger integer (up to +-2,147,483,647) |
 | float - floating-point number |
 | double - floating-point number, with more precision and greater
range than float |
|
 | Does not have an explicit Boolean |
 | Characters and arrays of characters (strings) are represented with single
or double quoting. |
 | Declaring variables:
 | int i; - initializes a variable of type integer called i |
 | int i = 3; - initializes and i and sets it equal to 3 |
 | int i1, i2; - initializes two variables: i1, and i2 |
|
 | Variables are case sensitive. |
 | Arrays:
 | All data types support array declarations. |
 | Example: char line[81]; - declares a character array with 81
positions |
 | Filling an array: line[2] = 'A' |
 | Multidimensional arrays are possible: e.g., short
array_name[50][20][10] creates an array with 10,000 elements. |
|
 | Variable scope:
 | Variables declared in functions live within the function. |
 | static int i; - creates a variable scoped within the file. |
 | extern int i; - creates a variable scoped within an application. |
|

Expressions and Operators
 | Math operators (in order of precedence)
 | * multiplication |
 | / division |
 | % modulus (remainder) |
 | + addition |
 | - subtraction |
 | +x value of x |
 | -x negative value |
 | ++ increment |
 | -- decrement |
|
 | Assignment operator: = |
 | Relational operators
 | <, <=, >, >= |
 | == is equal to |
 | != is not equal to |
|
 | Logical operators
 | && - logical and |
 | || - logical or |
 | ! - logical not |
|
 | Other operators:
 | () - function call, e.g., pow(x,y) |
 | (type) - cast, e.g., (long)x - the value of x is set with type long |
 | sizeof - size in bytes |
 | ?: - conditional evaluation |
 | , - sequence operator |
|

Control Statements
 | If-then
|
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;

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:
 | Standard Input/Output (stdio.h) |
 | Math functions (math.h) |
 | String handling functions (string.h) |
 | Time and date functions (time.h) |
 | Miscellaneous 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):
 | Standard Input and Output - C "assumes" that standard devices are
used for input (keyboard) and output (command line display) |
 | Keyboard input
 | scanf() - reads a standard input (from a keyboard) |
 | gets() - reads a string from standard input |
|
 | Screen output
 | printf() - handles output to the screen |
|
 | File I/O
 | fopen() - opens a file for access, returns a handle for processing |
 | fclose() - closes an open file |
 | fgetc() - reads a character from a file |
 | fgets() - reads a string from a file |
|

Resources
References
Tutorials
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)!
|