CS/CE 218 Lecture -*- Outline -*- connection: we've seen a brief overview of C; now we'll look at the details. In this lecture we'll focus on what is different from Pascal about C's ... * Types, operatiors, and expressions (chapter 2) advert: The core of any language, especially an expression language, is the set of built-in data types and expressions. ** Variable Names (section 2.1) Q: Is case (UPPER vs. lower) significant in C variable names? Q: What case is usually used for variable names in C programs? Q: Can you use ``float'' as a variable name? Q: What are some good rules for picking variable names? ** Atomic data types (section 2.2) In the ADT perspective, this section describes the set of objects of each of the numeric types. *** char Q: What is the size of a char? *** int, short, long also (short int = short, long int = long) Q: When should you use short, long, and int? distinctions help portability, int is "always efficient" use short or long based on information content short (at least 16 bits) < int < long (at least 32 bits) *** signed, unsigned Q: Is char the same as unsigned char? no, not on all machines char, signed char, unsigned char should be considered distinct use char for character data, signed char very small (8 bit?) ints unsigned char as modulo 2^n numbers no guarantee on how big or small chars are in Japan chars are probably 16 bits look in to see Q: Is signed int the same as int? Is unsigned int the same as int? *** float, double, long double Q: Does double always have more precision than float? no, but this is usual... ** constants (also called literals) (section 2.3) can think of literals as 0-argument operations that create objects of the appropriate type *** integer and floating point constants A suffix of L or l makes the constant long (or long double); for floats the suffix f makes the constant a float, otherwise it's a double. A + or - sign is not technically part of a constant. A floating point constant needs a decimal point or an exponent. Using octal or hex for floating point is not allowed. Q: Can you complete the following table: literal | type 3 | int 27L | 981u | 27498ul | 81.3 | .34l | 2e+4 | 2.73f | 3E9Fl | 3.14e0L | 0755 | 0x7F4L | 0XACFUL | ? Q: Is 010 == 10 in C? As illustrated by the last few, Onnn - is nnn in octal (so 010 is not 10, but 8) 0xnnn - is nnn in hex Good form always puts x in lower case (0x22A) *** char constants enclosed in single quotes (in Pascal a string is in single quotes) denote integers Q: Is '0' portable? yes, if you're using it for a character no, if you're using it as an integer Q: What are the most important character escape sequences? \n, \t, \b (bs), \\, \', \", see page 38 for others \ooo = characater ooo in octal \xhh = character hh in hex Q: What does '\0' mean? Why would you use it instead of 0? *** string constants enclosed in double quotes can use character escape sequences Q: How would you type in a very long string in a C program? "long string start" " and end" Q: How are strings represented at run-time? Q: Is "c" the same as 'c' in C? no, the former is a 2 character array, the latter an integer. *** enumeration constants enumeration just defines names for integers no checking for valid range (sigh) Unlike Pascal, no operations come with enum types (no ord, next, ...) names are distinct, values need not be Q: What's the default value of RED in the following: enum colors { red, yellow, blue }; ? Q: How could you make red have the value 1, yellow 2, blue 3? Q: Is there any advantage for using enumerations over #defines? ** declarations (section 2.4) can think of a declaration as the creation of a cell (variable), storing values of a given type: declaration: type t -> cell of t Q: What's the equivalent for each of the following Pascal declarations Pascal | C var x: integer; | int x; var x,y: integer; | var a: array 0..10 of real | var a,b: array 0..10 of int | ? Q: Can you initialize variables as they are being declared? yes! (cf. Pascal) ** arithmetic operators (non-basic constructors) (section 2.5) C has th usual arithmetic operators + - * /, also % Q: What is the value of 3 % 2? 1 (% is modulus) Q: What is the value of 3 / 2? Q: Is it portable to do integer division if one operand is negative? no, direction of truncation undefined *** Bitwise operators (section 2.9) Not really numeric operators, should be thought of as ops on bit-fields contained in ints best to use unsigned ints for these... like sets in Pascal & like Pascal's * | is inclusive OR ^ is exclusive OR << is left shift >> is right shift ~ is one's complement (unary) Q: If you need to do shifting, is it more portable to use signed or unsigned ints? combinations often help with portability (avoiding word sizes) e.g., x & ~077 vs. x & 0177700 (16 bits) Q: Can you do one of exercises 2-6 or 2-7? ** relational and logical operators (section 2.6) (observers, operations on "booleans") Usual relation on numeric types <, <=, >=, > Q: What is the difference between == and = in C? not pr== groups tighter (has higher precedence than) = this is perhaps "wrong"... must write (c = f()) == 0 as c = f() == 0 means c = (f() == 0) Q: How does && differ from Pascal's "and"? short-circuit evaluation Q: Is || like the || of the Bourne shell? yes, also && is similar if you remember different def of true Q: How are true and false represented in C? true = non-zero, false = 0 if (valid) same as if (valid != 0) Q: Can you do exercise 2-2? *** Conditional expressions (section 2.11) Can think of these as more operations on booleans b ? 3 : 4 --------------- int T(int m) { return (m % 2) ? (3*m+1)/2 : m/2 ; } --------------- Q: Can a conditional expression be used anywhere some other expession can be? Q: What's the type of ``b ? 3.14 : 0''? double can simplify any statement of the form if (cond) var = exp1 else var exp2 to var = cond ? exp1 : exp2 so don't have to write var name twice ** Type conversions (section 2.7) also called coercions, as they are implicit changes from one representation to another most (like int to float) are what you'd expect but if you're an old C user, floats no longer -> double not all coercions preserve values, e.g., i = 3.4 truncates i Q: Can characters be used as integers? Q: What is the sign of a character used as an integer? + if a "normal" character use signed or unsigned char if storing small ints in chars *** ctype.h contains observers and other ops on characters isupper, islower, isdigit toupper, tolower *** converting longer values to shorter (smaller) values Q: how are integers converted to chars? drop most significant bits Q: how is a float converted to an int? truncation watch for overflow... *** explicit conversions (casts) (section 2.7) makes a conversion explicit Q: Does a cast change the value of a variable; that is, does (int) r change the value of r? ** operations on variables these are operations that act on numeric variables (lvalues) *** increment and decrement operators (section 2.8) Q: What is the difference between ++i and i++? Q: Why doesn't ++3 make sense? These are often handy in arry indexing ------------ if (c == '\n') { s[i++] = c; } ------------- Q: Can you do exercise 2-4 or 2-5 on page 48? *** assignment operators (section 2.10) Can write i = i+2 as i += 2. Q: Are there any operators for which an an assignment op is not defined? Q: If i == 5, a[6]==6 and a[7]==7, what is the value of a[++i]+=2? ** precedence rules and order of evaluation (section 2.12) *** precedence rules Q: What does "higher precedence" mean to you? Q: Which precedence rules will you have to remember? & ^ | have lower precedence than == and != so x & mask == 0 means x & (mask == 0) *** order of evaluation C doesn't specify order in which arguments are evaluated Q: Why do you think C doesn't specify order of evaluation for arguments?