Presentation is loading. Please wait.

Presentation is loading. Please wait.

Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.

Similar presentations


Presentation on theme: "Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education."— Presentation transcript:

1 Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education - Zulfi Computer Department المملكة العربية السعودية وزارة التعليم العالي جامعة المجمعة كلية التربية بالزلفي قسم الحاسب الآلي Teacher: Chafika LAABIDI OUNI WANNASSI c.ouni@mu.edu.sa

2 Silberschatz and Galvin  1999 1.2 C loops: definition When you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:

3 Silberschatz and Galvin  1999 1.3 Definition (cont)

4 Silberschatz and Galvin  1999 1.4 Types C programming language provides the following types of loop to handle looping requirements. Loop TypeDescription while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body. for loop Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable. do...while loop Like a while statement, except that it tests the condition at the end of the loop body nested loops You can use one or more loop inside any another while, for or do..while loop.

5 Silberschatz and Galvin  1999 1.5 While loop

6 Silberschatz and Galvin  1999 1.6 While loop

7 Silberschatz and Galvin  1999 1.7 For loop

8 Silberschatz and Galvin  1999 1.8 For loop

9 Silberschatz and Galvin  1999 1.9 Do while loop

10 Silberschatz and Galvin  1999 1.10 Do while loop

11 Silberschatz and Galvin  1999 1.11 Header Files A header file is a file with extension.h which contains C function declarations and macro definitions and to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that come with your compiler.

12 Silberschatz and Galvin  1999 1.12 Header Files (cont) You request the use of a header file in your program by including it, with the C preprocessing directive #include like you have seen inclusion of stdio.h header file, which comes along with your compiler.

13 Silberschatz and Galvin  1999 1.13 How to Divide a Program between Several Files Where a function is spread over several files, each file will contain one or more functions. One file will include main while the others will contain functions which are called by others. These other files can be treated as a library of functions

14 Silberschatz and Galvin  1999 1.14 The advantages of this are: The object can easily be re-used in other programs. All related functions are stored together. Later changes to the object require only one file to be modified. How to Divide a Program between Several Files

15 Silberschatz and Galvin  1999 1.15 Include Syntax Both user and system header files are included using the preprocessing directive #include. It has following two forms: #include This form is used for system header files. It searches for a file named file in a standard list of system directories.

16 Silberschatz and Galvin  1999 1.16 Header Files (cont) #include "file" This form is used for header files of your own program. It searches for a file named file in the directory containing the current file.

17 Silberschatz and Galvin  1999 1.17 C – Recursion Recursion is the process of repeating items in a self-similar way. Same applies in programming languages as well where if a programming allows you to call a function inside the same function that is called recursive call of the function as follows.

18 Silberschatz and Galvin  1999 1.18 C – Recursion (cont) The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go in infinite loop. Recursive functions are very useful to solve many mathematical problems like to calculate factorial of a number.

19 Silberschatz and Galvin  1999 1.19 File I/O A file represents a sequence of bytes, does not matter if it is a text file or binary file. C programming language provides access on high level functions as well as low level (OS level) calls to handle file on your storage devices.

20 Silberschatz and Galvin  1999 1.20 Opening Files You can use the fopen( ) function to create a new file or to open an existing file, this call will initialize an object of the type FILE, which contains all the information necessary to control the stream. Following is the prototype of this function call: FILE *fopen( const char * filename, const char * mode );

21 Silberschatz and Galvin  1999 1.21 Opening Files Filename is string literal, which you will use to name your file and access mode can have one of the following values:

22 Silberschatz and Galvin  1999 1.22 Opening Files ModeDescription rOpens an existing text file for reading purpose. w Opens a text file for writing, if it does not exist then a new file is created. Here your program will start writing content from the beginning of the file. a Opens a text file for writing in appending mode, if it does not exist then a new file is created. Here your program will start appending content in the existing file content. r+Opens a text file for reading and writing both. w+ Opens a text file for reading and writing both. It first truncate the file to zero length if it exists otherwise create the file if it does not exist. a+ Opens a text file for reading and writing both. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended.

23 Silberschatz and Galvin  1999 1.23 Closing a File To close a file, use the fclose( ) function. The prototype of this function is: int fclose( FILE *fp ); The fclose( ) function returns zero on success, or EOF if there is an error in closing the file.

24 Silberschatz and Galvin  1999 1.24 Writing a File int fputs( const char *s, FILE *fp ); The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. You can use int fprintf(FILE *fp,const char *format,...) function as well to write a string into a file.

25 Silberschatz and Galvin  1999 1.25 Writing a File Example: #include main() { FILE *fp; fp = fopen("/tmp/test.txt", "w+"); fprintf(fp, "This is testing for fprintf...\n"); fputs("This is testing for fputs...\n", fp); fclose(fp); }

26 Silberschatz and Galvin  1999 1.26 Reading a File char *fgets( char *buf, int n, FILE *fp ); The functions fgets() reads up to n - 1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a null character to terminate the string.

27 Silberschatz and Galvin  1999 1.27


Download ppt "Silberschatz and Galvin  1999 1.1 C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education."

Similar presentations


Ads by Google