working environment - ariel.ac.il ?· 2 working environment • linux, gcc • we’ll work with...

Download Working environment - ariel.ac.il ?· 2 Working environment • Linux, gcc • We’ll work with c9.io…

Post on 29-Aug-2019

212 views

Category:

Documents

0 download

Embed Size (px)

TRANSCRIPT

  • C

    #include int main() {

    printf ("hello class\n"); return 0;

    }

  • 2

    Working environment

    • Linux, gcc

    • We’ll work with c9.io website, which works with

    ubuntu

    • I recommend to install ubuntu too

    • Also in tirgul

  • History

    3

    70’s – Development of UNIX. (Richie+Kernigham – Bell labs)

    80’s – Large efficient code. (Stroustrup – Bell labs)

    90’s – Language for the web. (Sun Microsystems)

    Simple to convert to

    machine code. VM

    Fast and Efficient Secure and Safe

    Welcome to C Easy to avoid bugs

    Easy to Debug

    C++C Java

  • History – C:

    K&R 1st

    ed. ('78) ANSI C

    ('89) C99 ('99)

    C11(’11)

    This course, without the VLA!

    • First "standard"

    • Default int

    •Enums

    •Return struct

    •void

    • //

    • VLA

    • variadic

    macros

    •inlining

    •multithreading

    •Anonymous

    structs

    • _Generic

  • 5

    C – Design Decisions

    “Bare bones” – the language leaves maximal flexibility

    with the programmer

    Efficient code (operating systems)

    Full control on memory & CPU usage

    High-level

    Type checking

    High-level constructs

    Portable

    Standard language definition

    Standard library

  • C – Warning Signs

    6

    No run-time checks

    Array boundary overruns

     Illegal pointers

    No memory management

    Programmer has to manage memory

  • First Program in C

    7

    // This line is a comment,

    /* and this line also. */

    // This line defines standard I/O library

    #include

    // main – program entry point. Start form here

    int main()

    { // {…} define a block

    printf("Hello class!\n");

    return 0;

    }

    7

  • Similar to java – you should know it already.

    8

    The basic syntax

  • Basic

    9

    • Case sensitive

    • White-space not important.

    • End statements with “;”

    • Code block defined with “{” and “}”

    • Return value from function using “return X;”

    • String with “ " ”

    • All lines below are legal:

    int x,y;

    x=5; {

    x; } ;

  • Statements - conditional

    10

    if (expression)

    //statement or block

    else if (expression)

    //statement or block

    else (expression)

    //statement or block

    switch (integer value)

    later...

  • Statements - loops

    11

    The usual suspects:

    int x,y; //in ANSI C you cannot declare inside

    the for!

    for (x=0,y=0;x

  • Statically typed – each variable has a type. Declare by:

    .

    int x;

    int x,y;

    Optionally initialize (otherwise undefined!)

    int x=0;

    Variables

    12

  • int x;

    Undefined value: as in this example, can have any T (here T=int) value.

    Undefined behavior: Everything may happen. Probably

    won’t format your disk, but may very well give a hacker a

    way to do it…

    The biggest problem: often programs seems to work

    fine although their behavior is undefined, and then…

    Undefined – important!

    13

  • Variables

    14

    Where to declare?

    1. Inside a block (C89 - block beginning) , will be visible only

    in block.

    2. Outside all blocks – global - will be visible everywhere.

    int x=0; // global int main()

    { int x=1; //local hides global

    { int x=2; //local hides outer scope //x is 2

    } //x is 1 again!

    }

  • Scopes

    15

    • Code block defined with “{” and “}”.

    • Only declarations inside current or outer scopes are visible.

    • Declarations in inner scope hide declarations in outer

    scopes:

    • Outmost scope (global) had no brackets.

    • Keep in mind that a function is also a scope.

    int y=5,x=0; {

    int x=y; //x is 5 {

    int y; }

    } // x is 0

  • Second Program

    16

    #include int main() {

    int i; // declares i as an integer int j = 0; // declares j as an integer,

    // and initializes it to 0 // for( initial ; test condition ; update step ) for( i = 0; i < 10; ++i ) {

    j += i; // shorthand for j = j + i printf("%d %d %d\n", i, j, (i*(i+1))/2);

    } return 0;

    }

    16

  • Running…

    17

    0 0 0

    1 1 1

    2 3 3

    3 6 6

    4 10 10

    5 15 15

    6 21 21

    7 28 28

    8 36 36

    9 45 45

    17

  • Functions

    18

    C allows to define functions

    Syntax:

    int power( int a, int b )

    {

    return 7;

    }

    Return type Parameter

    declaration

    Return

    statement

    18

  • Procedures

    19

    Functions that return void

    void power( int a, int b )

    {

    return;

    }

    Return w/o value

    (optional)

    19

  • Example – printing powers

    20

    #include

    int power( int base, int n )

    {

    int i, p;

    p = 1;

    for( i = 0; i < n; i++ )

    {

    p = p * base;

    }

    return p;

    }

    int main()

    {

    int i;

    for( i = 0; i < 10; i++ )

    {

    printf("%d %d %d\n",

    i,

    power(2,i),

    power(-3,i) );

    }

    return 0;

    } 20

  • Functions Declaration

    21

    “Rule 1”: A function

    “knows” only

    functions which were

    declared above it.

    void funcA()

    {

    ...

    }

    void funcB()

    {

    funcA();

    }

    void funcC()

    {

    funcB();

    funcA();

    funcB();

    }

    void funcA()

    {

    ...

    }

    void funcB()

    {

    funcC();

    }

    void funcC()

    {

    funcB();

    }

    Error:

    funcC is not

    known yet.

    21

  • Functions Declaration

    void funcC(int param);

    // or: void funcC(int);

    void funcA()

    {

    }

    void funcB()

    {

    funcC(7);

    }

    void funcC(int param)

    {

    }

    Amendment to “Rule 1” : Use forward declarations.

    22

  • Boolean variables – non!

    23

    int main()

    {

    int a = 5;

    while(1)

    {

    if(!(a-3))

    {

    printf("** a=3 **\n");

    break;

    }

    printf("a=%d\n",a--);

    }

    return 0;

    }

  • 24

    Building a program in C:

    Preprocessor, Compilation and Linkage

  • Building a program in C – Preprocessor

    25

    A text processor

    Commands start with #

    // copy & paste the file here

    #include

    hello.c

    Preprocessor

    stdio.h

    tmpXQ.i

    (C code)

  • Building a program in C – Compiling

    26

    Takes input C-code and produces machine code (object

    file)

    The object file does not contain all external references: It

    leaves names, such as “printf”, “area”, etc. as undefined

    references

    hello.c

    Preprocessor Compiler

    stdio.h

    tmpXQ.i

    (C code)

    hello.o

    (object

    file)

  • Linking

    27

    Combines object file with external references into an

    fully executable file, with no unresolved references

    Main

    Preprocessor,

    Compiler

    Main.c Main.o

    Linker

    libc.a

  • Link errors

    28

    Gcc: /media/sf_BitEagle_Projects/cbitcoin/test/testC BAddress.c:40: undefined reference to `CBNewByteArrayFromString‘

    Visual studio: Error 1 error LNK2019: unresolved external symbol _foo referenced in function _main

    c:\Users\ofirpele\documents\visual studio 2012\Projects\ConsoleApplication5\ConsoleAp plication5\ConsoleApplication5.obj

    ConsoleApplication5

  • 29

    The Preprocessor

  • Compilation in C

    30

    hello.c

    Preprocessor Compiler

    stdio.h

    tmpXQ.i

    (C code)

    hello.o

    (object

    file)

  • Preprocessor

    31

    A single-pass program that:

    1. Include header files

    2. Expands macros

    3. Control conditional compilation

    4. Remove comments

    Outputs – a code ready for the

    compiler to work on.

  • Preprocessor

    32

    In linux with gcc, we can test what

    the preprocessor does:

    > gcc –E hello.c

    will print the C code after running the

    preprocessing

  • #include directive

    33