looking for the story of C here

Type

edit

C is a full, procedural language.

Execution Entry Point

edit

An executable starts with the main() method.

General Syntax

edit

The typical statement is completed by a semi-colon. For the assignment of b to a use:

a = b;

Comments

edit

Block comments are specified by a starting /* and ending */ They can span multiple lines.

/*
 * this is a block comment
 */

Variable Declarations

edit

Declarations need to appear at the top of the scope.

Declare i as an integer:

int i;

Declare i as an integer and give it an initial value of 0:

int i = 0;

Method Declaration/Implementation

edit

Methods are declared by specifying the interface for the method.

return_type function_name(argument_1_type arg_1_name,
                          argument_2_type arg_2_name);

Method implementation repeats much of the same information

return_type function_name(argument_1_type arg_1_name, 
                          argument_2_type arg_2_name)
{
    // work with arg_1_name, arg_2_name, and default_arg_name
    // depending on the argument types the variables are passed by 
    //   value, reference, or are constant
    // don't forget to return something of the return type
    return 36;
}

Scope

edit

Scope is defined by curly braces.

{ // this the beginning of a scope
    // the scope is about to end
}

Conditional Statements

edit

If and only if A is equal to B assign C to D, otherwise, assign E to F.

if (A == B)
{
    D = C;
    // more code can be added here.  It is used if and only if A is equal to B
}
else
{
    F = E;
    // more code can be added here.  It is used if and only if A is not equal to B
}

or

if (A == B)
    D = C; //more lines of code are not permitted after this statement
else
    F = E;

Alternatively, a switch statement can be used for multiple choice operations. This sample converts a number input to text.

switch (number_value)
{
    case 37:
        text = "thirty-seven";
        break; // this line prevents the program from writing over this value with the
               //   following code
    case 23:
        text = "twenty-three";
        break;
    default: // this is used if none of the previous cases contain the value
        text = "unknown number";
}

Looping Statements

edit

This code counts from 0 to 9, adding up the contents of the array.

int i = 0;
for (int index = 0; index < 10; index = index + 1)
{
    i = array[index];
}

This code repeats until the number 4 is found. If this runs off of the end of the array, there could be a problem.

int index = 0;
while (4 != array[index])
{
    index = index + 1;
}

This code increments the counter before the check is made, so that it starts with element 1.

int index = 0;
do
{
    index = index + 1;
}
while (4 != array[index]);

Output Statements

edit
printf ("%s","Hello world!\n");

Containers

edit

struct

Algorithms

edit

Implement your own, or find a library.

Garbage collection

edit

Garbage collection is manual.

Physical Structure

edit

Generally, the interfaces are defined before the implementation in header files (often *.h) or above the implementation. The implementation files are often named *.c. Useful collections of classes can be compiled into libraries, often *.dll, *.a, or *.so, which can be compiled into executables (statically linked) or used on the fly (dynamically linked).

Tips

edit

Don't confuse these two:

=  // assignment
== // comparison, is equal to

Often using the one you don't want will compile, and will produce results you did not expect.

A good practice is to write; if(CONSTANT == variable) rather than if(variable == CONSTANT) since the compiler will catch; if(CONSTANT = variable) but not if(variable = CONSTANT).

Use a compiler that generates lots of helpful warnings, even if the code is valid. You could also / instead use a program such as 'lint'. This will help you avoid simple bugs such as the above-mentioned bug "if ( x = 1 )" instead of "if ( x == 1 )".

Arrays start with index 0.

Web References

edit

List additional references on the web. Please include for what level reader the references are appropriate. (beginner/intermediate/advanced)

Books and Articles

edit

need references

The C programming language: (sample link: http://cm.bell-labs.com/cm/cs/cbook/ )