50 likes | 135 Views
Comments. Any string of symbols placed between the delimiters /* and */. Can span multiple lines Can’t be nested! Be careful. /* /* /* Hi */ is an example of a comment. Keywords. Reserved words that cannot be used as variable names OK within comments . . .
E N D
Comments • Any string of symbols placed between the delimiters /* and */. • Can span multiple lines • Can’t be nested! Be careful. • /* /* /* Hi */ is an example of a comment. Keywords • Reserved words that cannot be used as variable names • OK within comments . . . • Examples: break, if, else, do, for, while, int, void • Exhaustive list in book
Identifiers • A “token” (“word”) composed of a sequence of letters, digits, and underscore (“_”) character. (NO spaces.) • Used to give names to variables, functions, etc. • Identifiers such as “printf” normally would not be redefined; be careful • Only the first 31 characters matter Constants • Will discuss more in near future. • 0, 77, 3.14 examples. • Strings: double quotes. “Hello” • Characters: single quotes. ‘a’ , ‘z’ • Have types implicitly associated with them… • 1234567890999 too large for most machines…
Operators and “Punctuators” • These are special characters with particular meanings, potentially depending on context • + - * / are the usual arithmetic operators, and can be applied to integer types as well as floating-number types • % is “mod operator”: a % b is equal to the remainder when a is divided by b, and returns a value in the range 0…(b – 1). We won’t worry about what happens when either a or b is non-positive. • % character has different meaning in printf( “%d”, a ); • Punctuators include parentheses, braces, semicolons… Some special operators provided by C • ++, --, +=, -=. Increment/decrement, assignment… • x += 2; equivalent to x = x + 2; • a--; equivalent to a -= 1; • Left hand side of any assignment must be “lvalue”; x + 2 is not a valid lvalue
Precedence of Operators • You may have learned about this in the third grade: • 1 + 2 * 3 has the value of 1 + (2 * 3) • If we want the addition to be performed first, must parenthesize: (1 + 2) * 3. • We say that * has a higher precedence than +. • See K&R page 53. Associativity of Operators • Question number two: What about operators at the same precedence level? For instance, * and / ? • Is 12 / 6 * 2 equal to (12 / 6) * 2, or 12 / (6 * 2) ? • It’s the first: these operators are left associative (as are most) • Moral of story: I say parenthesize when in doubt.
++ , the tricky guy int a = 10; printf( “%d”, a++ ); int a = 10; printf( “%d”, ++a ); • Two variants: before a variable and after. • I prefer personally not to mix in ++ or – as part of a more complicated expression • Another complication: int a = 10; printf( “%d %d”, a++, a++ ); • This complication not limited to the ++, -- operators, but occur with any operator with side effects