Chapter 7
Chapter 7
I. Type Definition
.. [....[...[.... :....:...[.....
Syntax: typedef datatype identifier ;
Ex1: typedef int INTEGER
Ex2: typedef char *String;
Source Code: #01
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ cl r scr ( ) ;
t ypedef i nt I nt eger ;
t ypedef i nt Ar r ayI nt [ 10] ;
I nt eger n, i ;
Ar r ayI nt a;
pr i nt f ( " i nput n: " ) ;
scanf ( " %d" , &n) ;
f or ( i =0; i <n; i ++)
{ pr i nt f ( " a[ %d] = " , i ) ;
scanf ( " %d" , &a[ i ] ) ;
}
f or ( i =0; i <n; i ++)
pr i nt f ( " a[ %d] = %d\ t " , i , a[ i ] ) ;
get ch( ) ;
}
II. Enumerated Type
enum ..[....:..[....._... keyword ( enum ) ..........
.... .. ._... .....[..... .:....... ( enumeration
constant)
Syntax: enum { enumeration constant } identifier ;
enum tag { enumeration constant } ;
enum tag identifier ;
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 1/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
Source Code: #02
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
enumMonNum{J AN, FEB, MAR, APR, MAY, J UN, J UL, AUG, SEP, OCT, NOV, DEC};
enumDayNum{SUN, MON, TUE, WED, THU, FRI , SAT};
char *daynames[ ] ={" SUN" , " MON" , " TUE" , " WED" , " THU" , " FRI " , " SAT" };
char *mont hnames[ ] ={" J AN" , " FEB" , " MAR" , " APR" , " MAY" , " J UN" , " J UL" ,
" AUG" , " SEP" , " NOV" , " DEC" };
i nt endmont h[ ] ={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
voi d mont hshow( i nt mont h_num, i nt year , i nt day1)
{ i nt i , bl anks;
pr i nt f ( " \ n\ n%10s%10d" , mont hnames[ mont h_num] , year ) ;
pr i nt f ( " \ n\ n" ) ;
f or ( i =SUN; i <=SAT; i ++)
pr i nt f ( " %7s" , daynames[ i ] ) ;
bl anks=day1;
pr i nt f ( " \ n\ n" ) ;
f or ( i =0; i <bl anks; i ++)
pr i nt f ( " %7s" , " " ) ;
f or ( i =1; i <=endmont h[ mont h_num] ; i ++)
{ pr i nt f ( " %7d" , i ) ;
i f ( ( i +bl anks) %7==0)
pr i nt f ( " \ n\ n" ) ;
}
}
voi d mai n( )
{ i nt year , f i r st day , i , mont h;
cl r scr ( ) ;
pr i nt f ( " \ n I nput t he year : " ) ;
scanf ( " %d" , &year ) ;
i f ( year %4==0) endmont h[ 1] =29;
pr i nt f ( " Ent er t he mont h: " ) ;
scanf ( " %d" , &mont h) ;
i f ( mont h<J AN+1| | mont h>DEC+1)
pr i nt f ( " \ n number i nval i d r ang" ) ;
pr i nt f ( " Whi ch day does 1st of t he mont h?" ) ;
scanf ( " %d" , &f i r st day) ;
i f ( f i r st day<1| | f i r st day>7)
pr i nt f ( " \ n number not i n val i d r ang" ) ;
mont hshow( mont h- 1, year , f i r st day- 1) ;
get ch( ) ;
}
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 2/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
III. Structure
1. Definition
structure ...[... :...._......[......,.. ..
... ..... structure ........... :....:
..[.[........:.
2. Declaration
..,[.... structure ..[..[ keyword ( struct )
Syntax1: struct tag { datatype fieldname1;
datatype fieldname2;
datatype fieldname3;
};
struct tag identifier;
Syntax2: struct { datatype fieldname1;
datatype fieldname2;
datatype fieldname3;
} identifier;
Ex1: struct EMP { int id;
char name[20];
char sex;
int age;
int salary;
};
struct EMP employee;
Ex2: struct { int id;
char name[20];
char sex;
char class[5];
int score;
} Student;
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 3/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
3. Using structure
..,... structure ...: ..,...[.[...
... ... structure .... . fields ..... ........
.[[ structure ...[[ field :...... ..,...:...
.. field : struct ..[. struct_variable.field_name
Ex: struct { int id;
char name[20];
int hours_work;
int salary;
} employee;
employee.id=1;
strcpy(employee.name,Dara);
employee.hours_work=40;
employee.salary=230;
Source Code:#03
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ cl r scr ( ) ;
st r uct { i nt i d;
char name[ 20] ;
i nt hour s_wor k;
i nt sal ar y;
} empl oyee;
pr i nt f ( " Ent er your i d: " ) ;
scanf ( " %d" , &empl oyee. i d) ;
f f l ush( st di n) ;
pr i nt f ( " Ent er your name: " ) ;
get s( empl oyee. name) ;
pr i nt f ( " Ent er your hour s_wor k: " ) ;
scanf ( " %d" , &empl oyee. hour s_wor k) ;
pr i nt f ( " Ent er your sal ar y: " ) ;
scanf ( " %d" , &empl oyee. sal ar y) ;
pr i nt f ( " %15s%15s" , " I d" , " Name" ) ;
pr i nt f ( " %15s%15s\ n" , " Hour s" , " Sal ar y" ) ;
pr i nt f ( " %15d%15s" , empl oyee. i d, empl oyee. name) ;
pr i nt f ( " %15d%15d" , empl oyee. hour s_wor k, empl oyee. sal ar y) ;
get ch( ) ;
}
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 4/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
4. Pointer to structure
.. structure ......,...j.:. .....[ pointer ..,. address
.... pointer ..:.
Ex: struct { int id;
char name[20];
int hours_work;
int salary;
} employee,*emp;
Source Code:#04
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ cl r scr ( ) ;
st r uct { i nt i d;
char name[ 20] ;
i nt hour s_wor k;
i nt sal ar y;
} empl oyee, *emp;
pr i nt f ( " Ent er your i d: " ) ;
scanf ( " %d" , &empl oyee. i d) ;
f f l ush( st di n) ;
pr i nt f ( " Ent er your name: " ) ;
get s( empl oyee. name) ;
pr i nt f ( " Ent er your hour s_wor k: " ) ;
scanf ( " %d" , &empl oyee. hour s_wor k) ;
pr i nt f ( " Ent er your sal ar y: " ) ;
scanf ( " %d" , &empl oyee. sal ar y) ;
emp=&empl oyee;
pr i nt f ( " %15s%15s" , " I d" , " Name" ) ;
pr i nt f ( " %15s%15s\ n" , " Hour s" , " Sal ar y" ) ;
pr i nt f ( " %15d%15s" , emp- >i d, emp- >name) ;
pr i nt f ( " %15d%15d" , ( *emp) . hour s_wor k, ( *emp) . sal ar y) ;
get ch( ) ;
}
5. Complex structure
Complex structure .. structure :.. field . structure .
Ex: struct DOB { int day;
int month;
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 5/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
int year;
};
struct { int id;
char name[20];
char sex;
sturct DOB Dob;
char address[20];
} Person;
Source Code:#05
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ cl r scr ( ) ;
st r uct DOB{ i nt day;
i nt mont h;
i nt year ;
};
st r uct { i nt i d;
char name[ 20] ;
char sex;
st r uct DOB Dob;
char addr ess[ 20] ;
} Per son;
pr i nt f ( " I d: " ) ; scanf ( " %d" , &Per son. i d) ;
pr i nt f ( " Name: " ) ; f f l ush( st di n) ; get s( Per son. name) ;
pr i nt f ( " Sex: " ) ; scanf ( " %c" , &Per son. sex) ;
pr i nt f ( " Dat e of bi r t h\ n" ) ;
pr i nt f ( " Day: " ) ; scanf ( " %d" , &Per son. Dob. day) ;
pr i nt f ( " Mont h: " ) ; scanf ( " %d" , &Per son. Dob. mont h) ;
pr i nt f ( " Year : " ) ; scanf ( " %d" , &Per son. Dob. year ) ;
pr i nt f ( " Addr ess: " ) ; f f l ush( st di n) ;
get s( Per son. addr ess) ;
pr i nt f ( " %d\ t %s\ t %c\ t , Per son. i d, Per son. name, Per son. sex) ;
pr i nt f ( %d/ , Per son. Dob. day) ;
pr i nt f ( %d/ , Per son. Dob. mont h) ;
pr i nt f ( %d\ t , Per son. Dob. year ) ;
pr i nt f ( %s\ n, Per son. addr ess) ;
get ch( ) ;
}
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 6/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
6. Array of structure
.. structure ......,...j.:. ....[.... array ..,,.
.......[.
Ex: struct { int id;
char product[10];
int unit;
int price;
int amount;
} Product[10];
Source Code:#06
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ cl r scr ( ) ;
st r uct { i nt i d;
char pr oduct [ 10] ;
i nt uni t ;
i nt pr i ce;
i nt amount ;
} Pr oduct [ 10] ;
i nt i , n;
pr i nt f ( " i nput number of pr oduct : " ) ;
scanf ( " %d" , &n) ;
f or ( i =0; i <n; i ++)
{ pr i nt f ( " I d[ %d] = " , i ) ;
scanf ( " %d" , &Pr oduct [ i ] . i d) ;
pr i nt f ( " Pr oduct [ %d] = " , i ) ;
f f l ush( st di n) ;
get s( Pr oduct [ i ] . pr oduct ) ;
pr i nt f ( " Uni t [ %d] = " , i ) ;
scanf ( " %d" , &Pr oduct [ i ] . uni t ) ;
pr i nt f ( " Pr i ce[ %d] = " , i ) ;
scanf ( " %d" , &Pr oduct [ i ] . pr i ce) ;
Pr oduct [ i ] . amount =Pr oduct [ i ] . uni t *Pr oduct [ i ] . pr i ce;
}
pr i nt f ( " I D\ t PRODUCTS\ t UNI T\ t PRI CE\ t AMOUNT\ n" ) ;
f or ( i =0; i <n; i ++)
{ pr i nt f ( " %d\ t " , Pr oduct [ i ] . i d) ;
pr i nt f ( " %s\ t \ t " , Pr oduct [ i ] . pr oduct ) ;
pr i nt f ( " %d\ t " , Pr oduct [ i ] . uni t ) ;
pr i nt f ( " %d\ t " , Pr oduct [ i ] . pr i ce) ;
pr i nt f ( " %d\ n" , Pr oduct [ i ] . amount ) ;
}
get ch( ) ;
}
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 7/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
7. Structure and function
...... structure .....[...:. .... parameter
..
Source Code:#07
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
t ypedef st r uct { i nt i d;
char pr oduct [ 10] ;
i nt uni t ;
i nt pr i ce;
i nt amount ;
} PRO;
voi d i nput Pr oduct ( PRO[ ] , i nt ) ;
voi d out put Pr oduct ( PRO[ ] , i nt ) ;
voi d mai n( )
{ cl r scr ( ) ;
PRO Pr oduct [ 10] ;
i nt n;
pr i nt f ( " i nput number of pr oduct : " ) ;
scanf ( " %d" , &n) ;
i nput Pr oduct ( Pr oduct , n) ;
out put Pr oduct ( Pr oduct , n) ;
get ch( ) ;
}
voi d i nput Pr oduct ( PRO Pr oduct [ ] , i nt n)
{ i nt i ;
f or ( i =0; i <n; i ++)
{ pr i nt f ( " I d[ %d] = " , i ) ;
scanf ( " %d" , &Pr oduct [ i ] . i d) ;
pr i nt f ( " Pr oduct [ %d] = " , i ) ;
f f l ush( st di n) ;
get s( Pr oduct [ i ] . pr oduct ) ;
pr i nt f ( " Uni t [ %d] = " , i ) ;
scanf ( " %d" , &Pr oduct [ i ] . uni t ) ;
pr i nt f ( " Pr i ce[ %d] = " , i ) ;
scanf ( " %d" , &Pr oduct [ i ] . pr i ce) ;
Pr oduct [ i ] . amount =Pr oduct [ i ] . uni t *Pr oduct [ i ] . pr i ce;
}
}
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 8/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
voi d out put Pr oduct ( PRO Pr oduct [ ] , i nt n)
{ i nt i ;
pr i nt f ( " I D\ t PRODUCTS\ t UNI T\ t PRI CE\ t AMOUNT\ n" ) ;
f or ( i =0; i <n; i ++)
{ pr i nt f ( " %d\ t " , Pr oduct [ i ] . i d) ;
pr i nt f ( " %s\ t \ t " , Pr oduct [ i ] . pr oduct ) ;
pr i nt f ( " %d\ t " , Pr oduct [ i ] . uni t ) ;
pr i nt f ( " %d\ t " , Pr oduct [ i ] . pr i ce) ;
pr i nt f ( " %d\ n" , Pr oduct [ i ] . amount ) ;
}
}
IV. Union
1. Definition
union ...[... :....._......[.... ... structure :.
:......,.... memory
2. Declaration
..,[.... union ..[..[ keyword ( union )
Syntax1: union tag { datatype fieldname1;
datatype fieldname2;
datatype fieldname3;
};
union tag identifier;
Syntax2: union { datatype fieldname1;
datatype fieldname2;
datatype fieldname3;
} identifier;
Source Code:#08
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ uni on a { i nt i ;
char ch[ 2] ;
};
uni on a key;
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 9/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
key. i =512;
pr i nt f ( " i = %d\ n" , key. i ) ;
pr i nt f ( " ch[ 0] = %d\ n" , key. ch[ 0] ) ;
pr i nt f ( " ch[ 1] = %d\ n" , key. ch[ 1] ) ;
get ch( ) ;
}
.......... union [. 2 bytes:.. location 2 bytes ....:.
fields ( key.a, key.ch[0], key.ch[1] ) .....:...... fields ( key.a =512 ,
key.ch[0] =0, key.ch[1] =2 ..:..:.....[..... :....:.
[...... key.a =512 ( 512 =0000001000000000 )
..:.. structure . union
struct { int ch; union { int ch;
char c[2]; char c[2];
} test; } test;
....:....[ struct test [.. memory 4 bytes ..
....:....[ union test [. memory ....: field .:..
......
key.a
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
low byte
key.ch[0]
high byte
key.ch[1]
1005 1006
test.ch
test.c[0] test.c[1]
1005 1006 1007 1008
test.ch text.c[0] test.c[1]
i =512
ch[0]=0
ch[1]=2
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 10/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
3. Declaration
.. union :.. field . structure structure .. union ........
[ structure .. union union .. structure ....
Source Code:#09
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ cl r scr ( ) ;
st r uct st 1 { i nt a;
char c[ 2] ;
};
st r uct st 2 { i nt b;
char d[ 2] ;
};
uni on Test { st r uct st 1 dat a;
st r uct st 2 key;
};
uni on Test t est ;
t est . dat a. a=350;
t est . key. d[ 0] =0;
t est . key. d[ 1] =56;
pr i nt f ( " \ nt est . dat a. a=%d" , t est . dat a. a) ;
pr i nt f ( " \ nt est . key. b=%d" , t est . key. b) ;
pr i nt f ( " \ nt est . dat a. c[ 0] =%d" , t est . dat a. c[ 0] ) ;
pr i nt f ( " \ nt est . key. d[ 0] =%d" , t est . key. d[ 0] ) ;
pr i nt f ( " \ nt est . dat a. c[ 1] =%d" , t est . dat a. c[ 1] ) ;
pr i nt f ( " \ nt est . key. d[ 1] =%d" , t est . key. d[ 1] ) ;
get ch( ) ;
}
test.data.a =350
test.key.b=350
test.data.c[0]=0
test.key.d[0]=0
test.data.c[1]=56
test.key.d[1]=56
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 11/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
4. Union in structure
........ ...... union ..........:.
.. memory : union .....:....... memory .......[
Source Code:#09
#i ncl ude<st di o. h>
#i ncl ude<coni o. h>
voi d mai n( )
{ t ypedef st r uct { i nt scor e;
char gr ad;
} STUDENT;
t ypedef st r uct { i nt r at e;
i nt hour ;
i nt sal ar y;
} EMPLOYEE;
st r uct { i nt i d;
char name[ 20] ;
char sex;
char t ype;
uni on { STUDENT st u;
EMPLOYEE emp;
};
} Per son[ 10] ;
i nt i , n ;
pr i nt f ( " \ n I nput Number of Per son: " ) ;
scanf ( " %d" , &n) ;
f or ( i =0; i <n; i ++)
{ cl r scr ( ) ;
pr i nt f ( " I d= " ) ;
scanf ( " %d" , &Per son[ i ] . i d) ;
pr i nt f ( " Name= " ) ;
f f l ush( st di n) ;
get s( Per son[ i ] . name) ;
pr i nt f ( " Sex= " ) ;
f f l ush( st di n) ;
scanf ( " %c" , &Per son[ i ] . sex) ;
pr i nt f ( " Type= " ) ;
f f l ush( st di n) ;
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 12/13
C C P Pr ro og gr ra am mm mi i n ng g C Ch ha ap pt t e er r 8 8
scanf ( " %c" , &Per son[ i ] . t ype) ;
swi t ch( Per son[ i ] . t ype)
{ case ' S' : pr i nt f ( " Scor e= " ) ;
scanf ( " %d" , &Per son[ i ] . st u. scor e) ;
i f ( Per son[ i ] . st u. scor e>=50)
Per son[ i ] . st u. gr ad=' P' ;
el se Per son[ i ] . st u. gr ad=' F' ;
br eak;
case ' E' : pr i nt f ( " Rat e= " ) ;
scanf ( " %d" , &Per son[ i ] . emp. r at e) ;
pr i nt f ( " Hour = " ) ;
scanf ( " %d" , &Per son[ i ] . emp. hour ) ;
Per son[ i ] . emp. sal ar y=Per son[ i ] .
emp. r at e*Per son[ i ] . emp. hour ;
br eak;
}
}
cl sr cr ( ) ;
f or ( i =0; i <n; i ++)
{ pr i nt f ( " %d\ t " , Per son[ i ] . i d) ;
pr i nt f ( " %s\ t " , Per son[ i ] . name) ;
pr i nt f ( " %c\ t " , Per son[ i ] . sex) ;
swi t ch( Per son[ i ] . t ype)
{ case ' S' : pr i nt f ( " %d\ t " , Per son[ i ] . st u. scor e) ;
pr i nt f ( " %c\ n" , Per son[ i ] . st u. gr ad) ;
br eak;
case ' E' : pr i nt f ( " %d\ t " , Per son[ i ] . emp. r at e) ;
pr i nt f ( " %d\ t " , Per son[ i ] . emp. hour ) ;
pr i nt f ( " %d\ n" , Per son[ i ] . emp. sal ar y) ;
br eak;
}
}
get ch( ) ;
}
-------------------------------------------------------------------------------------------
VoW
Prepared by Mr. C Ch he ea a S Sa am mn na an ng g 13/13