Title: CIS05 Comparative Languages Introduction
1CIS05 Comparative LanguagesIntroduction
- Marc Conrad
- D104a (Park Square Building)
- Marc.Conrad_at_luton.ac.uk
- Outline
- Week 1-6 (MC) C/C, Java, C, other object
oriented languages as Eiffel, Cecil, ... - Week 7-12 (Jie Zhang) Other (not necessarily
object oriented) languages
2Other information
- Further reading Internet Library.
- Practicals are in the Linux-Lab.
- Marking
- 1st assignment (week 7) 20
- 2nd assignment (week 11) 40
- Exam 40
3Today
- General introduction into programming languages -
overview. - History of C.
- Comparative Languages in Cambridge.
- Example of a C program.
- Structures in C.
4A history of programming languages - the early
days
- 1957 Fortran. (owned by IBM). Still used for
scientific computing, current version Fortran 90. - 1958 ALGOL. Developed as a non-proprietary
version of Fortran. Not widely used in practice
but a reference language for many research
papers. - 1967 Simula 67. Introduced Classes and
Inheritance therefore the first Object Oriented
Programming language. - 1980 Smalltalk. Develops further Object Oriented
Concepts
http//www.cs.iastate.edu/leavens/ComS541Fall97/h
w-pages/history/ http//en.wikipedia.org/wiki/Lis
t_of_programming_languages
5A history of programming languages C and C
- 1972 C. Developed as the implementation language
of the UNIX operating system. Allows system
independent low level programming. - 1979 Bjarne Stroustrup designs C with classes
which becomes later (1983) C. - 1989 ANSI C is published - a standardised version
of C. - 1989 work on an ISO standard on C begins.
- 1997 ISO C is published.
Bjarne Stroustrup http//www.research.att.com/bs
/
6A history of programming languages (recent).
- 1991 Python A high level interpreted,
interactive, object oriented scripting language.
http//www.python.org - 1995 Java An object oriented language originally
designed for video recorders. Now used in many
application areas. http//java.sun.com - 1997 PHP 3.0 A general purpose language designed
for web applications. http//www.php.net - 2000 C A Java like language for Microsofts .NET
framework. http//msdn.microsoft.com/vcsharp/
7Categorisation of Computer Programming Languages
- Classification according to levels of
abstraction - Machine code, assembler, high level languages
- Classification according to generations of
programming languages - 1GL, 2GL, 3GL, 4GL, 5GL
- Classification according to application area
- General Purpose, Research, AI, Games, ...
- Classification according to paradigm
- Object oriented, procedural, declarative,
imperative,
8The programming language C
- Many syntax elements used in a variety of
programming languages (C, Java, C, javascript,
PHP, ) today. - Main application area today System software.
- Especially Linux, Unix
- Compromise between assembly code and
compatibility. - Lots of legacy code.
- Overview
- http//en.wikipedia.org/wiki/C_programming_langua
ge
9History of C
http//cm.bell-labs.com/cm/cs/who/dmr/chist.html
- before 1960 CPL (Combined Programming Language)
developed in Cambridge and London. - 1966 Martin Richards designed BCPL, a simpler
version of CPL. - 1969-70 Ken Thompson created B, a simple form of
BCPL - 1971-1973 Dennis Ritchie develops C from B by
adding types. DRs Homepage http//www.cs.bell-l
abs.com/who/dmr/ - 1978 Kernighan and Ritchie established a de facto
standard by writing a book on C - 1989 Ansi C becomes the C standard.
10The really early history of CBCPL still alive
- Martin Richards Homepage
- http//www.cl.cam.ac.uk/users/mr/index.html
- BCPL uses a virtual machine approach
- http//en.wikipedia.org/wiki/BCPL
- Different in C (direct compilation)
- But similar strategy as in Java!
- A BCPL distribution
- http//www.cl.cam.ac.uk/users/mr/BCPL.html
- MRs lecture comparative languages (Cambridge,
2003) http//www.cl.cam.ac.uk/Teaching/2003/CompP
rogLangs/
11BCPL OCODE (virtual machines)
- From Martin Richards The BCPL Cintcode System
Users Guide (draft), August 2001,
http//www.cl.cam.ac.uk/users/mr/index.html
12Design principles in C
- Pragmatic
- close to the machine
- Efficient
- Use libraries for all system relevant tasks (I/O,
memory management, ) - Preprocessor (include, define, )
13Example A C program
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
14Example A C programThe entry point (main)
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- The entry point of each C program is the main()
function. - It does not need to have arguments (cf. Java)
- The return value of main() must be int and will
be passed back to the operating system.
15Example A C programBlocks and Statements
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- Curly brackets , are used to structure the
code. - Code inside a pair of and is called a block.
- Semicolons are used to separate statements.
- This is still the basis of a number of modern
programming languages.
16Example A C programLibraries Header files
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- The include statement is processed by the C
preprocessor. - Its meaning is simple Copy the contents of the
designated file (here stdio.h) in this place
before compilation. - The consequence is that we can use other
functions written by somebody else. - These header files provide declarations of
functions that are implemented in libraries. - stdio.h refers to the header file of the standard
I/O library.
17Example A C programprintf, sprintf, I/O
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- printf and sprintf are implemented in the
standard I/O library.
18Example A C programCompilation
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- So, the compilation of a C program consists of
three steps - Run the preprocessor.
- Locally compile the resulting file.
- Link the compiled file together with the
libraries.
19Example A C programComments
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- / and / indicate the start and end of a
comment. - The // syntax for comments existed in BCPL and
has been reintroduced in C and Java.
20Example A C programFunctions, Procedures
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- A C function. The function has no return value
(void), so it is in fact a procedure. - The argument of the function is a pointer to a
character.
21Example A C programArrays and Pointer
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- An array in C is identified with a pointer.
- So name contains a memory address, and the
following 10 memory cells are reserved by the
system for the lifetime of the variable name. - This allows a very efficient handling of arrays.
- On the other hand it is nearly impossible to
check for boundaries or memory integrity.
22Example A C programMemory management (there
isnt any)
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- sprintf writes the contents of the string
constant Hi Folks! in the memory locations
following the memory address where name points
to. - It does not know if there is enough memory
reserved, or if the memory location is valid.
23Example A C programStrings (arrays of
characters)
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- Hi Folks! is stored as H, i, , F,
o, l, k, s, !, \0where the last
character (\0) indicates the end of a string. - If the \0 were missing sprintf would copy
character after character without stopping (and
messing up the contents of the memory cells after
name).
24Example A C programPointer, Strings - Problems
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- Similarly fiveReverse will print the contents
of the next five memory cells where str points
to, independent if this makes sense or not. - It is not checked if str has been properly
initialised.
25Example A C programComplicated Statements
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- This code is equivalent to
- while(i ! 0)
- i i - 1
-
-
- i i - 1
- The C while(i--) code is closer to machine
language - Branch if zero
- Decrement register
- Today this is largely irrelevant because
compilers are better in optimising code!
Todays philosophy Produce human readable code!
26Example A C programPointer arithmetic
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- Pointer arithmetic
- An integer can be added to a pointer value,
- str i means a pointer to a memory cell i places
away. - A star stands for the contents of this memory
cell.
27Example A C programFormatting Output
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- The first argument of the printf function is a
format string - d print an integer
- c print a character
- Also x, e, f, g, s, ,
- See http//www.die.net/doc/linux/man/man3/printf.
3.html - Escape sequences for special characters
- \n print a new line (Also \b, \a, \, )
28Example A C programC and Java (Syntax)
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- Compare with Java
- Some elements are familiar (main, , , void,
int, char, - Functions are similar as Java methods.
- There are no classes.
- Arrays are similar declared, but internally they
are represented differently. - C libraries contain functions. What does a Java
package provide? - C has pointer variables. Has Java something
similar? - C has an include statement. How is this related
to a Java import statement?
29Example A C programQuestions
- include ltstdio.hgt
- / This function prints the first five
- characters in reverse order /
- void fiveReverse(char str)
- int i5
- while( i-- )
- printf("d c\n", i, (stri) )
-
-
- int main()
- char name10
- sprintf(name, "Hi Folks!")
- fiveReverse(name)
- return 0
- What happens when
- you replace 5 by 20?
- replace 5 by 1?
- replace 10 by 3?
- write char name instead of char name3?
- simply write Hi as input instead of Hi
Folks!? - Or write Hello World!?
30Structures a first glimpse of Object
Orientation in C
- A structure groups variables of different types
together. - The typedef keyword defines a new data type
(here Person). - We say A person has the attributes yearOfBirth
and lastName20. - Or The state of a Person is defined by its year
of Birth and the last name.
- include ltstdio.hgt
- typedef struct
- int yearOfBirth
- char lastName20
- Person
- int main()
- Person fabian
- int thisYear 2004
- fabian.yearOfBirth 1998
- sprintf(fabian.lastName, "Conrad")
- printf("Fabian s is d years old.\n",
- fabian.lastName,
- thisYear - fabian.yearOfBirth )
- return 0
31Structures a first glimpse of Object
Orientation in C
- This new data type Person can be used in the same
way as a primitive data type (as int, char, ). - A variable of type Person is declared.
- The attributes are accessed using a dot (.)
between the name of the variable and the name of
the attribute.
- include ltstdio.hgt
- typedef struct
- int yearOfBirth
- char lastName20
- Person
- int main()
- Person fabian
- int thisYear 2004
- fabian.yearOfBirth 1998
- sprintf(fabian.lastName, "Conrad")
- printf("Fabian s is d years old.\n",
- fabian.lastName,
- thisYear - fabian.yearOfBirth )
- return 0
32Structures a first glimpse of Object
Orientation in C
- In this example
- Person is a very simple form of a class.
- fabian is a very simple form of an object.
- Here, the class Person and the object fabian have
only state but they do not have behaviour.
- include ltstdio.hgt
- typedef struct
- int yearOfBirth
- char lastName20
- Person
- int main()
- Person fabian
- int thisYear 2004
- fabian.yearOfBirth 1998
- sprintf(fabian.lastName, "Conrad")
- printf("Fabian s is d years old.\n",
- fabian.lastName,
- thisYear - fabian.yearOfBirth )
- return 0
Object Orientation State Behaviour