Vous êtes sur la page 1sur 33

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 Bs typeless representation; used for developing UNIX. Early 80s - traditional C 1990 the ANSI-C standard C++ and Java

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

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

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

C vs. Java (cont.)


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

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

Programming Environment
Unix Make CPLEX

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

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

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.c

compile

hello.o

link

hello

Preprocessor Lexical analysis Syntax analysis Semantic analysis

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!!)

Comments
Arbitrary strings placed between the delimiters /* and */. /* * 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++

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.

Constants
char int long unsigned float double long double a, x, 0, &, \n 1, 0, -54, 4234567 0l, 123l, 7766l 0u, 23u, 3000000000u 1.2f, .2f, 1.f, 3.14159f 1.0, -3.1412, 1.2e-2 1.0l, 2.4433l

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

Some Character Constants and their Integer Values


char c = 'a'; a is printed printf("%c", c); 97 is printed printf("%d", c); abc is printed printf("%c%c%c", c, c+1, c+2);

Some Character Constants and their Integer Values


character: integer value: char c = 0; int i = 0; for ( i = 'a'; i <= 'z'; ++i ) printf( %c, i ); for ( c = 65; c <= 90; ++c ) printf( %c, c ); for ( c = '0'; c <= '9'; ++c ) printf( %d, c ); 'A' 65 'B' 66 'C' 67 ... ... 'Z' 90

abc ... z is printed

ABC ... Z is printed

48 49 50 ... 57 is printed

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

Decimal, Hexadecimal, Octal


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

Float Representation
Decimal: Binary: int.frac*10exp 1.frac*2exp

seee eeee sfff ffff ffff ffff ffff ffff

exp+127

What is the representation of 0.5 ? 0.5+2100 ? [precision.c]

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.c] on nova": The size of some fundamental types is computed. 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

The usual arithmetic conversion (promotion)


If either operand is of type long double, double, float or unsigned long, the other operand is converted to long double, double, float or unsigned long appropriately. Otherwise, the "integral promotions" are performed on both operands (char/short => int), 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, then the operand of type unsigned is converted to long. If a long cannot represent all the values of an unsigned, then both operands are converted to unsigned long. Otherwise, if either operand is of type long, the other operand is converted to long. Otherwise, if either operand is of type unsigned, the other operand is converted to unsigned. Otherwise, both operands have type int.

Expressions and Types


char c; unsigned long ul; short s; float f; int i; double d; unsigned u; long double ld;

Expression cs/i u * 2.0 i c+3 c + 5.0 d+s 2*i/l

Type int double int double double long

Expression u*7i f*7i 7 * s * ul ld + c u ul u-l

Type unsigned float unsigned long long double unsigned long system dependent

Cast
double d = 3.3; int i = 0; unsigned ui = 0; i == 3 i = (int)d; d == 1.5 d = (double)i / 2; ui = (unsigned)i;

Vous aimerez peut-être aussi