final review yuan long csc3320. variables byte,char,int,long float,double

19
Final Review Yuan Long CSC3320

Upload: toby-bradford

Post on 02-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Final Review

Yuan LongCSC3320

Page 2: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Variables

• byte,char,int,long• float,double

Page 3: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Variables

Type conversion:Narrower type can be converted to wider type.Wider type can be converted to narrower(Cast).long double

double

float

Unsigned long int

long int

unsigned int

int

float f, frac_part;frac_part = f – (int) f;

short int i;int j = 1000;i = j * j; /* WRONG */

Page 4: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Variables

• structstruct sample{

int a;char b;

};

sample test1,*q;test1.a=1;(*q).a=2;q->b=‘3’;

Page 5: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Arrays

• Declaration for an array is different from java.

One dimensional:int a[3];int a[3]={0};int a[3]={1,2};int a[3]={1,2,3};Int a[]={1,2,3};

Two dimensional:int a[2][2]={{1,2},{3,4}};

Page 6: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

String

• Character array(String in java)• ‘\0’ means the end of a string• char data1[8] = “June 14”; //strlen(data1)=7

• char data2[9] = “June 14”; //strlen(data2)=7

• char data3[7] = “June 14”; //strlen(data3)? Error

• char data4[ ] = “June 14”; //strlen(data4)=7

J u n e 1 4 \0

J u n e 1 4 \0 \0

J u n e 1 4

char *data5 = “June 14”; //strlen(data5)=7

Page 7: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

String

• printf(“%s”, str);• scanf(“%s”,str); // read until ‘\n’ found

Page 8: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Pointer

• Declaration and initialization:int i,j;int *p=&i,*q=&i;

• Assign value:p=&j;*p=j;*(&j) equals j

Page 9: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Example 1 of pointer

int i, j, *p, *q;p = &i;q = p;

p

q

i

Page 10: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Example 2 of pointer

int i, j, *p, *q;p = &i;q = &j;*q = *p;

p

q

i

Page 11: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Pointer and Array

• Use a pointer to represent an arrayint a[5];char b[5];int *pa=a; or int *pa=&a[0]char *pb=b; or char *pb=&b[0]

• Use a pointer to access the element in arrayp = &a[2];q = p + 3;p += 4;

Page 12: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Pointer

• Use pointer as an argument.Be careful: the original value may be changed.Refer to swap example.Refer to array example.

• Use pointer as a return value.Avoid returning a local pointer(defined in a function).

Page 13: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Functions

• Declare your functions before you use them• Two ways:

(1) The whole definition is placed before you use them.

(2) Just declare it before you use them(prototype).

double average(double x, double y);double average(double, double); int main(){ // use average} double average(double x, double y){ ……….}

Page 14: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Functions

• Parameters(appear in function definition)• Arguments(appear in function call)• Recursive function

Page 15: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Library Functions

• How to use printf()?• How to use scanf()?e.g. scanf(“%d,%d”,&x,&y);

• Useful functions in math.h• Useful functions in string.h

Page 16: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

System calls

• File managementopen()close()read()write()lseek()

Page 17: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

System calls

• Process managementfork()wait()getuid()exec()

Page 18: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Process

• Init() process • How to duplicate a process?• Zombie process?• Orphan process?• How to get rid of zombie process?wait()

Page 19: Final Review Yuan Long CSC3320. Variables byte,char,int,long float,double

Final Exam

• Multiple choice (Only one answer)• Short answers• Filling the blanks for short program• Writing the output for a program• Write a program