C Tutorials

31. Using the qsort() and bsearch() functions with values - example program in C

By: Manoj Kumar : 2007-10-03

Description: This program lets you enter up to MAX values (20 in this case). It sorts the values and prints them in order. Then it lets you enter a value to search for in the array. A printed message states the search's status.


32. Listing Files and Directories sample program in C

By: Daniel Malcolm : 2007-09-26

Description: A different kind of file system interaction is sometimes called for - determining information about a file, not what it contains. A directory-listing program such as the UNIX command ls is an example - it prints the names of files in a directory, and, optionally, other information, such as sizes, permissions, and so on. The MS-DOS dir command is analogous.


33. A Storage Allocator sample program in C

By: Emiley J : 2007-09-26

Description: In this tutorial let us write a storage allocator which is unrestricted. Calls to malloc and free may occur in any order; malloc calls upon the operating system to obtain more memory as necessary. These routines illustrate some of the considerations involved in writing machine-dependent code in a relatively machine-independent way, and also show a real-life application of structures, unions and typedef.


34. Fopen and Getc implementation program in C

By: Charles : 2007-09-26

Description: Files in the standard library are described by file pointers rather than file descriptors. A file pointer is a pointer to a structure that contains several pieces of information about the file: a pointer to a buffer, so the file can be read in large chunks; a count of the number of characters left in the buffer; a pointer to the next character position in the buffer; the file descriptor; and flags describing read/write mode, error status, etc.


35. Structures and Functions in C

By: Abinaya : 2007-09-26

Description: The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members. Copy and assignment include passing arguments to functions and returning values from functions as well. Structures may not be compared. A structure may be initialized by a list of constant member values; an automatic structure may also be initialized by an assignment.


36. Arrays of Structures example program in C

By: Baski : 2007-09-26

Description: Consider writing a program to count the occurrences of each C keyword. We need an array of character strings to hold the names, and an array of integers for the counts. One possibility is to use two parallel arrays, keyword and keycount, as in char *keyword[NKEYS];


37. Pointers to Structures example program in C

By: Charles : 2007-09-26

Description: To illustrate some of the considerations involved with pointers to and arrays of structures, let us write the keyword-counting program using pointers instead of array indices.


38. Binary Tree - (Self-referential Structures) example program in C

By: Daniel Malcolm : 2007-09-26

Description: One solution is to keep the set of words seen so far sorted at all times, by placing each word into its proper position in the order as it arrives. This shouldn't be done by shifting words in a linear array, though - that also takes too long. Instead we will use a data structure called a binary tree.


39. Table Lookup - hashtab - example program in C

By: Emiley J : 2007-09-26

Description: In this tutorial we will write the innards of a table-lookup package, to illustrate more aspects of structures. This code is typical of what might be found in the symbol table management routines of a macro processor or a compiler. For example, consider the #define statement. When a line like #define IN 1 is encountered, the name IN and the replacement text 1 are stored in a table. Later, when the name IN appears in a statement like state = IN; it must be replaced by 1.


40. typedef example program in C

By: Fazal : 2007-09-26

Description: Notice that the type being declared in a typedef appears in the position of a variable name, not right after the word typedef. Syntactically, typedef is like the storage classes extern, static, etc. We have used capitalized names for typedefs, to make them stand out.