Warum C? - Sicherheit auf allen Systemschichten Applikationen Hilfssysteme BS-Werkzeuge BS-Kern HW Evtl. Hochsprachen Skripte C Assembler - je tiefer die kompromittierte Schicht, umso größer der Schaden An Ungentle Introduction to C
What you should know and what you will learn You should know how to program in an imperative language, syntax and semantic of Java, shell-based interaction with a Unix system. You will learn a rough overview about C. What to do next? Get a got book about C and read. Try it (do the assignments). Use the online manual pages. General Properties no OOP functions (with side effects) and kind-of modules no garbage collection, manual memory management no memory protection no internal debug support only standardized basic libraries To understand how it works, forget abstraction For usage, apply abstraction whenever possible
Compilation Stages Sourcecode - declarations (*.h) - functions (*.c) Preprocessor Objectfiles - compiled source code (*.o) - third party libraries (lib*.a, lib*.so) Compiler Executable Linker Runner - bind with dynamic libraries - execute the function main(..) Basic Types, Literals, Expression Basic scalar types (# Bytes) (signed unsigned) char (1), short (2), int (4) float (4), double(8), long double Casting of basic types often does what you want, but you may loose precision, you may interfer with the sign bit. Literals are similar to Java, but no Unicode. Enumerations enum boolean {YES, NO}; Expressions are very similar to Java. No instanceof operator sizeof(<typeid>) returns the amount of memory used by an instance
Aggregates Arrays char str[50]; can be multidimensional indices run from 0 to (n-1) Structures struct Person{int age; int socid}; struct Person fred, carl; structures can create new types typedef struct Person person_t; person_t anja; assignment of structures (fred = carl):copy of every field comparison of structures is not allowed Unions union value {int ival, float fval}; Structures where each field uses the same memory carry no type information => dangerous to use Structure of a Sourcecode File // read declarations of other modules #include <mod1.h> #include../mod2.h... // function and variable definitions int globalvar=-1; int add(int para) { // local variables int help = 0;... // expressions... return para++; }... Search system path Uses current path Variable declarations and expressions must not be intermixed. Every source file is a module.
Scope and Lifetime of Variables... int aglobal; extern int hisglobal; static int alocal;... void function(int p1, char p2) { int m1; static int m2;... }... Global and unique in the whole application Imported from another module Only available in this module Available only in the function, valid only when the function executes value is saved between calls By-Value semantic for parameters Pointers (1/2) What is a Pointer? A pointer is a typed reference to memory Type declaration: int *iptr; void *ptr; Operators: <type> *id // pointer to an integer // pointer to something access the referenced content *(ptr) get the reference of a variable &(id) Example: int i=1; int *iptr; iptr = &i; // set iptr to point at i *(iptr) = 2; // set i from 1 to 2
Pointers (2/2) Pointers,Structures, Like in Java pointers/references allow for efficient handling of structures (objects without methods). void workonperson(person_t *personptr) {} Recursive aggregates: struct Tree { int value; struct Tree *left,*right; }; Shortcut: struct Tree *id; (*id).left is equals to id->left Pointers to functions are possible (but out of scope here) Pointer Arithmetic Arrays are very similar to Pointers Arrays support [] operator An array is no variable, it cannot be changed (its values can) // Example: add 1 to every member of an array int arr[10], i, *iptr;... iptr=arr; // arrays are some kind of pointers for (i=0;i<10;i++) {*iptr += 1; iptr++;} pointers are typed, +1 will shift the pointer according to the length of the type (arr++ is forbidden) all integer arithmetic is allowed casting + pointer arithmetic opens strange possibilities
Strings Strings are: either pointers to character char *str; or arrays of characters char str[80]; strings are mutable (=> side effects are dangerous) By convention a string is terminated by \0 hallo is actually { h, a, l, l, o, \0 } you need to reserve memory for the termination character if you forget termination, the strangest things can happen the standard library provides numerous functions for strings do a man string on a Unix box Basic I/O Formatted output to stdout: int printf(char *format,... ); (C supports arbitrary many parameters; out of scope) str = STR ; num = 4712; printf( String: %s, Num: %d \n, str, num); will print: String: STR, Num: 4712 Input from stdin:int scanf(char *format,... ); char name[80]; int age; float pi; scanf( %s,name); scanf( %d %f,&age, &pi); input a string from stdin until newline/eof parse the string according to the specifier store the results in the given variables read the documentation!!!
Memory Allocation void *malloc(size_t size) allocates some memory and returns a pointer to it void free(void *ptr) frees the memory struct Person *createperson(char *name) { struct Person *res; res = (struct Person *) malloc(sizeof(struct Person)); res->name = name; return res; } Preprocessor Constants #define MAXSIZE 100 Macros #define ASSERT(cond,msg) if(cond) {printf(msg);} Conditional inclusion #define DEBUG #ifdef DEBUG #define ASSERT(cond,msg) if(cond){printf(msg);} #else #define ASSERT(cond,msg) #endif
How to compile and start Start of an executable by calling the function int main(int argc,char **argv) with the command line parameters (Array of strings). Create an executable from a C-File > gcc file.c -o file Create an object file file.o > gcc -c file.c Link object files > gcc file1.o file2.o file3.o -o file Start the executable > file