final review yuan long csc3320. variables byte,char,int,long float,double
TRANSCRIPT
Final Review
Yuan LongCSC3320
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 */
Variables
• structstruct sample{
int a;char b;
};
sample test1,*q;test1.a=1;(*q).a=2;q->b=‘3’;
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}};
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
String
• printf(“%s”, str);• scanf(“%s”,str); // read until ‘\n’ found
Pointer
• Declaration and initialization:int i,j;int *p=&i,*q=&i;
• Assign value:p=&j;*p=j;*(&j) equals j
Example 1 of pointer
int i, j, *p, *q;p = &i;q = p;
p
q
i
Example 2 of pointer
int i, j, *p, *q;p = &i;q = &j;*q = *p;
p
q
i
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;
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).
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){ ……….}
Functions
• Parameters(appear in function definition)• Arguments(appear in function call)• Recursive function
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
System calls
• File managementopen()close()read()write()lseek()
System calls
• Process managementfork()wait()getuid()exec()
Process
• Init() process • How to duplicate a process?• Zombie process?• Orphan process?• How to get rid of zombie process?wait()
Final Exam
• Multiple choice (Only one answer)• Short answers• Filling the blanks for short program• Writing the output for a program• Write a program