Top Banner

of 33

softp-lec01-2012b

Apr 06, 2018

Download

Documents

Sagie Maoz
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/2/2019 softp-lec01-2012b

    1/33

    Software ProjectInstructor: Roded Sharan, [email protected]: Dana Silverbush

    Course materials: virtual.tau.ac.il

    http://www.cs.tau.ac.il/~roded/courses/soft-project12b.html

    A Book on C - ABCKelley/Pohl

    Addison-WesleyFourth Edition

  • 8/2/2019 softp-lec01-2012b

    2/33

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

  • 8/2/2019 softp-lec01-2012b

    3/33

    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

  • 8/2/2019 softp-lec01-2012b

    4/33

    Why C ?

    C is compact: few reserved words, tersecommands, 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 ofUNIX.

    Basis of C++ (and Java).

  • 8/2/2019 softp-lec01-2012b

    5/33

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

  • 8/2/2019 softp-lec01-2012b

    6/33

    C vs. Java

    Java was developed based on C and inherited

    most of its syntax.

    Main difference: C is procedural and not object-oriented. Thus, no classes/interfaces; methods are

    called functions and do not belong to any object.

    Similar primitive types as in Java, but no booleanand string types; other differences.

  • 8/2/2019 softp-lec01-2012b

    7/33

    C vs. Java (cont.)

    Pointers instead of references. Allow direct access to

    memory and dereferencing (but less elegant).

    No garbage collection need to manage memoryexplicitly.

    Preprocessor.

    Compilation to machine code faster but some

    platform dependency; java is processed via an

    interpreter (JVM).

  • 8/2/2019 softp-lec01-2012b

    8/33

    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)

  • 8/2/2019 softp-lec01-2012b

    9/33

    Programming Environment

    Unix

    Make

    CPLEX

  • 8/2/2019 softp-lec01-2012b

    10/33

    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.

  • 8/2/2019 softp-lec01-2012b

    11/33

    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.

  • 8/2/2019 softp-lec01-2012b

    12/33

    Project

    Task: Clustering via integer linear programming (ILP)

    Goals:

    Large scale programming

    Efficient computation

    Real world application (biological networks) Interfacing an external library (CPLEX)

  • 8/2/2019 softp-lec01-2012b

    13/33

    Lexical Elements

    (chapter 2)

  • 8/2/2019 softp-lec01-2012b

    14/33

    The C System

    edit hello.c compile hello.o link hello

    C

    Standard

    library

    PreprocessorLexical analysis

    Syntax analysis

    Semantic analysis

    Keywords

    Identifiers

    Constants

    Operators

    Punctuation

  • 8/2/2019 softp-lec01-2012b

    15/33

    Keywords

    Reserved words with a strict meaning

    C does a lot with relatively few keywords.

    auto do goto signed unsigned break

    double if sizeof void case else

    int static volatile char enum long

    struct while const extern register switch

    continue float return typedef default for

    short union

  • 8/2/2019 softp-lec01-2012b

    16/33

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

  • 8/2/2019 softp-lec01-2012b

    17/33

    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++

  • 8/2/2019 softp-lec01-2012b

    18/33

    Fundamental

    data typesChapter 3 in ABC

  • 8/2/2019 softp-lec01-2012b

    19/33

    Integral (signed or unsigned)

    char

    short

    int

    longFloating point types

    float

    double

    Fundamental Data Types

  • 8/2/2019 softp-lec01-2012b

    20/33

    Integral Types:

    char signed char unsigned char

    short int long

    usigned short unsigned unsigned long

    Floating Types:

    float double long double

    Declaration is important for:

    Memory allocation

    Interpreting the number correctly within expressions.

    Fundamental Data Types

  • 8/2/2019 softp-lec01-2012b

    21/33

    Constants

    a, x, 0, &, \nchar

    1, 0, -54, 4234567int

    0l, 123l, 7766llong

    0u, 23u, 3000000000uunsigned

    1.2f, .2f, 1.f, 3.14159ffloat

    1.0, -3.1412, 1.2e-2double

    1.0l, 2.4433llong double

  • 8/2/2019 softp-lec01-2012b

    22/33

    character: 'a' 'b' 'c' ... 'z'

    integer value: 97 98 99 ... 112

    character: '0' '1' '2' ... '9'

    integer value: 48 49 50 ... 57

    character: '&' '*' '+'

    integer value: 38 42 43

    Some Character Constants

    and their Integer Values

    The character 0

    has a value of 48

  • 8/2/2019 softp-lec01-2012b

    23/33

  • 8/2/2019 softp-lec01-2012b

    24/33

    Some Character Constants

    and their Integer Values

    char c = 'a';

    printf("%c", c);

    printf("%d", c);

    printf("%c%c%c", c, c+1, c+2);

    a is printed

    97 is printed

    abc is printed

  • 8/2/2019 softp-lec01-2012b

    25/33

    Some Character Constants

    and their Integer Valuescharacter: 'A' 'B' 'C' ... 'Z'

    integer value: 65 66 67 ... 90

    char c = 0;

    int i = 0;

    for ( i = 'a'; i

  • 8/2/2019 softp-lec01-2012b

    26/33

    Representation

    Binary valuevalueType

    vvvv vvvvunsigned char

    0000 00000unsigned char

    1111 1111255unsigned char

    0111 1111127unsigned char

    1000 0000128unsigned char

    svvv vvvv(2-complement)signed char

    1111 1111-1signed char

    1000 0000-128signed char

    svvv vvvv vvvv vvvv vvvv vvvv vvvv vvvvsigned int

    v means valuemeans sign[uint-max.c]

  • 8/2/2019 softp-lec01-2012b

    27/33

    Decimal, Hexadecimal, Octal[conversions.c]

    #include

    int main(void)

    {

    printf( %d %x %o\n, 19, 19, 19 );printf( %d %x %o\n, 0x1c, 0x1c, 0x1c);

    printf( %d %x %o\n, 017, 017, 017);

    printf( %d\n, 11 + 0x11 + 011);

    printf( %x\n, 2097151);

    printf( %d\n, 0x1FfFFf);

    return 0;

    }

    19 13 23

    28 1c 34

    15 f 17

    37

    1fffff

    2097151

  • 8/2/2019 softp-lec01-2012b

    28/33

    Float Representation

    Decimal: int.frac*10exp

    Binary: 1.frac*2exp

    seee eeee sfff ffff ffff ffff ffff ffff

    What is the representation of 0.5 ?

    0.5+2100 ? [precision.c]

    exp+127

  • 8/2/2019 softp-lec01-2012b

    29/33

    Sizeof

    sizeof(char) == 1

    sizeof(short)

  • 8/2/2019 softp-lec01-2012b

    30/33

    compute the size of some

    fundamental types[size-of-type.c] on nova":

    The size of some fundamental types is computed.

    char: 1 byteshort: 2 bytes

    int: 4 bytes

    long: 8 bytes

    unsigned: 4 bytes

    float: 4 bytes

    double: 8 bytes

    long double: 16 bytes

  • 8/2/2019 softp-lec01-2012b

    31/33

    The usual arithmetic conversion (promotion)If eitheroperand is of type long double, double, floatorunsigned long, the

    other operand isconvertedto long double, double, floatorunsigned longappropriately.

    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 longcan represent all the values of an unsigned, then the operand

    of type unsigned isconvertedto long.

    If a longcannot represent all the values of an unsigned, thenboth

    operands areconvertedto unsigned long.

    Otherwise, if either operand is of type long, the other operand isconverted

    to long.

    Otherwise, if either operand is of type unsigned, the other operand is

    convertedto unsigned.

    Otherwise, both operands have type int.

  • 8/2/2019 softp-lec01-2012b

    32/33

    Expressions and Types

    char c; short s; int i; unsigned u;

    unsigned long ul; float f; double d; long double ld;

    Expression Type Expression Type

    c s / i int u * 7 i unsigned

    u * 2.0 i double f * 7 i float

    c + 3 int 7 * s * ul unsigned long

    c + 5.0 double ld + c long double

    d + s double u ul unsigned long

    2 * i / l long u - l system

    dependent

  • 8/2/2019 softp-lec01-2012b

    33/33

    Cast

    double d = 3.3;

    int i = 0;

    unsigned ui = 0;

    i = (int)d;

    d = (double)i / 2;

    ui = (unsigned)i;

    i == 3d == 1.5