The C Language was originally developed to write and implement the UNIX operating system. By writing the originally implementation of UNIX in C, a higher level language than machine language and assembly language, anyone could easily understand, modify, and extend the UNIX operating system. When the first versions of UNIX were released into the academic community, the academic community quickly converted their theoretical courses, such as operating systems, into more practical ones. UNIX became the test-bed of operating system ideas at Universities due to its openness. Many of the things we take for granted were originally developed on the UNIX operating systems. For example, E-Mail was first developed on the UNIX operating system. The concept of a distributed file system (ie a Network File System(NFS)) was also developed on UNIX. Since the only way to modify the UNIX system was through the C language, the C language became the language of choice for programmers using the UNIX operating system. The C language prospered with the UNIX operating system, and porting of UNIX code(written in C) to other machine platforms pushed the wide acceptance of the C language on other platforms..
The C language is a general purpose language that is closer to machine language than any other language, other than assembly language. Assembly language is a simple language that corresponds to a given machine language. Every machine instruction has an equivalent English-like assembly instruction, and every assembly instruction has an equivalent machine instruction. Assembly is a bit better than machine language as it can be read more easily(ie English not numbers). But, programs written in assembly or just as difficult to write as in machine language. The C language offers users the ability to write programs easily that can still have the flexibility of machine language (such as speed and size). Many different device drivers, that once were written in Machine Language or Assembly Language, are mostly written in C now.
There are two basic variations in the C language called the K&R C and ANSI C. The K&R C was based on the original version of C as used on UNIX. Its name is so called because of the creators Kernigan and Richie. It is still used as a standard on all machines that use UNIX has the operating system platform. It is archaic, and through standardization, it has been replaced by ANSI C. These notes are based completely on ANSI C, using an ANSI C Compiler.
In all our programs, we will be using a C compiler and not a C-interpreter. C interpreters are very useful for debugging purposes, but most translators found in use are C compilers. The rest of the notes that follow assume that we are using a C compiler when translating a program for execution. A program is usually written as a text file containing C code(a program written in the langauge of C). Usually a file extension of .c is used to indicate that the file contains C code(ie first.c, one.c).
Simplest C Program:
The simplest program that can be written in the C language is called the empty program. This empty program consists of a single function called main. All C programs must contain a main function for execution to begin. The C language is case-sensitive. All elements of the language are lower case, although user defined symbols can be of any mixture of case.
|
All programs consist of the main function. The group of lines beginning with the curly open parenthesis and the closing curly parenthesis is referred to as the body of the main function. Any function that contains a body is said to be defined. The body is said to be the definition of the function. All programs must have a definition of the main function to execute.
The single statement int main(void) is referred to as the head of the function. The declaration head, int main(void), indicates that function main will return an integer value. For this program the return value of this function is specified by return 0; in the main body. The return statement will return from the main function and will return back to the operating system.
The return value specifies a value of zero, and is used by the operating system to indicate that the C program failed, or it worked.. These return values are used in UNIX scripts and DOS batch files. A return value of zero is a way of informing the operating system from your program that your code has worked. A return value of any thing else, not a zero, informs the operating system that your program has failed. The following program, upon completion of the execution, tells the operating system that it has failed. Technically, the return value can only be a value between 0-255. Even if a larger value is returned, the operating system only sees the first byte of your returned integer.
|
Terminals and Cursor Operations:
Technically a terminal is not a computer system as it only contains two basic units of a computer system: Input Unit and Output Unit. The output is usually displayed on a monitor. Personal computers can emulate being a terminal, and usually the display is broken into 25 rows with 80 characters per row. The standard input and output library functions of C are meant for a text terminal. A text terminal has a blinking or block character, called a cursor, that can move from left to right and top to bottom only on the screen. A character can only be printed at the location of the cursor. After a character is printed on the screen at the cursor, the cursor automatically moves to the next character position to the adjacent character spot to the left. If the character just printed is as the end of a row, the cursor is automatically pushed to the beginning of the next line. If the cursor is located at the end of the screen, bottom right corner, all the lines on the screen are pushed up by one line and the cursor is placed at the beginning of the last line.
Stream Output C function:
The standard stream output function in C is called printf. The next program will print Hello World on a screen.
|
In the C language, there are no standard C functions for either input or output. The C language has nothing more than control and loop structures. The elements, the words, of the C language are called keywords. Keywords ,such as int and return, are reserved and cannot be used as either function names or variable names. The symbol printf is the name of a function that is used for standard output, but it is not part of the C language. The first line of our program in our C program is to tell the translator that the symbol "printf" is a function that returns an integer and has arguments of at least a string. Without this first statement the C compiler will assume that the printf symbol is a mistake and complain of errors in your program.
Strings in C consist of a sequence of characters nested in between two double quotes. In our program, "Hello World" is a C string. The above program will print Hello World commencing wherever the cursor is on the screen. The first character 'H' is printed and the cursor is moved to left by one character spot, the next character 'e' is printed and cursor is moved to the next character spot, and this process is repeated until the whole line is finished printing. After the line is finished printing on the screen, the cursor should be one character spot to the left of the last character 'd'.
The next program will perform the same as our last program. The first printf function will print out a "Hello", the next will print a space, and the last will print "World"). As the cursor stays exactly where is left at the end of each "printf" statement, the output is identical to the the output of the last program.
|
Special characters embedded in the string can perform special actions. Any character following a \ is referred to as an escape sequence character. A '\n' is used to force the cursor to move to the beginning of the next line.
The next program will print "Hello World" on two different lines. The first printf statement will force the cursor to move to the beginning of the next line. The second printf will output "Hello World" and then move the cursor to the beginning of the next line. The third statement will print a "Hello World" and move the cursor to the beginning of the next line. The return statement will exit the main function and return back to the operating system.
|
To generate the special character \, use the string "\\". The double quotes can be generated by the escape character \". The escape character '\a' does not generate any visible characters. It instead generates a short beep on the computer. The next program will generate a double quoted Hello World and beep three times.
|
Comments in C program:
Comments in large programs are very useful to a programmer to read and understand his code. Comments in C begin with a double forward slash and extend to the end of the line. Another form used in C is by placing comments between /* and the characters */. Comments are not translated. The following program contains comments.
|
Although this program is commented well, one should always remember that comments are meant to ease the understanding of a program. Comments should be inserted only when a piece of code is difficult to understand. Too many comments are just as bad as putting too few comments into a program. Commenting every line of code is a bit too extreme, as this reduces the productivity of the programmer in writing new code. But too few comments also reduces the productivity of a programmer. After a certain amount of code is written without any comments, the programmer can easily get confused about what he has written. He ends up wasting valuable time trying to analyze the code he has written minutes earlier. Poor commented code also leads to poor code reuse and maintenance.