Introduction To C: Basic Training
Introduction To C: Basic Training
Basic training
Basic C
C: History
When writing an OS kernel, efficiency is crucial This requires low-level access to the underlying hardware:
e.g. programmer can leverage knowledge of how data is laid out in memory, to enable faster data access
UNIX originally written in low-level assembly language but there were problems:
No structured programming (e.g. encapsulating routines as functions, methods, etc.) code hard to maintain Code worked only for particular hardware not portable
Basic C
C: Characteristics
Direct access to memory layout through pointer manipulation Concise syntax, small set of keywords Currently, the most commonly-used language for embedded systems
Block structure Some encapsulation of code, via functions Type checking (pretty weak)
Basic C
Separate compilation
A C program consists of source code in one or more files Each source file is run through the preprocessor and compiler, resulting in a file containing object code Object files are tied together by the linker to form a single executable program
Source code file1.c Source code file2.c
Preprocessor/ Compiler Preprocessor/ Compiler
Libraries
Linker
Separate compilation
When modifying a program, a programmer typically edits only a few source code files at a time. With separate compilation, only the files that have been edited since the last compilation need to be recompiled when re-building the program. For very large programs, this can save a lot of time.
Basic C
To compile and link a C program that is contained entirely in one source file: cc program.c The executable program is called a.out by default. If you dont like this name, choose another using the o option: cc program.c o exciting_executable To compile and link several C source files: cc main.c extra.c more.c This will produce object (.o) files, that you can use in a later compilation: cc main.o extra.o more.c Here, only more.c will be compiled the main.o and extra.o files
will be used for linking.
To produce object files, without linking, use -c: cc c main.c extra.c more.c
6 Basic C
Basics of C Environment
Program edited in Editor and stored on disk Preprocessor program processes the code Creates object code and stores on disk
Phase 1
Editor
Disk
Phase 2 Preprocessor
Disk
Phase 3
Compiler
Disk
Phase 4
7
Linker
Disk
Basic C
Basics of C Environment
Primary memory
Phase 5 Loader Puts program in memory
CPU
Basic C
The preprocessor
The preprocessor takes your source code and following certain directives that you give it tweaks it in various ways before compilation. A directive is given as a line of source code starting with the # symbol The preprocessor works in a very crude, wordprocessor way, simply cutting and pasting it doesnt really know anything about C!
Your source code Enhanced and obfuscated source code Object code
Preprocessor
9
Compiler
Basic C
Use of comments
/* ** This program reads input lines from the standard input and prints ** each input line, followed by just some portions of the lines, to
10
Basic C
Comments on comments
Dont use /* */ to comment out code it wont work if the commented-out code contains comments
/* Comment out the following code int f(int x) { return x+42; /* return the result */ } */ This will not!
Anyway, commenting out code is confusing, and dangerous (easy to forget about) avoid it
11 Basic C
Preprocessor directives
#include <stdio.h> #include <stdlib.h> #include <string.h>
The #include directives paste the contents of the files stdio.h, stdlib.h and string.h into your source code, at the very place where the directives appear. These files contain information about some library functions used in the program:
stdio stands for standard I/O, stdlib stands for standard library, and string.h includes useful string
manipulation functions.
Preprocessor directives
#define MAX_COLS 20 #define MAX_INPUT 1000
every instance of MAX_COLS is replaced with 20, and every instance of MAX_INPUT is replaced with 1000.
13
Basic C
Pieces of C
Definitions of data in memory Arithmetic, logical, and assignment operators in an infix notation Sequences of conditional, iteration, and branching instructions
Expressions
Statements
Functions
14
Basic C
Function prototypes
int void read_column_numbers( int columns[], int max ); rearrange( char *output, char const *input, int n_columns, int const columns[] );
These look like function definitions they have the name and all the type information but each ends abruptly with a semicolon. Wheres the body of the function what does it actually do? (Note that each function does have a real definition, later in the program.)
15
Basic C
Function prototypes
Q: Why are these needed, if the functions are defined later in the program anyway? A: C programs are typically arranged in top-down order, so functions are used (called) before theyre defined.
(Note that the function main() includes a call to read_column_numbers().) When the compiler sees a call to read_column_numbers() , it must check whether the call is valid (the right number and types of parameters, and the right return type). But it hasnt seen the definition of read_column_numbers() yet!
The prototype gives the compiler advance information about the function thats being called.
Of course, the prototype and the later function definition must match in terms of type information.
Basic C
16
program execution.
Int main( void ) {
value
Q: Integer value? Isnt the program just printing out some stuff and then exiting? Whats there to return? A: Through returning particular values, the program can indicate whether it terminated nicely or badly; the operating system can react accordingly.
Basic C
17
In this program, the notions of string, array, and pointer seem to be somewhat interchangeable:
In main(), an array of characters is declared, for purposes of holding the input string:
char input[MAX_INPUT];
Yet when its passed in as an argument to the rearrange() function, input has morphed into a pointer to a character (char *):
void rearrange( char *output, char const *input,
18
Basic C
A pointer is simply a memory address. The type char * pointer to character signifies that the data at the pointers address is to be interpreted as a character. An array is simply a pointer of a special kind:
The array pointer is assumed to point to the first of a sequence of data items stored sequentially in memory. How do you get to the other array elements? By incrementing the pointer value.
A string is simply an array of characters unlike Java, which has a predefined String class.
19
Basic C
o
(char)
i
(char)
n
(char)
t
(char)
e
(char)
r
(char)
NUL
(char)
input What is input? Its a string! Its a pointer to char! Its an array of char!
How do we get to the n? Follow the input pointer, then hop 3 to the right *(input + 3) - or input[3]
20
Basic C
C Storage Classes
#include <stdlib.h> int global_static; static int file_static;
Linker-visible. Allocated at fixed location Visible within file. Allocated at fixed location.
void foo(int auto_param) Visible within func. { Allocated at fixed static int func_static; location. int auto_i, auto_a[10]; double *auto_d = malloc(sizeof(double)*5); }
21
Basic C
C Storage Classes
#include <stdlib.h> int global_static; static int file_static;
void foo(int auto_param) { Space allocated on static int func_static; stack by function. int auto_i, auto_a[10]; double *auto_d = malloc(sizeof(double)*5); }
Space allocated on heap by library routine.
22 Basic C
23
Basic C
More flexible than automatic variables (stacked) More costly in time and space malloc() and free() use complicated non-constant-time algorithms Each block generally consumes two additional words of memory Pointer to next empty block Size of this block Common source of errors Using uninitialized memory Using freed memory Not allocating enough Neglecting to free disused blocks (memory leaks)
24
Basic C
Memory usage errors so pervasive, entire successful company (Pure Software) founded to sell tool to track them down Purify tool inserts code that verifies each memory access Reports accesses of uninitialized memory, unallocated memory, etc. Publicly-available Electric Fence tool does something similar
25
Basic C
What are malloc() and free() actually doing? Pool of memory segments:
Free
malloc(
26
Basic C
Rules:
Each segment contiguous in memory (no holes) Segments do not move once allocated
malloc()
Find memory area large enough for segment Mark that memory is allocated
free()
27
Basic C
Very efficient code generation follows from close semantic match Language lets you do just about everything Very easy to make mistakes
28
Basic C
HAVE FUN
29
Basic C