Friday, May 4, 2012

C Basics

    Now that the C compiler is functioning, it's time to start building and understanding programs in the C language.  To begin, let us examine the program from the previous post:
#include <stdio.h>

int main() {
    printf("C compiler working");

    getchar();
    return 0;
}


    First, the command #include <stdio.h> will read the file stdio.h which is a reference to the C Standard Input and Output library (or cstdio) and has helpful functions for printing text output and reading user input.  More information about stdio.h can be found at http://www.cplusplus.com/reference/clibrary/cstdio/ which also discusses its helpful file handling functions.  For this program, only the functions printf and getchar were used from stdio.h.

    Next, the line int main() { declares a function called main with no parameters that returns an int, or integer.  Functions are free to return any type of data, have an arbitrary number of various parameters, and have any name; but main is reserved for the function where the program will first start performing commands in order.  Every C program must therefore have exactly one main function.  Also, the { symbol indicates the start of the sequence of commands which will be part of the main function.

    The command printf reads a string of characters from its parameter list and outputs it to the terminal or command prompt.  Upper case C and space are examples of individual characters.  Also, the command getchar waits until the user types any character from the keyboard then does nothing.

    Finally, the command return 0; ensures that the main function returns a zero to indicate a successful operation. The file closes with a } to complement the { from before and close the declaration of the main function.

    The semi-colons at the end of each command separates them from one another since C will ignore most white spaces(tabs, spaces, returns, etc.) in your file.  This means main could be written as int main(){printf("C compiler working");getchar();return 0;} but writing code that can be read by yourself and fellow humans is recommended.

    Now, to become more familiar with C syntax, here are some modifications to the program:
// use functions from cstdio 
#include <stdio.h>

//This line is a comment ignored by C
//This is because it begins with //

/*
For comments consisting of many lines,
it is best to place a /* at the
beginning and end the comment lines
with a * and / as seen here.
*/

//say_hello commands
void say_hello() {
    printf("C compiler working\n");
    return; //returns nothing
}

//wait_key commands
void wait_key() {
    printf("Press any key to exit\n");
    getchar();
    /* void functions don't require
    a return command, but it can be
    used to stop a function and
    return to where it was called */
}

//program starts here
int main() {
    //go to say_hello commands
    say_hello();

    //go to wait_key commands
    wait_key();

    //exit program successfully
    return 0;
}

    This example introduces comments which are also ignored by C.  Comments are intended to communicate the purpose of your code to people who read them.  It is highly recommended that programs contain comments because it helps others understand the code, but can also help the author if he or she revisits a long forgotten program and needs a quick review.

    This example also shows two new functions: say_hello and wait_key.  The names use underscores since C does not allow spaces in function names.  Both return nothing, which is indicated by the word void, and require no parameters.  An additional printf was called to tell the user that a key press is needed to exit.  Notice that these functions are declared before main is declared.  This is because main must only call functions which have already been defined.

    Both printf calls now have a \n character in the parameter to create a return or enter character.  That was not a typo; the "newline" character consists of \ and n.  The \ tells C that the next character will use its secondary meaning, so the character n has a second meaning as the end of a line and the start of a new line.  This also works for \" since placing " anywhere in the string of characters would mean the end of the string, while \" is used to place a " character in the string.  Try placing quotes around compiler as practice.

    The program begins at main, and the first command is to call the function say_hello.  The function say_hello calls printf and returns control of the program to main.  The program then returns to main and calls wait_key.  Function wait_key calls printf and getchar and finishes.  Control of the function returns to main again and finally return 0 is called to exit the program.

    The next post will discuss variables, especially integers.

No comments:

Post a Comment