Programming Tutorials

C Tutorials

41. union example program in C

By: Grenfel : 2007-09-26

Description: A union is a variable that may hold (at different times) objects of different types and sizes, with the compiler keeping track of size and alignment requirements. Unions provide a way to manipulate different kinds of data in a single area of storage, without embedding any machine-dependent information in the program. They are analogous to variant records in pascal.


42. Using Bit-field in C

By: Henry : 2007-09-26

Description: When storage space is at a premium, it may be necessary to pack several objects into a single machine word; one common use is a set of single-bit flags in applications like compiler symbol tables. Externally-imposed data formats, such as interfaces to hardware devices, also often require the ability to get at pieces of a word.


43. Standard Input and Output in C

By: Ivan Lim : 2007-09-26

Description: The C library implements a simple model of text input and output. A text stream consists of a sequence of lines; each line ends with a newline character. If the system doesn't operate that way, the library does whatever necessary to make it appear as if it does. For instance, the library might convert carriage return and linefeed to newline on input and back again on output.


44. Using printf function in C

By: Jagan : 2007-09-26

Description: The output function printf translates internal values to characters. We have used printf informally in previous chapters. The description here covers most typical uses but is not complete; int printf(char *format, arg1, arg2, ...); printf converts, formats, and prints its arguments on the standard output under control of the format. It returns the number of characters printed.


45. Variable-length Argument Lists sample program in C

By: Lakshmi : 2007-09-26

Description: This tutorial contains an implementation of a minimal version of printf, to show how to write a function that processes a variable-length argument list in a portable way. Since we are mainly interested in the argument processing, minprintf will process the format string and arguments but will call the real printf to do the format conversions.


46. scanf and sscanf sample program in C

By: Manoj Kumar : 2007-09-26

Description: scanf reads characters from the standard input, interprets them according to the specification in format, and stores the results through the remaining arguments. The format argument is described below; the other arguments, each of which must be a pointer, indicate where the corresponding converted input should be stored. As with printf, this section is a summary of the most useful features, not an exhaustive list.


47. File read and write - sample program in C

By: Norman Chap : 2007-09-26

Description: The objective of this tutorial to write a program that accesses a file that is not already connected to the program. One program that illustrates the need for such operations is cat, which concatenates a set of named files into the standard output. cat is used for printing files on the screen, and as a general-purpose input collector for programs that do not have the capability of accessing files by name. For example, the command cat x.c y.c


48. Error Handling - Stderr and Exit - sample program in C

By: Priya : 2007-09-26

Description: The treatment of errors in cat is not ideal. The trouble is that if one of the files can't be accessed for some reason, the diagnostic is printed at the end of the concatenated output. That might be acceptable if the output is going to a screen, but not if it's going into a file or into another program via a pipeline.


49. fgets(), fputs() - Line Input and Output - sample program in C

By: Reema sen : 2007-09-26

Description: fgets reads the next input line (including the newline) from file fp into the character array line; at most maxline-1 characters will be read. The resulting line is terminated with '\0'. Normally fgets returns line; on end of file or error it returns NULL. (Our getline returns the line length, which is a more useful value; zero means end of file.)


50. malloc, calloc - Storage Management - in C

By: Sam Chen : 2007-09-26

Description: The functions malloc and calloc obtain blocks of memory dynamically.