Programming in C

Programming in C ! ! ! Programming in C Prof. Gustavo Alonso Computer Science Department ETH Zurich [email protected] ethz. ch http://www. inf. ethz. ch/department/IS/iks/ A brief history of C C as a programming language C Programming “main function “constants, variables, data types “operators, control structures “functions “data structures “pointer arithmetic “structures “dynamic memory allocation ©Gustavo Alonso, ETH Zurich. Programming in C 2 A brief history of C ! ! C as a programming language ! ! ! Programming languages are used to specify, design, and build software systems. Programming languages evolve with the systems they are used to construct. C is a good example of how this process takes place. UNIX was developed at around 1969. It first version was programmed in assembler and run on a DEC PDP-7. The second version was ported to a PDP-11 in 1971 and it was a great success: “16 KB for the system “8 KB for user programs “disk of 521 KB “limit of 64 KB per file !

Hire a custom writer who has experience.
It's time for you to submit amazing papers!


order now

While writing a FORTRAN compiler for UNIX, a new programming language was developed: B B was interpreted (like Java) and, therefore, slow. To solve the performance problems of B, a new language was created: C “allowed generation of machine code (compilation) “declaration of data types “definition of data structures In 1973 UNIX was rewritten in C something that was never done before “C is much easier to handle than assembler but “first C version of UNIX was 20 to 40 % larger and slower than assembler version Programming in C 3 ! C has been standardized (ANSI C) and spawned new languages (C++, Stroustrup, 1986) that improve C The basic characteristics of C are: “small in size “loose typing (lots of freedom, error prone) “structured (extensive use of functions) “designed for systems programming (i. e. , low level programming of the type needed to implement an operating system) “C is higher level than assembler but still close to the hardware and allows direct manipulation of many system aspects: pointers, memory allocation, bitwise manipulation … ! !

As we will see when we study assembler, C is not very far from the assembler language but it provides higher level language constructs (functions, data structures) that facilitate programming without loosing too much performance Being a low level language, C gives a lot of freedom to the programmer: “it has the advantage that good programmers can implement very efficient programs in a compact manner “it has the disadvantage that most of us are not good programmers and the freedom C grants is usually translated in error prone, messy code Gustavo Alonso, ETH Zurich. ©Gustavo Alonso, ETH Zurich. Programming in C 4 This is C #include main(t,_,a) char *a; {return! 0= 3) { a = a – 3; if ( a == 3) a = a * 3;} else a = a * 5; Programming in C 18 ©Gustavo Alonso, ETH Zurich. Programming in C 17 ©Gustavo Alonso, ETH Zurich. switch statement (1) ! Switch statement (2) Switch using break selector int–value 1 = selector false int–value 2 = selector false int–value 2 = selector false Statement 4 true Statement 3 true Statement 1 !

The switch statement is used to conditionally perform statements based on an integer expression (selector) switch (selector) { case int-value1 : statement1; break; case int-value2 : statement2; break; case int-value3 : statement3; break; default: statement4; } The exact behavior of the switch statement is controlled by the break and default commands “break continues execution after the switch statement “default is executed if no other match is found Switch without break selector true Statement 2 witch (selector) { case int-value1 : statement1; case int-value2 : statement2; case int-value3 : statement3; default: statement4; } /* fall through */ int–value 1 = selector false int–value 2 = selector false int–value 2 = selector false Statement 4 true Statement 1 true Statement 2 true Statement 3 ©Gustavo Alonso, ETH Zurich. Programming in C 19 ©Gustavo Alonso, ETH Zurich. Programming in C 20 Switch (example) char a = ‘A’; switch (a) { case ‘A’: x *= x; break; case ‘B’: x /= x; break; default: x += 5; } ! for statement a = ‘A’; switch (a) { case ‘A’: x *= x; case ‘B’: x /= x; default: x += 5; } !

The for statement provides a compact way to iterate over a range of values. for (initialization; termination; increment) { statement } ! ! ! initialization ! Once the case ‘A’ is found, x *= x is executed and then we continue after the switch statement. in all cases, only one case will be executed (either ‘A’ or ‘B’ or ‘default’) Note that a is of type char. It does not matter, char is treated as an integer using type conversion Once case ‘A’ is found, x *= x is executed. However, there is no break. This means we continue executing statements while ignoring the cases (the check is not performed anymore).

Thus, the following statements will also be executed x /= x; x += 5; Programming in C 21 ! All elements in the for loop are optional: for (; ; ); break can be used to interrupt the loop without waiting for the termination condition to evaluate to true continue can be used to skip execution of the body of the loop and re-evaluate the termination condition true termination false increment statement for (int x =0; x

The while statement is used to continually execute a block of statements while a condition remains true. while (expression) { statement } As before, break and continue can be used to terminate the loop or to finish an iteration and go back to the evaluation of the expression for and while are equivalent (can you do it? write a for loop using the while statement and vice versa) expression false true statement The do-while is similar to the while statement except that the loop is always executed once and the condition is checked at the end of each iteration. do { statement } while (expression) break and continue have the same effect as before tatement true ! expression false ! main() { char t; while((t = getchar()) ! = ‘! ’) { if (t >= ‘A’ && t no error, 1 -; not found, 99 -; crash exit() is a very primitive way to terminate a program and one that leaves only very limited chance to deal with failures. More modern programming languages use exceptions and exception propagation mechanisms to indicate the occurrence of an error without having to terminate the program ! Goto C was written as a language that is one step above assembly language. This can be seen, for instance, on the existence of a goto statement Do not use goto when programming.

There are very good reasons to avoid it: “Style: avoid spaghetti code “Use of stack frames ! ! ! ! if (! (buf = AllocMem (BufSize);)) { printf(“kein Speicher vorhanden”); exit(NO_MEM); } ©Gustavo Alonso, ETH Zurich. Programming in C 25 An array is a finite set of variables of the same basic type Instead of giving each variable a name, we use enumeration and group all of them in an array The enumeration of elements within an array always starts with 0. If the array has N elements, the last element is in position N-1 The C compiler does not check the array boundaries “this is a very typical error that t is very difficult to find (usually happens inside loops that traverse the array) “always check the array boundaries before accessing a variable in an array #include float data[5]; /* data to average and total */ float total; /* the total of the data items */ float average; /* average of the items */ main() { data[0] = 34. 0; data[1] = 27. 0; data[2] = 45. 0; data[3] = 82. 0; data[4] = 22. 0; total = data[0] + data[1] + data[2] + data[3] + data[4]; average = total / 5. 0; printf(“Total %f Average %f
“, total, average); return (0); } ©Gustavo Alonso, ETH Zurich. Programming in C 26 Multi-dimensional arrays int a[3][3]

MEMORY Array traversals (examples) 5 6 7 8 9 1 2 3 4 a[0][0] a[0][1] a[0][2] a[1][0]a[1][1] a[1][2]a[2][0] a[2][1] a[2][2] int array[5][5]; for (int i=0; i

x

Hi!
I'm Heather

Would you like to get such a paper? How about receiving a customized one?

Check it out