Open main menu

Wikibooks β

Software Engineers Handbook/Language Dictionary/C

< Software Engineers Handbook(Redirected from Software Engineers Handbook/Language Dictionary/Procedural/C)



looking for the story of C here


C is a full, procedural language.

Execution Entry PointEdit

An executable starts with the main() method.

General SyntaxEdit

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

a = b;


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

 * this is a block comment

Variable DeclarationsEdit

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/ImplementationEdit

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 is defined by curly braces.

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

Conditional StatementsEdit

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
    F = E;
    // more code can be added here.  It is used if and only if A is not equal to B


if (A == B)
    D = C; //more lines of code are not permitted after this statement
    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";
    default: // this is used if none of the previous cases contain the value
        text = "unknown number";

Looping StatementsEdit

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;
    index = index + 1;
while (4 != array[index]);

Output StatementsEdit

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




Implement your own, or find a library.

Garbage collectionEdit

Garbage collection is manual.

Physical StructureEdit

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).


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 ReferencesEdit

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

Books and ArticlesEdit

need references

The C programming language: (sample link: )