1 / 16

Course Notes For CPSC 2100

Course Notes For CPSC 2100. These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall (westall@cs.clemson.edu) in the Department of Computer Science at Clemson University .

tuari
Download Presentation

Course Notes For CPSC 2100

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Course Notes For CPSC 2100 These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall (westall@cs.clemson.edu) in the Department of Computer Science at Clemson University. The notes have been modified for CpSc 2100. These notes introduce the C programming language, outline the development of a program called a ray tracer that produces a photo-realistic image of a virtual world, demonstrates standard program development methodology for large programs, and introduces basic tools for program development in a Unix environment. This version was first modified by Dr. Wayne Madison for the Spring 2007 offering of CpSc 210, later modified by Dr. Rose Lowe for the Spring 2013 offering of CpSc210, and most recently modified by Dr. Lowe for CPSC 2100.

  2. The Machine Model • Memory • A one dimensional array of individually addressable storage elements. • Each element, called a byte, holds 8 binary digits (bits) of information. • It is extremely important to understand and be able to distinguish • the addressof a storage element • the contentsof a storage element

  3. The Machine Model • Memory • the addressof a storage element • Addresses begin at 0 and increase in unit steps to N-1 where N is the total number of bytes in the address space. A pointervariable holds an address. • the content of a storage element • Since each byte consists of only 8 bits, there are only 256 different values that can be contained in a storage element. These range from binary 00000000 to binary 11111111, which corresponds to decimal numbers 0 to 255.

  4. The Machine Model • Aggregation of basic memory elements • More than 8 bits are needed for useful application to numerical problems. Thus it is common to group adjacent bytes into units commonly called words. • Multiple word lengths are common, and common word lengths include 2 bytes, 4 bytes, and 8 bytes. • In some architectures (Sun Sparc) it is required that the address of each word be a multiple of word length. That is, the only valid addresses of 4-byte words are 0, 4, 8, 12, 16, ...) • .

  5. C Program Structure • The Basic Block - • { • declaration of variables • executable code • } • Historically, unlike in Java and C++, all variable declarations must precede the first line of executable code within the block. With newer compilers this restriction may not be true, but in any case, scattering variable declarations throughout a program has adverse effects on the readability and maintainability of a program. • Nesting of blocks is legal and common. Each interior block may include variable declarations.

  6. Declaration of variables Two generic types of basic (unstructured) variables exist: integer floating point Integer variables may be declared as follows: char a; /* 8 bits */ short b; /* (usually) 16 bits */ int c; /* (usually) 32 bits */ long d; /* 32 or 64 bits */ long long e; /* (usually) 64 bits */ These declarations implicitly create signedintegers. An 8 bit signed integer can represent values in the range [-27,...0,....27-1] Signed integers are represented internally using 2's complement representation.

  7. Declaration of variables Unsigned integers Each of the preceedingdeclarations may also be preceded by the qualifier unsigned. unsigned char a; /* 8 bits */ unsigned short b; /* (usually) 16 bits */ An 8 bit unsigned integer can represent values in the range [0,....28-1] In all modern computer systems different hardware instructionsare used for signed and unsigned arithmetic.

  8. Encoding of integer constants: Integer constants may be expressed in several ways decimal number 65 hexadecimal number 0x41 octal number 0101 ASCII encoded character 'A' ALL of the above values are equivalent ways to represent the 8 bit byte whose value is: 01000001

  9. Encoding of integer constants: Constants of different representation may be freely intermixed in expressions. x = 11 + 'b' - 055 + '\t'; x = 0xb + 0x62 - 0x2d + 0x9; x = 11 + 98 - 45 + 9; x = 73;

  10. Floating point data Two variations on floating point variables float 32 bits double 64 bits Example float a, b; double c; Floating point constants can be expressed in two ways Decimal number 1024.123 Scientific notation 1.024123e+3 avogadro = 6.02214199e+23;

  11. Executable code Expressions: consist of (legal combinations of): constants variables operators function calls Operators Arithmetic: +, -, *, /, % Comparative: ==, != , <, <=, >, >= Logical: !, &&, || Bitwise: &, |, ~, ^ Shift: <<, >>

  12. Executable code Special types of expression A statementconsists of an expression followed by a semicolon An assignment expression consists of lvalue= expression; lvalueis short for "left-value", which in turn represents any entity that may legitimately be assigned a value: The two most common examples are: A simple variable A pointer dereference

  13. Executable code Warnings: Non-assignment statements (with the exception of function calls) are syntactically legal but (generally) semantically useless: x + y - 3; x <= 2; Use parentheses to avoid problems with: operator precedence(the order in which the operators are evaluated) y = x + 5 & z - 5; and operator associativity (the direction in which operations of equal precedence are evaluated) y = 10 / 5.0 * 4.0

  14. Executable code Control flow: if and while if (expression) statement | basic-block <--- Executed if expression value is true else statement | basic-block <--- Executed if expression value is false while (expression) statement | basic-block <--- Executed while expression remains true do <--- Executed until expression becomes false statement | basic block while (expression);

  15. Executable code Control flow: if and while There is no booleantype in C. An expression is false <=> its value is 0 If the expression is an assignment, the value of the expression is the value that is assigned. Be particularly careful not to confuse the following: if (x = (a + b)) if (x == (a + b)) Be careful not to accidentally use: while (x < y);

  16. Executable code Function definitions A C function is comprised of 4 components • the type of value returned by the function • the name of the function • parenthesized declaration of function parameters • at least one basic block containing local variable declarations and executable code int main( intargc, /* Number of cmd line parms */ char *argv[]) /* Array of ptrs to cmd line parms */ { --- basic block --- }

More Related