by Dinesh Thakur Category: Structure And Union

Union is a collection of data items of different data types. It can hold data of only one member at a time though it has members of different data types. If a union has two members of different data types, they are allocated the same memory.

## C Program to add of two complex numbers

by Dinesh Thakur Category: Structure And Union

Let us use structure complex to represent a complex number and write a simple program containing only the main function. Thus, we can define structure complex either in the main function or before it as a global definition.

## C Program to read and print info of a person

by Dinesh Thakur Category: Structure And Union

The program given below reads the information about a person and prints it on the screen. It uses a structure containing a pointer member name to represent the information about a person. Note that the program uses the dstr_read function.

## Typedef with Nested Structures in C

by Dinesh Thakur Category: Structure And Union

We have used the declaration of nested structures on the parent structure in the same declaration. The structures may also be declared separately and included in the parent structure. The innermost structure should be declared first, then the next enveloping structure and then the next enveloping structure. The code is illustrated in Program.

## Manipulation of Complex Numbers by Structures

by Dinesh Thakur Category: Structure And Union

A complex number is also a multivalue object. It consists of two parts: real part and imaginary part. The imaginary part carries symbol i which is equal to √-1 . A complete number is written as sum of the real part and the imaginary part as shown below.

## Union in c

by Dinesh Thakur Category: Structure And Union

As we already know, a structure is an ordered list of elements of either the same or different types. A union is similar to a structure but with one major difference that the structure stores all its members one after another, whereas the union can store only one member at a time, since all the members in a union are stored beginning from the same memory location. Thus, the total memory required to store a union is the same as that required for the largest member in it. The union is useful when we have to store one of the alternative values of different types and yet conserve space.

## Typedef in Structure Declaration in C

by Dinesh Thakur Category: Structure And Union

In this method, the declaration starts with typedef followed by key word struct. The data members of the structure are placed between a pair of curly braces after struct. The type name is placed after the closing right brace (}). This is explained in the following example:

## Sorting of Structures in C

by Dinesh Thakur Category: Structure And Union

When structures are used to keep record of employees, students, and so on, we would need to sort them according to some criterion. For employees, it may be according to alphabetical order, designation, or pay.

## C Program to Pointers to structures

by Dinesh Thakur Category: Structure And Union

Pointers to structures may be declared as we declare pointers to any other data type. Like arrays and functions, the name of structure carries the address of the structure where the values of its various members are stored. Therefore, the pointer may be initialized by the name of structure which is a constant pointer to the structure. Program illustrates the declaration and use of pointer to a structure.

## C Program to Structure as a Parameter of Functions

by Dinesh Thakur Category: Structure And Union

Program provides an illustration of a function with structure as one of its parameter. The function calculates and returns the magnitude of a vector which is an instance of structure defined to hold the three components of a vector as its data members. The structure is declared as below.

## C program to count the number of characters, words and lines in a text file

by Dinesh Thakur Category: File Handling

Let us use a while loop in conjunction with the getc macro to read a text file character by character as explained in Program. The counting of characters and lines is very straight forward, the code for which is given below.

## The fgets and fputs Functions in C

by Dinesh Thakur Category: File Handling

The fgets function reads a sequence of character, i. e., a character string from an input stream. Its prototype is given below.

## File management in C

by Dinesh Thakur Category: File Handling

The getw and putw functions, The fprintf & fscanf functions, Random access to files and fseek function. C supports a number of functions that have the ability to perform basic file operations, which include:

## Function putw()and getw() in C

by Dinesh Thakur Category: File Handling

Function putw()

This function writes an integer (whole number) into a file. The prototype of the function is as shown below.

## Error-Handling functions in C

by Dinesh Thakur Category: File Handling

Function clearerr ()

The function prototype is as given below.

void(clearerr(FILE* Stream);

## C Program to use ferror, perror and fputs

by Dinesh Thakur Category: File Handling

To read a string in a file we can use fgets () whose prototype is:

char * fgets (char * str, int size, FILE * fp);

## C Program to Text file containing prime numbers in a given range

by Dinesh Thakur Category: File Handling

The fscanf () function works as scanf (). The difference is that fscanf () reads a file and not the computer keyboard. prototype:

int fscanf (FILE * fp, char * str, ...);

## What is Streams?

by Dinesh Thakur Category: File Handling

The devices used with a computer, such as a keyboard, monitor, printer, hard disk, magnetic tape, etc., have widely varying properties regarding data input and output. To simplify data I/O operations, the C standard library supports a simple mode of input and output based on the concept of a stream.

## The ungetc Function in C

by Dinesh Thakur Category: File Handling

If a character read from an input stream is not appropriate for the current operation, we can push it back to the stream using the ungetc function. A subsequent character read operation (such as fgetc or getc) on that stream will return this character. The prototype of the ungetc function is given below.

## C program to fputc Function and the putc Macro

by Dinesh Thakur Category: File Handling

The fputc function and putc macro are used to write a character to an output stream. Their prototypes are given below.

Page 14 of 16