declarations and expressions c and data structures baojian hua [email protected]

29
Declarations and Expressions C and Data Structures Baojian Hua [email protected]

Post on 19-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Declarations and Expressions

C and Data StructuresBaojian Hua

[email protected]

Page 2: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Overview Variable declaration

the way to introduce variables into programs

Operators do some useful operations on variables and

constants Expressions

form complex expressions with variables, operators, constants and other expressions

Page 3: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Variables Variable formation rules:

consists of one or more letter or digit, (“_” counts as a letter)

starts with a letter lower and upper cases are distinct different from key words

It’s common practice not to start with “_” variable names in C library

Choose informative variable names Compare “sldj_wdwp_” with “name”

Page 4: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Types and Constants

type name rule example

char num or char ‘a’, 97

int num or char 97, ‘a’

long suffixes: “L” or “l”

97L, 999l

float suffixes: “F” or “f”

97.0f, 2.5e-2F

double 97.0, 2.5e-2

Page 5: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Types and Constants(2)

octal starts with 0 05, 013

hexadecimal starts with 0x

0x5, 0x13

escapesequence

\ooo\xhh

\013\xb

string “…” “hello, world”

enum enumeration enum boolean { NO, YES };

Page 6: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Variable Declaration C obeys the variable use-after-declaration r

ule Variable declarations consists of these part

s: Type name Variable list (may only one) initialization Ex: int x, y, z; double f; char c = ‘c’;

Type qualifiers: signed, unsigned (default is signed), const

Page 7: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Sample Variable Declarationint low, high;

char line[1000];

double pi = 3.14;

const double e = 2.71;

const char digits[] = “0123456789abcdef”;

Page 8: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Variable Declaration

A declaration makes clear a variable’s type, size and allocates memory for it

int i, j;$#*@?

?%*

Page 9: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Variable Initialization

Programmers’ duty to initialize that memory space

int i, j;i = 9;

9

?%*

Page 10: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Array Declaration

int i, j;i = 9;

int a[5];

9

$?

#@

&^

!~

@$

a

?%*

Page 11: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Array Initialization

int i, j;i = 9;

int a[5];for (int j=0; j<5; j++) a[j] = j*j;

9

0

1

9

4

16

?%*

a

Page 12: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Arithmetic Operators

Operator: Example:

+ x + y

- x – y

* x * y

/ x / y

% x % y

+ + x

- - x

Page 13: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Relational and Logical Operators

Operator: Example:> x > y>= x >= y< x < y<= x <= y== x == y!= x != y&& x && y|| x || y! !x

Page 14: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Relational and Logical Operators

Boolean operators are short-circuit Computations stop as soon as true or f

alse is known

x = 9;

if ((3>2) || (x=8));

// what’s x’s value? 8 or 9?

Page 15: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Relational and Logical Operators// another example on &&x = 9;if ((1<0) && (x=8));// what’s x’s value? 8 or 9?

// a more fancy exampleint i = 0;int a[10];

while (i<10 && a[i]!=0){…

}

Page 16: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

“!” Not “!” converts 0 to 1, and non-zero values to

0

if (!a){ …}// reads “if a does not hold”, then …// be equivalent with

if (0 == a) { …}// we’d see more examples later

Page 17: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Increment and Decrementint n = 9;

int x;

x = n++;

// x == 9

// n == 10

int n = 9;

int x;

x = ++n;

// x == 10

// n == 10

Page 18: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Bitwise Operators

Name: Example:

& x & y

| x | y

^ x ^ y

<< x << y

>> x >> y

~ ~ x

Page 19: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Bitwise Operators Example// count num of 1’s in an integer xint count (int x){

int num = 0;unsigned int y = (unsigned int)x;

while (y) { if (y & 0x1) num++; y >>= 1;}return num;

}

Page 20: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Assignment Operators and Expressions Assignment is a special kind of expression, s

o the following code fragment is legalint x;

printf (“%d\n”, x=999);

Other kind of assignment expressions are of the form: e1 op= e2 Equivalent to: e1 = e1 op e2 Example: x += 9; ====> x = x + 9; See the text for a complete list of op

Page 21: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Assignment Operators and Expressions // more examples on assignment operators:

int a, b, c;

a = b = 9;

a = (b = 9) + (c = 8);

a = (b = 9) / (c = 3);

// even as the arguments of function calls:

int a, b;

x = sum (a = 1, b = 2);

Page 22: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Conditional Expressionsif (a > b) max = a;else max = b;

// C provides a more succinct way to do this:max = (a>b) ? a : b;

// the general form is:e1 ? e2 : e3;

// if e1 evaluates to true, then evaluates e2,// else evaluates e3.

Page 23: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Type Conversion (Cast) When the type of some expression

is not compatible with the one expected, automatic type conversion occurs

Example from our previous code:

// all of c, ‘0’ and ‘9’ are automatic converted// into integer values by the compiler:if ((c>=‘0’) && (c<=‘9’)) a[c-’0’]++;

Page 24: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Type Conversion (Cast)

Generally, there are two kinds of automatic cast: cast a “big” into “smaller” ones

long ==> int cast a “small” into “bigger” ones

char ==> int The former is not always safe

Page 25: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Type Conversion (Cast) The general scheme for “safe” cast is:

double float

long

unsigned

int char

Page 26: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Type Conversion (Cast)// Dirty simple? However, it’s more subtle than// it first looks. Consider:#include <stdio.h>int main (){ int i = -1; unsigned int j = 0;

if (i < j) printf (“Never reach here, :-( \n”); else printf (“Shoot myself in the foot\n”); return 0;}

Page 27: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Type Conversion (Cast) Don’t expect the compiler will always

behave as you desire Two general principals:

Always use the C’s type system as strong as possible

It’s your basic protection One reason for strong type system’s

popularity Make use of C’s type conversion explicitly

To make clear what we are doing

Page 28: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Explicit Type Cast General form of explicit type conversion:(type) expression

// which converts the expression’s type to type

// More examples:

int i = (int)3333.14;

char c = (char)i;

int a[10];

a[(int)2.71] = 99;

As we see, data precision may be changed

Page 29: Declarations and Expressions C and Data Structures Baojian Hua bjhua@ustc.edu.cn

Safety Issue Type conversion is an infamous source of C

programs bugs especially with pointers (int *)999 will send your passwd to BillG…

Any serious and well-designed C program should use type conversion really rarely

General principle: do NOT use it, always use the explicit form