Programming in C

Only available on StudyMode
  • Download(s) : 949
  • Published : October 29, 2009
Open Document
Text Preview
Programming in C
! ! !

Programming in C
Prof. Gustavo Alonso Computer Science Department ETH Zürich alonso@inf.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 Zürich.

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

!

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 Zürich.

©Gustavo Alonso, ETH Zürich.

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 Zürich.

Programming in C 17

©Gustavo Alonso, ETH Zürich.

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

switch (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...
tracking img