7. introduction to c

Upload: pianofreak88

Post on 09-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 7. Introduction to C

    1/28

    Introduction to CIntroduction to C

    Systems Programming

    WS 2010

  • 8/8/2019 7. Introduction to C

    2/28

    Systems Programming WS 2010 Introduction to C (2)

    Outline

    Language history

    Brief comparison to Assembly and Java

    The process of language translation

    Separate compilation

    Hello world

  • 8/8/2019 7. Introduction to C

    3/28

    Systems Programming WS 2010 Introduction to C (3)

    History of the C language (1/3)

    Developed at Bell Laboratories in 1972 by Dennis Ritchie

    Many of its principles and ideas were taken from the earlier language B and B'searlier ancestors BCPL and CPL.

    CPL ( Combined Programming Language ) supported both high level,

    machine independent programming and control over the behavior ofindividual bits of information but was too large for use in manyapplications.

    In 1967, BCPL ( Basic CPL ) was created as a scaled down version of CPL

    In 1970, Ken Thompson, while working at Bell Labs, developed the Blanguage, a scaled down version of BCPL written specifically for use in

    systems programming.

    Finally in 1972, a co-worker of Ken Thompson, Dennis Ritchie, returnedsome of the generality found in BCPL to the B language in the process of

    developing the language we now know as C.

  • 8/8/2019 7. Introduction to C

    4/28

    Systems Programming WS 2010 Introduction to C (4)

    History of the C language (2/3)

    C's power and flexibility soon became apparent. Because of this, the Unix

    operating system which was originally written in assembly language, wasalmost immediately re-written in C

    Unix was one of the first operating systems not written in assembly

    During the rest of the 1970's, C spread throughout many colleges anduniversities because of its close ties to Unix and the availability of Ccompilers.

    In 1978, Dennis Ritchie and Brian Kernighan published the first edition of

    The C Programming Language. This book, known to C programmers as "K&R", served for many years as an

    informal specification of the language.

    The version of C that it describes is commonly referred to as "K&R C". The

    second edition of the book covers the later ANSI C standard. K&R introduced several new language features

    In the years following the publication of K&R C, several unofficial featureswere added to the language (since there was no standard), supported by

    compilers from AT&T and some other vendors.

  • 8/8/2019 7. Introduction to C

    5/28

    Systems Programming WS 2010 Introduction to C (5)

    History of the C language (3/3)

    In 1983, the American National Standards Institute (ANSI) formed a

    committee, X3J11, to establish a standard specification of C. In 1989, the standard was ratified as ANSI X3.159-1989 "Programming

    Language C." This version of the language is often referred to as ANSI C,Standard C, or sometimes C89.

    In 1990, the ANSI C standard (with a few minor modifications) was adoptedby the International Organization for Standardization (ISO) as ISO/IEC9899:1990.

    This version is sometimes called C90. Therefore, the terms "C89" and "C90"refer to essentially the same language.

    The latest standard to address the C language was ISO 9899:1999, issuedin 1999.

    This standard is commonly referred to as "C99." It was adopted as an ANSIstandard in March 2000.

    This lecture will only cover some of the features of C99, as compilers dont yethave full support for it

  • 8/8/2019 7. Introduction to C

    6/28

    Systems Programming WS 2010 Introduction to C (6)

    Outline

    Language history

    Brief comparison to Assembly and Java

    The process of language translation

    Separate compilation

    Hello world

  • 8/8/2019 7. Introduction to C

    7/28Systems Programming WS 2010 Introduction to C (7)

    C compared to Assembly

    One level of abstraction above assembly

    Language constructs that immensely facilitate structuredprogramming e.g., loops

    Automatic stack management Integrated heap management

    think of this as a sophisticated version of the memory allocator in the

    last assembly example but memory management still the responsibility of the programmer

    Strongly typed but with unchecked type conversions!

    Source-level portability between processor architectures andoperating systems

    The standard C Library and many other standards-compliant libraries (e.g., POSIX)

  • 8/8/2019 7. Introduction to C

    8/28Systems Programming WS 2010 Introduction to C (8)

    C compared to Java

    Both high-level and low-level language

    Better control of low-level mechanisms

    Performance better than Java

    But this is not a universal truth any more!

    Java hides many details needed for writing OS code

    But,. Not object oriented

    Memory management responsibility

    Explicit initialization and error detection More room for mistakes

    C exposes many details only needed for writing OS code

  • 8/8/2019 7. Introduction to C

    9/28Systems Programming WS 2010 Introduction to C (9)

    Outline

    Language history

    Brief comparison to Assembly and Java

    The process of language translation

    Separate compilation

    Hello world

  • 8/8/2019 7. Introduction to C

    10/28Systems Programming WS 2010 Introduction to C (10)

    The C Compiler

    A compiler translates source code directly into assembly

    language or machine instructions. The eventual end product is a file or files containing machine code.

    Some languages (such as C and C++) are designed to allowpieces of a program to be compiled independently.

    These pieces are eventually combined into a final executable program

    by a tool called the linker. This process is called separate compilation.

    Why is this a useful feature?

    Modern compilers can insert information about the sourcecode into the executable program.

    This information is called debug information and is used by source-level debuggers.

  • 8/8/2019 7. Introduction to C

    11/28Systems Programming WS 2010 Introduction to C (11)

    The compilation process (1/3)

    In C, compilation starts by running a preprocessor on the

    source code. The preprocessor is a simple program that replaces patterns in the

    source code with other patterns the programmer has defined (usingpreprocessor directives).

    These directives are used, among other things, to save typing, toincrease code readability, to control inclusion of header files, etc.

    The pre-processed code is often written to an intermediate file.

    Example (compare to assembly directives): constant declaration

    pre-processing compilation linking

  • 8/8/2019 7. Introduction to C

    12/28Systems Programming WS 2010 Introduction to C (12)

    The compilation process (2/3)

    Compilers usually do their work in two passes

    The first pass parses the pre-processed code into a parse tree. A global optimizer is sometimes used between the first and second

    passes to produce smaller, faster code.

    In the second pass, the code generator walks through the parse tree

    and generates either assembly code or machine code for the nodes ofthe tree.

    If the code generator creates assembly code, the assembler must thenbe run.

    The end result in both cases is an object module (a file that typicallyhas an extension of .o or .obj).

    A peephole optimizer is sometimes used in the second pass to look forpieces of code containing redundant assembly-language statements.

    pre-processing compilation linking

  • 8/8/2019 7. Introduction to C

    13/28Systems Programming WS 2010 Introduction to C (13)

    The compilation process (3/3)

    The linker combines a list of object modules into an

    executable program that can be loaded and run by the OS. When a function in one object module makes a reference to a function

    or variable in another object module, the linker resolves these

    references. The linker also makes sure that all the external functions and data

    you claimed existed during compilation do exist.

    Linker also adds a special object module to perform start-up activities

    The linker can search through special files called libraries inorder to resolve all its references.

    A library contains a collection of object modules in a single file.

    A library is created and maintained by a program called a librarian.

    pre-processing compilation linking

  • 8/8/2019 7. Introduction to C

    14/28Systems Programming WS 2010 Introduction to C (14)

    Compiled vs. Interpreted languages

    source code

    interpreter

    machine code

    execution

    source code

    compiler

    machine code

    execution

    CPU/Memory

    COMPILED INTERPRETEDStorage

  • 8/8/2019 7. Introduction to C

    15/28Systems Programming WS 2010 Introduction to C (15)

    The Java model for comparison

    virtual machine

    machine code

    execution

    source code

    compiler

    byte code

    CPU/Memory

    Storage

  • 8/8/2019 7. Introduction to C

    16/28Systems Programming WS 2010 Introduction to C (16)

    Outline

    Language history

    Brief comparison to Assembly and Java

    The process of language translation

    Separate compilation

    Hello world

  • 8/8/2019 7. Introduction to C

    17/28Systems Programming WS 2010 Introduction to C (17)

    Separate compilation

    Separate compilation important when building large projects.

    The most fundamental tool for breaking a program up into pieces is theability to create named subroutines or subprograms.

    In C, a subprogram is called a function, and functions are the atomicunits of code.

    To create a program with multiple files, functions in one file

    must access functions and data in other files. When compiling a file, the C compiler must know about the functionsand data in the other files, in particular their names and proper usage.

    The compiler ensures that functions and data are used correctly.

    This process of telling the compiler the names of external functionsand data and what they should look like is called declaration.

    Once you declare a function or variable, the compiler knows how tocheck to make sure it is used properly.

  • 8/8/2019 7. Introduction to C

    18/28Systems Programming WS 2010 Introduction to C (18)

    Declarations vs. definitions

    In C declarations and definitions are not the same thing.

    Declaration introduces a name (= identifier) to the compiler. Definition allocates storage for the name

    This meaning applies for both variables and functions.

    For a variable space is generated in memory to hold the data. For a function the compiler generates code, which ends up

    occupying storage in memory.

    You can declare a variable or a function in many differentplaces, but there must be only one definition per item in C(this is sometimes called the ODR: one-definition rule). This is checked when the linker is uniting all the object modules.

    A definition can also be a declaration. If the compiler hasntseen the name xbefore and you define int x;, the compilersees the name as a declaration and allocates storage for it all

    at once.

  • 8/8/2019 7. Introduction to C

    19/28Systems Programming WS 2010 Introduction to C (19)

    Function declarations

    A function declaration in C gives the function name, the

    argument types passed to the function, and the return value ofthe function:

    int func1(int,int);

    C declarations attempt to mimic the form of the items use.For example, if ais an integer the above function might be

    used this way:a = func1(2,3);

    Arguments in function declarations may have names. Thecompiler ignores the names but they can be helpful asmnemonic devices for the user:

    int func1(int length, int width);

  • 8/8/2019 7. Introduction to C

    20/28Systems Programming WS 2010 Introduction to C (20)

    Function definitions

    Function definitions look like function declarations except

    that they have bodies. A body, like in Java, is a collection ofstatements enclosed in braces:

    int func1(int length, int width) { }

    Notice

    in the function definition, the braces { and } replace the semicolon

    the arguments in the function definition must have names if you want touse the arguments in the function body

  • 8/8/2019 7. Introduction to C

    21/28Systems Programming WS 2010 Introduction to C (21)

    Variable declarations

    Variable declarations are not identical to function declarations.

    For example this is not a declaration:int a; /* Wrong! This is a definition */

    In the code above, there exists sufficient information for thecompiler to create space for an integer called a, and thatswhat happens.

    To resolve this problem, a keyword was necessary for C to sayThis is only a declaration; its defined elsewhere. Thekeyword is extern.

    Note that externcan mean that the definition is external to the file, orthat the definition occurs later in the file.

    externcan also be used for function declaration for consistency.

    Example of a proper variable declaration:extern int a; /* Correct! This is a declaration */

  • 8/8/2019 7. Introduction to C

    22/28Systems Programming WS 2010 Introduction to C (22)

    Including headers (1/2)

    Most libraries contain significant numbers of functions and

    variables. To save work and ensure consistency when makingthe external declarations for these items, C uses a devicecalled the header file.

    A header file is a file containing the external declarations for a library,and typically has the extension .h

    Library header files are provided along with the libraries themselves.

    To declare the functions and external variables in the library,the user simply includes the header file. To include a headerfile, use the #include preprocessor directive. This tells the preprocessor to open the named header file and insert its

    contents where the #include statement appears.

    #include may name a file in two ways: in angle brackets (< >) or in

    double quotes.

    I l di h d (2/2)

  • 8/8/2019 7. Introduction to C

    23/28Systems Programming WS 2010 Introduction to C (23)

    Including headers (2/2)

    File names in angle brackets, such as:

    #include cause the preprocessor to search for the file in the includesearch path. The mechanism for setting the search path

    varies between machines, operating systems, and Cimplementations.

    File names in double quotes, such as:#include "local.h"

    tell the preprocessor to search for the file in an

    implementation-defined way. What this typically means is tosearch for the file relative to the current directory. If the file isnot found, then the include directive is reprocessed as if it had

    angle brackets instead of quotes.

    Li ki i i d (1/2)

  • 8/8/2019 7. Introduction to C

    24/28Systems Programming WS 2010 Introduction to C (24)

    Linking revisited (1/2)

    When you make an external reference to a function or variable in C, the

    linker, upon encountering this reference, can do one of two things. If it has not already encountered the definition for the function or variable, it

    adds the identifier to its list of unresolved references.

    If the linker has already encountered the definition, the reference is resolved.

    If the linker cannot find the definition in the list of object modules, itsearches the libraries.

    Libraries have some sort of indexing so the linker doesnt need to look throughall the object modules in the library it just looks in the index.

    When the linker finds a definition in a library, the entire object module (but notthe entire library) is linked into the executable program.

    Because the linker searches files in the order you give them, you canpre-empt the use of a library function by inserting a file with your ownfunction, using the same function name, into the list before the library name

    appears.

    Li ki i it d (2/2)

  • 8/8/2019 7. Introduction to C

    25/28Systems Programming WS 2010 Introduction to C (25)

    Linking revisited (2/2)

    When a C executable program is created, certain items are secretly

    linked in. One of these is the start-up module, which contains initialisation routines that

    must be run any time a C program begins to execute.

    These routines set up the stack and initialise certain variables in the program.

    The linker always searches the standard library for the compiled versionsof any standard functions called in the program.

    Because the standard library is always searched, you can use anything in thatlibrary by simply including the appropriate header file in your program.

    If you are using an add-on library, you must explicitly add the library name

    to the list of files handed to the linker.

    O tli

  • 8/8/2019 7. Introduction to C

    26/28

    Systems Programming WS 2010 Introduction to C (26)

    Outline

    Language history

    Brief comparison to Assembly and Java

    The process of language translation

    Separate compilation

    Hello world

    Si l l

  • 8/8/2019 7. Introduction to C

    27/28

    Systems Programming WS 2010 Introduction to C (27)

    Simple example

    hello.c

    #include

    int main(void) {

    /* print out a message */

    printf("Hello World. \n \t Not again! \n");

    return 0;}

    Compile:

    $ gcc hello.c -o hello

    Run:

    $ ./hello

    Hello World.

    Not again!

    S i i th l

  • 8/8/2019 7. Introduction to C

    28/28

    Summarizing the example

    #include

    Include header file stdio.h

    No semicolon at end

    Small letters only C is case-sensitive

    main(void) { }

    Program entry point

    Is the only code executed

    printf("/* message you want printed */");

    Prints a message \n = newline

    \t = tab

    \ in front of other special characters within printf. printf("Have you heard of \"The Rock\" ? \n");