City & Guilds 7261/224 | Coding and Programming in C II |
1 | INTRODUCTION
C was created by Dennis Ritchie in 1972 to enable him to write a new UNIX operating system in a high-level language rather than assembler. However, because C can be ported easily from one system architecture to another, and because it allows developers to get into the heart of a machine, it has found a niche between assembler and high level programming languages.
|
|
2 | PRODUCING AN EXECUTABLE FILE | |
2.1 | Source Code
Source code is human readable and contains the statements which constitute your program. Source code files normally have a .c extension. Here is an example of a source code file, which contains a program to display the words hello world on the screen: #include <stdio.h> /* main: display message on screen */ int main(void) { printf("hello world\n"); return 0; }We'll look at this in detail later.
| |
2.2 | Object Files
Source code is normally compiled in to .o files, or .obj files, these are object files. An object file is not human readable. It contains information that the linker needs to produce an executable file.
| |
2.3 | Library Files
Library files are normally supplied with C and they contain the object files for standard functions. They are normally stored in the subdirectory lib. They are not human readable.
| |
2.4 | Executable Files
After object files are produced, they are linked with library files, using the linker, to produce an executable file. An executable file contains executable code (machine instructions for the processor) and is not human readable. Executable files can have the file extension .EXE but it really depends on what system they're being created. In UNIX you can specify the executable file name.
| |
3 | CODE ANALYSIS
Let us now examine the program given previously. A C program consists of functions and variables. A function contains statements that specify the computing operations to be done, and variables store values used during the computation. In the previous example, there were no variables.
| |
3.1 | Function 'main'
You can give functions any names you like (subject to the syntax of identifiers and name space restrictions), but main is special. All programs must have a function called main because this is where all C programs commence execution.
|
|
3.2 | #include Preprocessor Directive
The line #include <stdio.h> is replaced by the file | |
3.3 | Function Overview
A function can pass data to another function by providing a list of
values, called arguments, to the
function it calls. The parentheses after the function name
enclose the argument list. In this example Braces printf("hello world\n"); return 0; To call a function, cite its name followed by a parenthesized list
of arguments, if any. The The exit status is not an issue in this course, and not even
mentioned in the syllabus. Just get into the habit of returning
zero from | |
4 | STRING CONSTANTS
A sequence of characters in double quotes, like The
#include <stdio.h> int main(void) { printf("hello "); printf("world"); printf("\n"); return 0; } to produce identical output. |