Software Project

Instructor: Roded Sharan, roded@post.tau.ac.il TA: Dana Silverbush Course materials: virtual.tau.ac.il
http://www.cs.tau.ac.il/~roded/courses/soft-project12b.html

A Book on C - ABC Kelley/Pohl Addison-Wesley Fourth Edition

Course Structure
• The C programming language: 8-9 classes including 2 exercises (5%+10%; mandatory) and a final exam (30%). • Large project (55%): in pairs, 2 classes will be devoted to its presentation; includes interfacing an integer linear programming library (CPLEX).

The C programming language
• B, initial versions of UNIX, 1970 • C, Deniss Ritchie, Bell Labs, 1972; overcomes B’s typeless representation; used for developing UNIX. • Early 80’s - traditional C • 1990 – the ANSI-C standard • C++ and Java

Why C ? • C is compact: few reserved words. • C is the native language of UNIX. • C is efficient: direct memory management. • Basis of C++ (and Java). . powerful set of operators. • C is modular and typed. manipulation of memory addresses. terse commands. some operators directly modify machine registers.

q.} .n=10.*a=" main() {char q=34.n). pr intf(a.q.C is also beautiful main(){char q=34.*a=% c% s% c.n).q.pr intf(a.a.}% c" .a.n=10.q.

• Similar primitive types as in Java.C vs. no classes/interfaces. Thus. . methods are called functions and do not belong to any object. Java • Java was developed based on C and inherited most of its syntax. • Main difference: C is procedural and not objectoriented. other differences. but no boolean and string types.

java is processed via an interpreter (JVM). Java (cont. • Preprocessor. . • Compilation to machine code – faster but some platform dependency. Allow direct access to memory and dereferencing (but less elegant).) • Pointers instead of references.C vs. • No garbage collection – need to manage memory explicitly.

10) Input/output and files (Ch. 4) Functions (Ch. 12. 5). 11) .6) Bitwise operators (Ch. Runtime environment Arrays. Dynamic matrix allocation (Ch. 2) Fundamental data types (Ch. pointers and strings (Ch. 8) Structures (Ch. 3) Flow of control (Ch.C Topics • • • • • • • • • • Lexical elements (Ch. 9) Linked lists (Ch. 7) Preprocessor (Ch. 6).

Programming Environment • Unix • Make • CPLEX .

Users → AUI → API → OS kernel → Hardware Shell: command-line interpreter to interface the OS. memory…) and software (editors. development tools…) resources. • Provides interface for using them by multiple users.Operating System Operating system: • Manages the available hardware (CPU. .

.The Unix OS Unix: multi-user. multi-process OS. open source. • 70’s – Rewritten in C. For basic introduction and commands see web-page. History: • 1969 – Initial version by Thompson & Ritchie at Bell Labs (assembly and later B). • 80’s – System-V (AT&T) and BSD (Berkeley) versions. • 90’s – Linux by Linus Torvalds.

Project Task: Clustering via integer linear programming (ILP) Goals: • • • • Large scale programming Efficient computation Real world application (biological networks) Interfacing an external library (CPLEX) .

Lexical Elements (chapter 2) .

The C System Keywords Identifiers Constants Operators Punctuation C Standard library edit hello.o link hello Preprocessor Lexical analysis Syntax analysis Semantic analysis .c compile hello.

Keywords Reserved words with a strict meaning C does a lot with relatively few keywords. auto double int struct continue short do if static while float union goto sizeof volatile const return signed void char extern typedef unsigned case enum register default break else long switch for .

Identifiers A token composed of a sequence of letters tax = price * tax_rate • First character cannot be a digit!! 5a_cse is not legal • Case sensitive CSE_5a is different from cse_5a • Some identifiers are already taken: keywords. • Choose names that are meaningful (cse_5a is not a good example!!) .

/* * A comment can be written in this fashion * to set it off from the surrounding code. */ For single line comments (do not follow the ANSI standard): // This is a comment in c++ .Comments Arbitrary strings placed between the delimiters /* and */.

Fundamental data types Chapter 3 in ABC .

Fundamental Data Types Integral (signed or unsigned) char short int long Floating point types float double .

.Fundamental Data Types Integral Types: char short usigned short Floating Types: float signed char int unsigned unsigned char long unsigned long double long double Declaration is important for: • Memory allocation • Interpreting the number correctly within expressions.

1. -54.2f. 1. 3. ‘\n’ 1.2e-2 1.4433l .1412.Constants char int long unsigned float double long double ‘a’. –2. 3000000000u 1. . ‘0’. 123l. 23u. ‘&’. –7766l 0u.0l.2f.14159f 1. ‘x’. 0.0. 4234567 0l.f. -3.

....... .. . .Some Character Constants and their Integer Values character: integer value: character: integer value: character: integer value: 'a' 97 '0' 48 '&' 38 'b' 98 '1' 49 '*' 42 'c' 99 '2' 50 '+' 43 . 'z' 112 '9' 57 The character ‘0‘ has a value of 48 .

Some Character Constants and their Integer Values Name of Character alert backslash horizontal tab newline null character quote Written in C ‘\a’ ‘\\’ ‘\t’ ‘\n’ ‘\0’ ‘\” Integer Value 7 92 9 10 0 39 The character ‘\0‘ has a value of 0 .

c).Some Character Constants and their Integer Values char c = 'a'. c. c+1. a is printed printf("%c". . c). 97 is printed printf("%d". c+2). “abc“ is printed printf("%c%c%c".

i <= 'z'... for ( c = '0'. Z“ is printed “48 49 50 .. i ). c <= '9'.. int i = 0.. z“ is printed “ABC .. ++i ) printf( “%c”. for ( c = 65. 'Z' 90 “abc . ..Some Character Constants and their Integer Values character: integer value: char c = 0. c <= 90. for ( i = 'a'. ++c ) printf( “%c”.. ++c ) printf( “%d”. c ). 'A' 65 'B' 66 'C' 67 .. 57“ is printed . c )..

Representation Type unsigned char unsigned char unsigned char unsigned char unsigned char signed char signed char signed char signed int 0 255 127 128 (2-complement) -1 -128 value Binary value vvvv vvvv 0000 1111 0111 1000 svvv 1111 1000 svvv vvvv vvvv vvvv 0000 1111 1111 0000 vvvv 1111 0000 vvvv vvvv vvvv vvvv s means sign [uint-max.c] v means value .

Octal [conversions. 0x1c.h> int main(void) 19 13 23 { printf( “%d %x %o\n”. } 2097151 . 15 f 17 printf( “%d\n”. 37 printf( “%x\n”. 1fffff return 0. 2097151). 28 1c 34 printf( “%d %x %o\n”. printf( “%d %x %o\n”. 017). 017.Decimal. 0x1FfFFf). printf( “%d\n”. 0x1c. 19. Hexadecimal. 017. 19 ).c] #include <stdio. 19. 0x1c). 11 + 0x11 + 011).

frac*2exp seee eeee sfff ffff ffff ffff ffff ffff exp+127 What is the representation of 0.5 ? 0.c] .frac*10exp 1.5+2100 ? [precision.Float Representation Decimal: Binary: int.

Sizeof sizeof(char) == 1 sizeof(short) <= sizeof(int) <= sizeof(long) sizeof(signed) == sizeof(unsigned) == sizeof(int) sizeof(float) <= sizeof(double) <= sizeof(long double) .

compute the size of some fundamental types [size-of-type. char: short: int: long: unsigned: float: double: long double: 1 byte 2 bytes 4 bytes 8 bytes 4 bytes 4 bytes 8 bytes 16 bytes .c] on “nova": The size of some fundamental types is computed.

and the following rules are applied: • If one operand has type long and the other operand has type unsigned then one of two possibilities occurs: – If a long can represent all the values of an unsigned. .The usual arithmetic conversion (promotion) If either operand is of type long double. the other operand is converted to long. Otherwise. • Otherwise. • Otherwise. • Otherwise. float or unsigned long appropriately. if either operand is of type long. then the operand of type unsigned is converted to long. – If a long cannot represent all the values of an unsigned. if either operand is of type unsigned. double. the "integral promotions" are performed on both operands (char/short => int). the other operand is converted to long double. the other operand is converted to unsigned. float or unsigned long. then both operands are converted to unsigned long. double. both operands have type int.

Expressions and Types char c. unsigned long ul. double d. float f.0 – i c+3 c + 5. long double ld. int i. unsigned u. short s. Expression c–s/i u * 2.0 d+s 2*i/l Type int double int double double long Expression u*7–i f*7–i 7 * s * ul ld + c u – ul u-l Type unsigned float unsigned long long double unsigned long system dependent .

int i = 0. unsigned ui = 0. .Cast double d = 3. d == 1. i == 3 i = (int)d.3.5 d = (double)i / 2. ui = (unsigned)i.

Sign up to vote on this title
UsefulNot useful