1 lecture 3 lexical elements some operators: /, %, =, +=, ++, -- precedence and associativity ...

22
1 Lecture 3 Lexical elements Some operators: /, %, =, +=, ++, -- precedence and associativity #define Readings: Chapter 2 Section 1 to 10.

Post on 22-Dec-2015

223 views

Category:

Documents


0 download

TRANSCRIPT

1

Lecture 3

Lexical elementsSome operators:

/, %, =, +=, ++, -- precedence and associativity

#define Readings: Chapter 2 Section 1 to

10.

2

Syntax of C

Like any language, C has an alphabet and rules for putting together words and punctuation to make legal program; that is called syntax of the language

C compilers detect any violation of the syntactic rule within the program

C compilers collect the characters of the program into tokens, which form the basic vocabulary of the language

Tokens are separated by space

3

Tokens Tokens in C can be categorized into:

keywords, e.g., return, int identifiers, e.g., user-defined variables,

identifiers used in preprocessing statements and function names

character constants, e.g. ‘c’ string constants, e.g., “Hello” numeric constants, e.g., 7, 11, 3.14 operators, e.g., = (assignment operator), ++ (increment operator) punctuators, e.g., ; and ,

4

Keywordsauto do goto signed unsigned

break double if sizeof void

case else int static volatile

char enum long struct while

const extern register switch

continue float return typedef

default for short union

PS: Keywords in italics are rarely used.

5

Keywords (cont’d)

There are only 32 keywords (C is small) Each keyword in C has a reserved meaning

and cannot be used as identifiers Most of the keywords given in the previous

page will be covered in this course; some keywords are deliberately omitted

Note: main is NOT a keyword. However, every program will use it as a function name. Therefore you should not use it as variable names.

6

Identifiers

An identifier is composed of a sequence of letters, digits and underscore

An identifier must begin with either a letter or an underscore (not recommended)

Identifiers give unique names to various objects in a program; reserved keywords such as float and int cannot be used as identifiers

In ANSI C, at least the first 31 characters of an identifier are discriminated

Always use meaningful names for identifiers!!!

7

Constants

Constants can be numeric-, character- or string-based, e.g., 13, 7.11, ‘\n’, “Tuesday”

Numbers are represented in decimal system but octal (preceded by 0) or hexadecimal integers (preceded by 0x) can also be represented, e.g., the following number are equal to a decimal 26

032 /* an octal integer */0x1a /* an hexadecimal

integer */

8

Constants (cont’d)Character constants are enclosed by

single quotation marks, e.g., ‘a’, ‘\n’, ‘\t’

String constants are delimited by double quotes, e.g. “Hello world\n”

String is treated as an array of characters in C

If “ or newline characters are to be used within a string, it has to be preceded by a backslash \

9

Operators

Some types of operators: Arithmetic, e.g. +, -, *, /, %, ++, -- Assignment, e.g., = Relational Boolean (logical)and a few more…

Some symbols have meaning that depends on context, e.g.,

printf(“%d”, 40%7);

10

Punctuators

Punctuators such as , and ; are used to separate language elements, e.g.,

int a, b = 4, c = 4;

a = b + c;

11

The / operators The “division” operator, /, is used to compute

the quotient when one number is divided by another

Example 1:float a = 20, b = 7, c;

c = a/b;

printf(“%f”, c); /* 2.857143 */

Example 2:int a = 20, b = 7;

float c;

c = a/b;

printf(“%f”, c); /* 2.000000 */

12

The % operators

The “modulus” operator, %, is used to compute the remainder when one integer is divided by another

Example:int a = 41, b = 7;

printf(“%d”, a%b); /* 6 */

printf(“%d”, 49%b); /* 0 */

Question: What is the value of -30%7 ? Cannot be applied on floating point values

13

Expressions & statements

An expression is a meaningful combination of operators, variables and/or constantsE.g. a + b - 10

Every expression has a value A valid expression followed by a semicolon is

a statement. E.g. a + b - 10;(although this is not very useful)

There are other forms of statements. In general, a statement specifies some actions.

14

Assignment operator The following is an assignment expression:

a = 1 involving variable a, constant 1 and the assignment operator =

The value of the above expression is defined as 1. The expression followed by a semicolon:

a = 1; is an assignment statement.

Further examples: a = (b=32) + (c=23);a = b = c = 0;

15

Efficient assignment operators

Generic form of efficient assignment operatorsvariable op= expression;

where op is operator; the meaning isvariable = variable op (expression);

Efficient assignment operators include+= -= *= /= %=

>>= <<= &= ^= |=

16

Efficient assignment operators

Examples:

a += 5; is same as a = a + 5;

a -= 5; is same as a = a - 5;

a += b*c; is same as a = a + (b*c);

a *= b+c; is same as a = a * (b+c);

17

Increment & decrement operators

In terms of the actions done on variable k, k++ and ++k are equivalent to k=k+1 k-- and --k are equivalent to k=k-1

Post-increment and post-decrement: k++ and k-- k’s value is altered AFTER evaluating the expression int k=1, j;

j=k++; /* result: j==1, k==2 */ Pre-increment and pre-decrement: ++k and --k

k’s value is altered BEFORE evaluating the expression int k=1, j=0;

j=++k; /* result: j==2, k==2 */

18

Precedence & associativity of operators

An expression may have more than one operator and its precise meaning depends on the precedence and associativity of the involved operators

What is the value of variable d after executing each of the following statements

int a = 1024, b = 8, c = 2, d; d = a*b+c;

d = a–b–c; // (a-b)-c or a-(b-c)?

d = a/b/c; // (a/b)/c or a/(b/c)?

19

Table of Precedence & Associativity

See p.397 of [Kelly & Pohl 2001] for a complete table with all operators in C.

Operator precedence (high to low) Associativity() ++ (postfix) -- (postfix) left to right + -(unary) ++(prefix) --(prefix) right to left

* / % left to right + - left to right

= += -= *= /= etc. right to left

20

Some good programming habits

Always use brackets ( ) to express your exact intention if you are not sure about the precedence/associativity

Try to avoid very complex expressions

21

Using #define directives

Two purposes: to define symbolic constants, and macros

The directives#define MAX_SCORE 100

causes all occurrences of MAX_SCORE in the C program to be replaced by 100 BEFORE the program is compiled.

It is a convention to name a symbolic constant with upper case letters.

22

Example use of #define/* To convert length in inches to cm */

#include <stdio.h>

#define RATIO 2.54

int main(void)

{

float inches, cm;

printf(“Enter length in inches: ”);

scanf(“%f”, &inches);

cm = RATIO*inches;

printf(“approximately %f in cm\n”, cm);

return 0;

}