CS255 PROGRAMMING LANGUAGES HARISH TIWARI SCHOOL OF ENGINEERING

  • Slides: 10
Download presentation
CS-255 PROGRAMMING LANGUAGES HARISH TIWARI, SCHOOL OF ENGINEERING, SIR PADAMPAT SINGHANIA UNIVERSITY, UDAIPUR

CS-255 PROGRAMMING LANGUAGES HARISH TIWARI, SCHOOL OF ENGINEERING, SIR PADAMPAT SINGHANIA UNIVERSITY, UDAIPUR

11. Imperative Programming § Introduction § Key Concepts § Pragmatics § Case Study: C

11. Imperative Programming § Introduction § Key Concepts § Pragmatics § Case Study: C Language § Case Study: Ada 6 -2

11. 4 Case Study: C Language § Introduction to C language § Values and

11. 4 Case Study: C Language § Introduction to C language § Values and Types § Variables, storage, and control § Bindings and scope. § Procedural abstraction. § Independent compilation. § Preprocessor directives. § Function library 6 -3

1. C Language Introduction. § C was originally designed in the early 1970 s

1. C Language Introduction. § C was originally designed in the early 1970 s by Dennis Ritchie, primarily for writing the UNIX operating system and its associated compilers and utilities. § An internationally-recognized standard was developed in the 1980 s. § C is characterized by the presence of low-level operations and weak type checking. § It also lacks modern concepts such as data abstraction, generic abstraction, or exceptions. § An expert C programmer can use it to write very efficient systems software. 6 -4 § it is an ancestor of two other popular languages (C++ and JAVA).

2. Values and Types. § C has a limited primitive types: • Enumeration types.

2. Values and Types. § C has a limited primitive types: • Enumeration types. • integer types (with various ranges, both signed and unsigned). • Floating point types (with single- or double-precision). § There is no specific Boolean type • false and true are conventionally represented by zero and nonzero integers. § There is also no specific character type • characters are conventionally represented by values of type char but, despite the type’s name, these values are just small integers. § C has composite types • Array types, Structure types, and Union types. 6 -5

Values and Types(2) § C does not support recursive types directly. Instead, programmers must

Values and Types(2) § C does not support recursive types directly. Instead, programmers must declare recursive types using pointers. § Only primitive values, pointers, and structures are first-class values • They may be assigned, passed as arguments, and returned as function results. § Arrays are not first-class values • only pointers to arrays can be assigned, passed as arguments, and returned as function results. § Similarly, strings (which are represented by character arrays) are not first-class values. 6 -6

Values and Types(3) § C assignment ‘‘V = E’’ is actually an expression, yielding

Values and Types(3) § C assignment ‘‘V = E’’ is actually an expression, yielding the value of E as well as assigning that value to the variable V. § C allows ordinary binary operators to be combined with assignment. • if ⊗ is a binary operator, then ‘‘V⊗=E’’ is a concise alternative to ‘‘V = V ⊗ E’’. § C provides pre- and post-increment/ decrement operators named. 6 -7

Values and Types(4) § C’s type system is extremely weak. • A pointer can

Values and Types(4) § C’s type system is extremely weak. • A pointer can be cast to an integer. • An integer can be cast to a pointer. • A pointer of one type can be cast to a different pointer type. • Another serious weakness is that certain function calls are not typechecked. § If p is a pointer, ‘‘*p’’ denotes the variable to which p points. § If p is a pointer to a structure or union variable that has a field named f, we can access that field using the notation ‘‘ • p->f - which is just an abbreviation for (*p). f. 6 -8

3. Variable, Storage and Control § C supports global and local variables. It also

3. Variable, Storage and Control § C supports global and local variables. It also supports heap variables in a low-level manner. § C has some of the characteristics of an expression language. • An assignment is an expression. • A function call is also an expression. § C’s commands includes • a skip command. • sequential commands. • conditional commands (if- and switch commands). • iterative commands (while-, do-while-, and for-commands) • block commands. 6 -9

Variable, Storage and Control(2) § C’s break, continue, and return sequencers allow single-entry multi-exit

Variable, Storage and Control(2) § C’s break, continue, and return sequencers allow single-entry multi-exit control flows to be programmed as easily as singleentry single-exit control flows. § C also supports jumps (goto statement). § C’s for-command is actually syntactic shorthand for a whilecommand, conveniently gathering the loop initialization code (C 1), loop condition (E 1), and loop continuation code (E 2) together in one place: for (C 1; E 2) C 2 ≡ C 1 while (E 1) { C 2 E 2; 6 -10