Presentation is loading. Please wait.

Presentation is loading. Please wait.

These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson.

Similar presentations


Presentation on theme: "These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson."— Presentation transcript:

1 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 CpSc 210, and most recently modified by Dr. Lowe for CPSC 2100. Course Notes For CPSC 2100

2 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 o the address of a storage element o the contents of a storage element The Machine Model

3 Memory the address of a storage element o 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 pointer variable holds an address. the content of a storage element o 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. The Machine Model

4 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,...). The Machine Model

5 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. C Program Structure

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 signed integers. An 8 bit signed integer can represent values in the range [-2 7,...0,....2 7 -1] Signed integers are represented internally using 2's complement representation.

7 Declaration of variables Unsigned integers Each of the preceeding declarations 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,....2 8 -1] In all modern computer systems different hardware instructions are 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 statement consists of an expression followed by a semicolon An assignment expression consists of lvalue = expression; lvalue is 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 boolean type 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 1.the type of value returned by the function 2.the name of the function 3.parenthesized declaration of function parameters 4.at least one basic block containing local variable declarations and executable code int main( int argc, /* Number of cmd line parms */ char *argv[]) /* Array of ptrs to cmd line parms */ { --- basic block --- }


Download ppt "These notes were originally developed for CpSc 210 (C version) by Dr. Mike Westall in the Department of Computer Science at Clemson."

Similar presentations


Ads by Google