This is an addendum to Chapter 12. It is about the header files and it shows haw can be created user-defined header files.
12-a.1 The header files Header files contain definitions of functions, data types, constants, and even variables. They can be incorporated into any C program by using the preproces- sor directive #i ncl ude. There are several standard header files that are pro- vided together with each compiler; these cover the most important and frequent- ly used operations from different areas: input/output actions, string handling, mathematical operations, data conversions, and others. Besides these standard library functions, the programmer is able to implement his own functions, which can be gathered into a library. The prototypes of these functions will form a new header file; it can be used in a program almost the same way a standard header file is used. The difference is given by the way this header file is specified in the preprocessor directive #i ncl ude. Generally, there are two possibilities to do this (describing two different strategies for file searching): #i ncl ude <st di o. h> #i ncl ude " myHeader . h" The use of angle brackets <>informs the compiler to search for the specified file in a standard list of systems folders (for instance, the compilers i ncl ude folder). This is typically used for the systems header files. The use of the double quotes " " around the filename informs the compiler to search in the current folder (or in a specific folder of a project) for the indicated file. Usually, this is a user-defined header file. Declaring a new header file is a good idea each time there is a group of related declarations (of functions, data types, etc.) and all or most of them are needed in several different source files. The alternative is to rewrite (or to copy) the entire code into each source file that needs it. This would be time-consuming and error-prone. Therefore, one important advantage of the header files is that they are written once and they can be used as many times as they are needed. Other Computer Programming The C Language
2
April 2014 Lect.eng. Adriana ALBU, PhD Politehnica University of Timioara
advantages are connected to the facility of modifying something: the changes are made in one place and all the programs that use the header file will automat- ically see this changes when the code is compiled again. This also ensures that all programs use the same version of the code that belongs to a header file. There are several steps that must be followed when applications with user- defined header files are developed. These will be explained creating a library of functions that manipulate a vector of f l oat numbers. First, the header file is created. It will have the extension . h and will contain the prototypes of the functions (without the implementation of that functions), and other definitions if necessary (data types, constants, varia- bles). This is like an interface of the library to the outside programs (this is what the applications know about the library) and programs should use the- se prototypes when they call the functions. The header file myHeader . h of program 12-a.1.1 contains seven func- tions: o mySum( ) , myAver age( ) , myMax( ) , and myMi n( ) re- ceive as argument the vector v, together with its dimension n and return a float; o myCount ( ) receives as arguments the vector v, together with its dimension n, and also a float value x and returns an integer; o pr i nt Lt oR( ) and pr i nt Rt oL( ) receive as argument the vector v, together with its dimension n and doesnt return any- thing. Program 12-a.1.1 the first part: myHeader . h f l oat mySum( i nt n, f l oat v[ ] ) ; f l oat myAver age( i nt n, f l oat v[ ] ) ; i nt myCount ( i nt n, f l oat v[ ] , f l oat x) ; voi d pr i nt Lt oR( i nt n, f l oat v[ ] ) ; voi d pr i nt Rt oL( i nt n, f l oat v[ ] ) ; f l oat myMax( i nt n, f l oat v[ ] ) ; f l oat myMi n( i nt n, f l oat v[ ] ) ; The second step is to create the file that contains the implementation of the functions from the header. It must have the same name as the header file, 12 Files addendum
Lect.eng. Adriana ALBU, PhD Politehnica University of Timioara
April 2014 3
but the extension . c. This source code will include in a preprocessor di- rective the header file it implements and also other header files it needs. It is obvious that this file doesnt contain a mai n( ) function and cannot be run. The source code myHeader . c of program 12-a.1.1 gives the following meanings for the functions of the header file: o mySum( ) calculates and returns the sum of the vectors ele- ments; o myAver age( ) calls the function mySum( ) , divides the val- ue received from it to the number of vectors elements and re- turns the result; o myCount ( ) counts and returns the number of elements that are greater than or equal to a threshold value x received as ar- gument; o pr i nt Lt oR( ) displays the vector from the left to the right; o pr i nt Rt oL( ) displays the vector from the right to the left; o myMax( ) founds and returns the maximum value of the vec- tor; o myMi n( ) founds and returns the minimum value of the vec- tor. Program 12-a.1.1 the second part: myHeader . c #i ncl ude <st di o. h> #i ncl ude " myHeader . h"
f l oat mySum( i nt n, f l oat v[ ] ) { i nt i ; f l oat sum=0; f or ( i =0; i <n; i ++) sum+=v[ i ] ; r et ur n sum; }
f l oat myAver age( i nt n, f l oat v[ ] ) { r et ur n mySum( n, v) / n; Computer Programming The C Language
4
April 2014 Lect.eng. Adriana ALBU, PhD Politehnica University of Timioara
}
i nt myCount ( i nt n, f l oat v[ ] , f l oat x) { i nt i , count er =0; f or ( i =0; i <n; i ++) i f ( v[ i ] >=x) count er ++; r et ur n count er ; }
voi d pr i nt Lt oR( i nt n, f l oat v[ ] ) { i nt i ; f or ( i =0; i <n; i ++) pr i nt f ( " %. 2f " , v[ i ] ) ; }
voi d pr i nt Rt oL( i nt n, f l oat v[ ] ) { i nt i ; f or ( i =n- 1; i >=0; i - - ) pr i nt f ( " %. 2f " , v[ i ] ) ; }
f l oat myMax( i nt n, f l oat v[ ] ) { i nt i ; f l oat m; m=v[ 0] ; f or ( i =1; i <n; i ++) i f ( v[ i ] >m) m=v[ i ] ; r et ur n m; }
f l oat myMi n( i nt n, f l oat v[ ] ) { i nt i ; f l oat m; m=v[ 0] ; f or ( i =1; i <n; i ++) i f ( v[ i ] <m) m=v[ i ] ; r et ur n m; } 12 Files addendum
Lect.eng. Adriana ALBU, PhD Politehnica University of Timioara
April 2014 5
The final step is to create an application that uses this library. It is a . c file and should include in a preprocessor directive the new header file (oth- erwise, it cannot use the librarys functions). The application myMai n. c of program 12-a.1.1 declares and initializes a vector of marks. Then, it displays the average mark (calling the function myAver age( ) ), the number of students that passed the exam (calling the function myCount ( ) with the threshold value 5), and prints the vector ac- cording to a users option regarding the printing direction (left to right or right to left). Program 12-a.1.1 the third part: myMai n. c #i ncl ude <st di o. h> #i ncl ude " myHeader . h"
voi d mai n( voi d) { f l oat mar ks[ ] ={4, 7. 5, 9. 75}; i nt n=3, answer ; pr i nt f ( " The aver age mar k i s: %. 2f " , myAver age( n, mar ks) ) ; pr i nt f ( " \ n%d st udent s passed t he exam" , myCount ( n, mar ks, 5) ) ; pr i nt f ( " \ nThe maxi mum mar k was: %. 2f " , myMax( n, mar ks) ) ; pr i nt f ( " \ nHow do you want t o di spl ay t he r e- sul t s?" ) ; pr i nt f ( " \ n1 - Fr oml ef t t o r i ght " ) ; pr i nt f ( " \ n2 - Fr omr i ght t o l ef t " ) ; pr i nt f ( " \ nAnswer : " ) ; scanf ( " %d" , &answer ) ; i f ( answer ==1) pr i nt Lt oR( n, mar ks) ; el se pr i nt Rt oL( n, mar ks) ; get ch( ) ; }
12-a.2 Questions and exercises A. Find the error. Computer Programming The C Language
6
April 2014 Lect.eng. Adriana ALBU, PhD Politehnica University of Timioara
1. ; 2. ; 3. ; B. Considering the following programs, specify what will be printed on the screen once these programs are executed. 4. ; 5. ; C. Choose the correct answer (one only). 6. a); b); c); d); e). 7. a); b); c); d); e) . 8. a); b); c); d); e). 9. a); b); c); d); e). 10. a); b); c) ; d); e). D. Write a program to solve the following problem. 11. Create a new header file, mySt r i ng. h, which contains several func- tions that operate on strings. For instance: a. i nt i ndexOf ( char *s, char c) returns the index of the first occurrence of a char c in a string s or - 1 if the char doesnt belong to that string; b. i nt l ast I ndexOf ( char *s, char c) returns the index of the last occurrence of a char c in a string s or - 1 if the char doesnt belong to that string; 12 Files addendum
Lect.eng. Adriana ALBU, PhD Politehnica University of Timioara
April 2014 7
c. i nt l enCompar e( char *f i r st , char *second) compares the length of two strings; d. i nt st ar t sWi t h( char *s, char *pr ef i x) veri- fies if a string starts with a specified prefix; e. i nt endsWi t h( char *s, char *suf f i x) verifies if a string ends with a specified suffix; f. voi d r epl aceChar ( char *s, char c1, char c2) replaces a char c1 with another char c2 in a string s; g. voi d subSt r i ng( char *sour ce, i nt st ar t I ndex, i nt endI ndex, char *r esul t ) ex- tracts the substring delimited by two specified indexes.
12-a.3 Answers to questions and exercises A. 1. B. 4. C. 6.