Introduction to C Programming http:www'angelfire'comma3los - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to C Programming http:www'angelfire'comma3los

Description:

The compiler compiles each source file into an intermediate object file first. ... Unsigned data type only to represent positive quantities and signed both ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 30
Provided by: duyng
Category:

less

Transcript and Presenter's Notes

Title: Introduction to C Programming http:www'angelfire'comma3los


1
Introduction to C Programminghttp//www.angelfi
re.com/ma3/los
Duy Nguyen, Ph.D. phone (781) 981-2079 email
duy_at_ll.mit.edu
2
Outline
  • Overview of Course
  • Compiler and environment
  • Overview of C programming
  • Program development cycle
  • Data types
  • Standard input/output stream (i.e. cout, cin)
  • Writing a sample C program, compile, and run it

3
Overview of Course
  • Objectives
  • Grading
  • Assignments
  • Final project
  • Exams

4
Compiler and Environment
  • Example of Operating Systems
  • DOS (Disk Operating System)
  • Single user, single task OS for Intel x86
    compatible systems (PCs)
  • Command line mode interface
  • Popularity replaced by Windows NT and 95/98
  • UNIX
  • Command line mode interface
  • Powerful multi-user multi-task OS originated by
    ATT
  • Popular in the RD field, a lot of variations
    from different vendors and developers, e.g.
    SunOS, AIX (IBM), BSD (Berkeley), Ultrix
    (Digital), System V (ATT), Unicos (Cray), Linux
    (amateurs UNIX for PC hackers), etc

5
  • Windows
  • Windows 3.11, Windows NT, Windows 95/98 graphical
    user interface (GUI) from Microsoft
  • Multi-tasking or pseudo-multi-tasking
  • Good for novice users and easy to use
  • X-windows
  • Graphical user interface for the Unix system

6
  • Programs
  • The OS coordinates the proper functioning of the
    various parts comprising the computer system and
    provides a preliminary interface to the user.
    Programs add functionality to the computer
    (translator). Programs are developed using
    programming languages.
  • Low level programming languages
  • Machine dependent
  • Difficult for human beings to read
  • Efficient for machine processing
  • Mostly used for developing OS, real time systems,
    and device drivers
  • e.g. machine language, assembly language

7
  • High-level programming languages
  • Portable (machine independent)
  • Easy to read (English like)
  • Allows programmer to concentrate on problem
    solving
  • Development language for most application
    programs
  • e.g. BASIC, Pascal, FORTRAN, COBOL, LISP, C/C,
    etc
  • High level programs need to be translated into
    machine language before they can be understood by
    the hardware.
  • Two different approaches exist
  • 1. The compiler takes the high-level program as a
    whole and compile (translate) the source codes
    into machine language at once before execution,
    e.g. C/C, FORTRAN

8
  • 2. The interpreter takes one instruction of the
    high-level program at a time, interprets
    (translate) it and executes the instruction
    before the next instruction is interpreted, e.g.
    LISP
  • Compiled programs are more efficient than
    interpreted programs. C/C is a general purpose
    intermediate-level or system-level programming
    language. It is like a high-level language and
    also has the efficiency of low-level languages.
    The language is designed to take the advantage of
    hardware designs. The UNIX operating system was
    written in C.
  • Nowadays, most operating systems are written in
    C/C.

9
Overview of C
  • Program Development Cycle
  • In a big programming project, usually a
    programming team of several programmers is
    involved. Multiple source files are generated.
    In addition, some of the programming effort may
    last for a certain development, test and
    redevelopment period. The compiler compiles each
    source file into an intermediate object file
    first. A linker is then called to link the
    object files and libraries together to generate
    the final executable codes. (A library file is
    an archive collection of pre-compiled object
    codes that have been developed and tested ahead
    of time)

10
Problem Specification
Algorithm Development
Write the Source Code
Compile the Code
Debug the Program
Execute the Program
11
  • Text Files vs. Binary Files
  • Text files are also called ASCII files. They are
    represented by ASCII (American Standard Codes for
    Information Interchange) codes. Text files are
    portable and can be transferred from machine to
    machine over the network. Program source codes
    are all text files so that they can be carried
    from one machine to another and compiled for
    different machine platforms
  • Binary files are usually machine or dependent or
    platform dependent and non-portable. Compiled
    object files, libraries, executable files are
    most of the data files are binary files. Binary
    files are usually more efficient in terms of
    storage and retrieval.

12
  • Editors
  • You need a text editor to edit your programs.
    There are many of them available with different
    ease of use, e.g. vi, emacs for the UNIX
    terminals, textedit and xedit for the X-windows
    systems, notepad for MS-windows, edit for MS-DOS.

13
Writing a C Program
  • A C/C program consists of preprocessor
    directives and statements. Compiling a C/C
    program involves
  • Preprocessor call and,
  • Compilation
  • The preprocessor appends the necessary include
    files to the program, expands user defined
    macros, and processes the machine dependent
    environment variables before the compiler is
    called. The compiler compiles the program into
    object codes.
  • C is an object oriented programming language.
    C programs consist of objects and functions
    (procedures). Every C program has one and only
    one main function that tells the compiler where
    to start executing the program. A function
    consists of statements.

14
  • A return statement at the end of the main
    function signifies the end of the function. Of
    course, the return statement in the main function
    also signifies the end of the program. The
    convention for the main function is to return a 0
    to represent the successful termination of the
    program and a non-zero value to indicate an
    error. The value returned by the main function
    is passed to the operation system. This is
    useful for communicating the status of the
    program to the operating system when it is
    terminated.
  • Keywords
  • These are special words with special meanings
    reserved by the compiler (e.g. int, void, double,
    return, etc). They cannot be used as variable
    names.
  • Statements
  • The function body consists of statements.
    Statements include variable declarations,
    arithmetic and logic expressions, branches, loops
    and function calls

15
  • Note
  • C/C language is case sensitive
  • Statements are separated by semicolons
  • C/C language is a block structured language.
    Use braces and indentations to separate blocks.
    Indentation is not necessary but it is good
    practice to use indentation properly to enhance
    the readability of the program.
  • Comments are used to enhance the readability of
    the program. They are ignored by the compiler.
    Comments are enclosed by / and / delimeters and
    can span across several lines. There is no space
    between the slash and the asterisk. Comments on
    each line can also be preceded by //.
  • Use comments to document your program wherever
    appropriate. Always document your program with
    your name, date, the source file name and the
    purpose of your program.

16
  • Preprocessor directives are prefixed by the pound
    sign . There is no separation between and the
    word following it. include ltiostream.hgt tells
    the preprocessor to include the header file
    stdio.h. The preprocessor replaces the include
    directive with the content of the file to be
    included. This is useful when using
    pre-developed library functions.
  • Header files usually contain function
    declarations (prototypes) which will be explained
    later in the course and macro definitions. cout
    and cin are declared (prototyped) in the header
    file iostream.h. It also contains the prototypes
    of other library I/O functions.

17
Data Types
  • Variable Declarations
  • Syntax
  • data_type variable1, variable2, , variableN
  • Variables are associated memory locations used to
    stored data values. Variables must be declared
    before they can be used. The compiler allocates
    the memory when it sees the declaration. C has
    built-in data types
  • int, unsigned int machine dependent (usually
    2 to 4 bytes long)
  • char, unsigned char one-byte
  • bool one-byte
  • wchar_t two-byte
  • short, unsigned short two-byte
  • long, unsigned long four-byte
  • float four-byte
  • double, long float eight-byte
  • long double ten-byte

18
Identifiers (or variable names)
  • Identifiers represent memory locations to store
    data. You need to declare identifiers before you
    use them. Declaring the variable reserves memory
    space for the variable and associate the address
    with the identifier.
  • (1) Use only alphanumeric characters and
    underscore to create identifiers
  • (2) Do not start with numbers, e.g. 3num is
    illegal.
  • (3) Do not pick names that are too long.
  • (4) Do not use standard names such as cout as
    identifiers.
  • (5) Pick meaningful names for identifiers, e.g.
    radius
  • (6) Use cases and underscore sign wisely to make
    up identifiers, e.g. first_name, studentNumber
  • (7) Do not use cases to distinguish identifiers,
    such as using radius and Radius to mean different
    things although it is completely legal.
  • Examples day, weather, year1, year2, _income,
    annual_income

19
Data Types
  • Floating point representations
  • (1) double
  • Double precision floating point real numbers.
    8-byte
  • Numbers entered with a decimal point or in
    exponential form are double by default
  • e.g. 3.5, 4.6, 5., 6.0E7, -8.3e-6
  • (2) float
  • Similar to double, except only 4-byte storage
  • Smaller range of representation, less precision
  • Less memory usage
  • Floating point numbers are followed by a modifier
    f
  • e.g. 3.5f, 6.24F, 5.5e8f, etc
  • (3) long double
  • 10-byte long (more precision and bigger range)
  • e.g. 3.333L, 0.57e4L

20
  • Integer representations
  • (1) int (whole numbers)
  • 2- or 4-byte long depending on the compiler
  • Most efficient data type in term of operation
    speed
  • By default, whole numbers are int, e.g. 3, 60,
    -999
  • The range of int is defined as two constant
    macros as INT_MAX and INT_MIN in limits.h.
  • (2) long
  • Long integer, 4-byte
  • Bigger range of representation
  • e.g. 300L, 65L (avoid small l to avoid
    confusion)
  • (3) short
  • Short integer, 2-byte
  • No specific way to write short constants, can be
    specified (short) 3

21
  • (4) char
  • 1-byte integer to represent characters (ASCII)
  • All integer operations, , -, , /, are
    applicable
  • Printable characters, e.g. a,0, , ,
    \n
  • Comparison is possible
  • C allows conversion between character and
    integer. We can use printf(I, c) to print
    out the ASCII code of c.
  • Also, we can use printf(c, c 5) to print
    out the fifth ASCII character after c
  • (5) w_char
  • Same as a char, except it is 2-byte long
  • (6) bool
  • 1-byte integer taking on two values true and
    false.

22
  • Signed and Unsigned
  • All the integral data types can be both signed
    and unsigned. Unsigned data type only to
    represent positive quantities and signed both
    positive and negative
  • Unsigned constants are specified using U or
    u, e.g. 50u, 45UL, 127uL, etc
  • Use signed (optional) and unsigned modified
    before the data type in variable declaration,
    e.g.
  • signed int x, y
  • unsigned short u, v
  • unsigned long h, k
  • Initializations
  • Declaration only reserves memory space for
    variables. The content is undefined
  • int x-5
  • double pi3.14159

23
  • Precision in number representations
  • Integer types represent EXACT quantities and are
    more efficient in operation but are more limited
    in range
  • Floating point numbers are represented by
    mantissa and exponent
  • Value Mantissa x 2Exponent
  • Bigger range. As in decimal systems, certain
    fractions such as 1/7 can only be approximated.
  • gt rounding errors
  • Cancellation (offset) errors
  • When a very big fractional number is operated on
    a very small number, due to the limitation in the
    number of bits used to represent floating point
    numbers, the small number will be cancelled,
    e.g.
  • 10000.0 0.00000000001
  • gives a result 10000.0

24
  • Overflows and Underflows
  • Finite memory size sets limits to the range of
    number that can be represented by each data type.
    ltlimits.hgt defines some macro constants of the
    maximum and minimum values of certain data types,
    e.g. INT_MAX, INT_MIN, etc
  • Overflow occurs when an operation generates a
    value which is outside the range of
    representation, e.g. a very big positive or a
    very small negative number.
  • Underflow occurs when the value generated is too
    small to be represented, e.g. the product of two
    very small non-zero floating point numbers which
    requires the precision that is beyond the ability
    of the hardware. A zero is obtained in reality.

25
Input/Output
  • Comments
  • // This is a comment in C which can occupy
    only one line.
  • /
  • This is also a comment in C which can span
    multiple
  • lines
  • /
  • Input / Output in C
  • I/Os in C are achieved using iostreams.
  • include ltiostream.hgt
  • Predefined input stream (data type istream) cin
    and output stream (data type ostream) cout are
    connected to the keyboard and monitor of your
    computer.
  • Input is taken from the istream cin using the
    extraction operator gtgt
  • Output is sent to the ostream cout using the
    insertion operator ltlt

26
  • Example
  • include ltiostream.hgt
  • int main( void )
  • double side1, side2
  • cout ltlt Input the two sides of a rectangle
  • cin gtgt side1 gtgt side2
  • cout ltlt Length of side 1 is
  • ltlt side1 ltlt endl ltlt Length of side 2 is ltlt
    side2 ltltendl
  • return 0

27
  • Escape sequence characters
  • \n new line
  • \r carriage return
  • \t horizontal tab
  • \v vertical tab
  • \b backspace
  • \\ backslash
  • \ double quotation mark
  • \ single quotation mark
  • \? Question mark
  • \ddd ASCII code in octal format
  • \xdd ASCII code in hexadecimal format

28
Example
  • include ltiostream.hgt
  • int main(void)
  • int temperature
  • double money
  • money 30.76
  • temperature 80
  • cout ltlt It is a nice day.
  • cout ltlt The temperature is ltlt temperature ltlt
    degrees.\n
  • cout ltlt I have ltlt money
  • cout ltlt Lets go party. ltlt endl
  • return 0
  • Output
  • It is a nice day. The temperature is 80 degrees.
  • I have 30.76. Lets go party.

29
Example
  • include ltiostream.hgt
  • int main(void)
  • int x
  • double y
  • cout ltlt Enter an integer number
  • cin gtgt x // Get integer data from the
    keyboard
  • cout ltlt endl ltlt Enter a double number
  • cin gtgt y
  • cout ltlt endl ltlt The numbers are ltlt x ltlt
    ltlt y ltlt endl
  • return 0
Write a Comment
User Comments (0)
About PowerShow.com