380 likes | 503 Views
Learn about the history, characteristics, syntax, and data structures in the influential C programming language. Understand pointers, memory access, operations, and absent features. Discover the syntax, undeclared behavior, and pointers' role in building data structures. Dive into this comprehensive guide.
E N D
Programming, Data Structures and Algorithms (The C Programming Language) Anton Biasizzo
The C Programming Language • General purpose programming language • Developed 1972 at AT&T Bell Labs for use with the UNIX Operating System • Designed for implementing system software • Influenced the development of other programming languages: • C++ (initially an extension of C), • Java, • Perl, • C#, …
The C Programming Language • Procedural system implementation language • Provides low-level access to memory • Language constructs map efficiently to machine instructions (useful for application formerly coded in assembler) • Encourage machine-independent programming • Available in wide range of platforms: from embedded systems to supercomputers
History • Initial development from 1969 to 1972 at AT&T Bell Labs by Dennis Ritchie. • By 1973 the most of the UNIX kernel were rewritten in C. • In 1978 first edition of “The C Programming Language” published by Brian Kernighan and Dennis Ritchie (K&R C). • During late 1970 and 1980 widespread on mainframe computers, minicomputers and microcomputers (IBM PC) • In 1983 formed ANSI committee to establish standard specification of C. • In 1990 the ANSI C standard was adopted (C89 or C90) • In 1999 standard revised (C99) • In 2007 work began for another standard revision (C1X)
Characteristics • Encourages structured programming. • Allows lexical variable scope and recursion. • Has a static type system. • All executable code is contained within functions. • Function parameters are passed by value. Pointer values used to emulate pass by reference. • Free-format source code • Semicolon is statement terminator • Aggregate data types (struct)
Characteristics • Relatively small set of reserved keywords • A preprocessor for file inclusion, macro definition, conditional compilation, … • Assignment is denoted by equal-sign (like FORTRAN) • Large number of compound operators (+=. ++, &=,…) • Partially weak typing (characters used as integers) • Low-level access to memory via typed pointers • Array indexing also as pointer arithmetic • Variables may be hidden in nested blocks • Complex functionality delegated to library routines • Ad-hoc run-time polymorphism (function and data pointers)
Absent Features • No direct assignment of compound data (array, string, …) • No requirement for array bounds check • No operations on whole array • No automatic garbage collection • No exception handling • No compile-time polymorphism (no function or operator overloading) • Limited support for object-oriented programming • No native support for networking and multithreading • No standard libraries for computer graphics, …
Undefined behavior • The exact behavior that arises is not specified by standard • Many operations in C have undefined behavior • Not diagnosed at compile time • Examples of undefined behavior: • Signed integer overflow • Reading the value of a variable before initializing • Accessing out of bounds of an array • Reaching the end of non-void function without a return statement • Some behavior left undefined to allow compilers to generate more efficient code
Syntax • Free-form source code which allows whitespace to format code (in contrast to column based or text-line based) • Comments appear between delimiters /* and */ or following // until the end of line (in C99) • Source files contain declarations and function definitions • Function definitions contain declarations and statements • Declarations • Define new types (using struct, union, enum) • Assign type and reserve storage to variable • Sections of code are enclosed in braces { } (curly brackets) • To limit the scope of declarations • To act as a single statement for control structures
Data structures • A static weak typing type system • Built-in types: • Character (char) • Could be interpreted as 8 bit integer • Signed or unsigned (signed, unsigned) • Integers (reserved keyword int): • Various size: 32 bit (long), 16 bit (short), 64 bit (long long) • Signed or unsigned (signed, unsigned) • Floating point numbers • float (32 bit floating point type) • double (64 bit floating point type) • Derived data types: enumerations (enum), arrays, pointers, records (struct) and unions (union) • Type casting: convert a value from one type to another
Pointers • Pointers are simple type of reference to an object: • A simple variable (strings) • A record (used for lists, trees, …) • A function (callback functions) • Run-time representation is the address of the object in memory • Pointer’s type includes the type of the object pointed to: expressions can be checked at compile time • Manipulated using pointer arithmetic • Pointer arithmetic is scaled by the size of the pointed object • Null pointer is a pointer to non-valid location • Used for the final node of the linked list • Error indication from functions returning pointers,…
Pointers • Voidpointer (void *) point to an object of unknown type • Cannot be dereferenced • Pointer arithmetic is not allowed • Can be converted to and from any object pointer type • Pointers are used for dynamic memory allocation • Careless use is potentially dangerous: • Pointer variable can be made to point to arbitrary location • Deallocated and reused pointers (dangling pointers) • Non-initialized pointers (wild pointers) • Other languages use more restrictive reference types
Arrays • Array types are typically of a fixed, static size specified at compile time • No bounds checking • Writing beyond the array bound – buffer overflow or buffer overrun • Multidimensional arrays: • No special provisions • Recursion within the type systems (arrays of arrays) • The indices are ordered • Passed to functions as a pointer to a first object • In run-time internally accessed via pointers • Unification of arrays and pointers • Possible to allocate a block of memory at run-time and treat it as an array (using pointers)
Array-pointer interchangeability • The array-subscript notation x[i] can be viewed as pointer operation • Access to the (i+1)th object of several adjacent data objects pointed to by x. • Formally x[i] is equivalent *(x+i) • Type of the object pointed by pointer is known at compile time and address pointed to by x is incremented by i*sizeof object • Equivalent assignment x[i] = 1; *(x + i) = 1; *(i +x) = 1; i[x] = 1; • Difference: pointer occupies memory while array itself does not (cannot assign a value to an array variable)
Memory management • Important function of programming language • C provides three methods for allocating memory: • Static memory allocation Space for the object is provided in program binary at compile time • Automatic memory allocation Temporary objects are stored on the stack and the space is freed after the block in which they are declared is exited • Dynamic memory allocation Blocks of memory of arbitrary size can be requested from heap at run-time using library functions and freed by corresponding library function.
Memory allocation • Allocation overhead: • Static allocation has no allocation overhead • Automatic allocation may have allocation overhead • Dynamic allocation can have substantial overhead • Allocation management: • Automatic and static allocation are managed by compiler • Dynamic allocation is managed by program (memory leak) • Allocation size: • Automatic and static allocation have fixed size • Dynamic allocation provides arbitrary size of arrays • Initialization: • Automatically and dynamically allocated objects are not initialized
Operators • Expression statements can use built-in operators • Arithmetic (+, -, *, /, %) • Equality testing (==, !=) • Order relations (<, <=, >, >=) • Boolean logic (!, &&, ||) • Bitwise logic (~, &, |, ^) • Bitwise shifts (<<, >>) • Assignment (=, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=) • Increment and decrement (++, --) • Reference and dereference (&, *, [ ]) • Conditional evaluation (? :) • Member selection (., ->) • Type conversion ( ( ) ) • Object size (sizeof)
Statements • C uses statements to specify actions • Expression statement consist of an expression to be evaluated, followed by semicolon (statement terminator) • Conditional statements • if (condition_expression) statement; • if (condition_expression) statement else alternative_statement; • switch (expression) { case value1 : statements1; case value2 : statements2; …. case valuen : statementsn; default : other statements; }
Statements • Non-zero value of the condition expression satisfies the condition (true) • Examples: if (min > value[i]) min = value[i]; if (in = fopen(“input.txt”, “r”)) read_file(in); else report_error(“Error opening file input.txt\n”); switch (state) { case 0: if (in = 1) state = 1; break; case 1: if (in = 0) state = 2; break; case 2: state = 0; break; default: state = 2; }
Statements • Iterative statements • do statement while (condition_expression); • while (condition_expression) statement; • for (initial_expression; condition_expression; loop_expression) statement; • Control-flow statements • break statement is used to leave inner-most loop or switch statement • continue statement is used to skip to the loop_expression or condition_expression part of the iterative statement • Non-structured statements • goto labelid; branches directly to the designated label within function.
Statements • Examples: i = 0; do // compute squares value[i] = i*i; while (i < n); while (i < sqrt(n)) { // print dividers of a given number if (n % i == 0) printf(“%d divides %d\n”, i, n); } sum = 0; // sum non-negative elements of an array for (i=0; i<n; i++) { if (value[i] < 0) continue; sum += value[i]; }
Function definition • Type of the return value. • Name of the function. • Argument list with their types • Body of the function int max(int N, int *values) { int i, max; for (max=*values,i=1; i<N; i++) if (max < values[i]) max = values[i]; return max; } • main is special function representing the entry point of the program.
Libraries • Libraries are primarily a method of extension • Library is a set of precompiled functions contained within a single archive file • Each library typically has associated one or several header files, which contains the descriptions (prototypes) of the functions within the library • In order to use the library the program source code must include corresponding header file and the program must be linked with the library archive
Libraries • Most common is C standard library specified by ISO and ANSI C standard • Stream input and output • Memory allocation • Mathematics • String manipulation,… • Applications targeting UNIX and UNIX-like systems use POSIX libraries • Provide interface to the kernel • Many variety of other libraries • Libraries for other high-level languages are often written in C because of efficient code generation
Example • Simple example #include <stdio.h> // including std. library header file int main(void) { printf(“hello, world\n”); return 0; } • This example does not require additional libraries gcc –o hello hello.c
Language environment • Various different development environments • Gnu C/C++ • Microsoft Visual C++ • Intel C++ compiler • C++ Builder • Oracle Developer Studio (Sun Studio) • Commercial tools provide integrated development environment (IDE) • Free IDE • Eclipse • Code::Blocks • NetBeans
Language environment • Target applications type • Command line applications • MS Windows applications • X Windows applications • Command line application environments • UNIX or UNIX-like system with basic development tools • Native installation of UNIX-like OS • Installation on virtual machine (VirtualBox, VMWare, …) • Windows port of Gnu toolchain Cygwin
Oracle VirtualBox • Available as Open Source Software (GPL v2) • Host operating systems include MS Windows, Linux, OS X, Solaris • For MS Windows installation • Use VirtualBox for Windows host • After installation create Linux guest • Download Linux installation CD image (Kubuntu, Ubuntu, Fedora, …) • Mount the image as CD in the Linux guest • Boot Linux guest – Install linux • Unmount CD • Optionally in Linux guest install VBoxGuestAdditions
Eight queen puzzle /***** Put eight queens on the chess board in such a way that they don’s attack each other *****/ #include <stdio.h> int board[8]; // y positions of the queen at index column void putboard() { int i; for (i=0; i<8; i++) printf(“%4d”, board[i]); printf(“\n”); }
Eight queen puzzle // is the queen at x-th column attacked by any already positioned queen int unsafe(int x) { int i, t, y; y = board[x]; for (i=1; i<=x; i++) { t = b[x-i]; if ( (t ==y) || (t == y-i) || (t == y+i) ) return 1; } return 0; }
Eight queen puzzle int main(void) { int x = 0; board[0] = -1; while (x >= 0) { do { board[x]++; } while ((board[x] < 8) && unsafe(x)); if (board[x] < 8) if (x < 7) b[++x] = -1; // initialize next column else putboard(); // print solution else x--; // no more solutions in x column, backtrack } return 0; }