129. What Is A Structure?
Answer: A structure is a collection of data in which all the declared members are public by default. Data security is limited in structure. The general format of declaration of a structure is:
130. What Are The Differences Between A Structure & A Union?
Answer: A structure is a collection of data in which all the declared members are public by default. Data security is limited in structure.
Union is almost the same as the structure as structure contains members of different data types. In structure, each member has its own memory location whereas members of unions have same memory locations. We can assign values to only one member at a time in union.
131. How Can Typedef Be Used to Define A Type Of Structure?
Answer: typedef is used for defining new data types of structures. The general syntax is:
typedef type data_name;
Here, type is the data type and data_name is the name for that type.
For defining structures, typedef is used in the following manner:
Here, data name is another name for defining the structure.
132. What Is A Node? What Is A Linked List In C?
Answer: A node is the component of a linked list. Each node has two parts, first part contains the information field and second part contains the address of the next node. The address part of the last node of linked list will have NULL value.
A linked list is a self referential structure in which a member of a structure points to the structure itself. In other words, a linked list is a collection of elements called nodes.
133. What Is A Self Referential Structure?
Answer: It is a structure that has a member that is of the same type as the structure in which it is defined. A common self-referential structure is a node in a linked-list, which is a member of a set of structures that are linked to one another with pointers. Each node of the linked-list is defined by the structure.
134. Differentiate Between Structure & Array.
Answer: Differences between structure and array are:
- An array is a collection of related data elements of same type. Structure an have elements of different types.
- An array is derived data type whereas a structure is a programmer-defined one.
- Any array behaves like a built-in data type. Declare an array variable field use it. In the case of structure, first, data structure is to be designed and declared before the variables of that type are declared and used.
135. What Is A Union?
Answer: Union is a concept similar to a structure with the major difference in terms of storage. In the case of structures each member has its own storage location, but a union may contain many members of different types but can handle only one at a time. Union is also defined as a structure is done but using the syntax union.
Union var x;
Now x is a union containing three members m,c,a. But only one value can be stored either in x.m, x.c or x.a
136. Define A Structure. How It Is Different From Union?
Answer: A structure contains an ordered group of data objects. Unlike the elements of an array, the data objects within a structure can have varied data types. Each data object in a structure is a member or field. A union is an object like a structure except that
- In union, one block is used by all the member of the union but in case of structure, each member has their own memory space. All of union members start at the same location in memory. A union variable can represent the value of only one of its members at a time.
- The size of the union is equal to the size of the largest member of the union whereas size of the structure is the sum of the size of all members of the structure.
Now if we define struct book book1, then it will assign 1+2+4=7 bytes of memory for book1.
If we define it as union like
The compiler allocates a piece of storage that is large enough to store the largest variable types in union. All three variables will share the same address and 4 bytes of memory is allocated to it.
137. Define A Structure To Store The Following Information About An Employee Name, Sex(male, female), Marital_Status(single, married, divorced or widowed), age.(using bit fields).
Answer: Definition of a structure to store information of an employee:
unsigned sex: 1;
unsigned martial_status: 1;
unsigned age: 7;
138. Explain Pointers & Structures By Giving An Example Of Pointer To Structure Variable?
Answer: We can have a pointer pointing to a structure just the same way a pointer pointing to an int, such pointers are known as structure pointers. For example, consider the following example:
struct student stu,*ptr;
printf("\n Enter data\n");
printf("%s %5d\n",ptr->name,ptr->roll_no); ptr++;
Here ptr is a structure pointer not a structure variable and dot operator requires a structure variable on its left. C provides arrow operator "->" to refer to structure elements. "ptr=stu" would assign the address of the zeroth element of stu to ptr. Its members can be accessed by statement like "ptr->name". When the pointer ptr is incremented by one, it is made to point to the next record, that is stu and so on.
139. What Is Meant By Union?
Answer: Union is almost the same as the structure as structure contains members of different data types. In structure, each member has its own memory location whereas members of unions have same memory locations. We can assign values to only one member at a time in union.
140. What Are The Advantages Of Unions?
Answer: The greatest advantage of union is that it saves memory. When a union is declared, compiler automatically allocates a memory location to hold the largest data type of members in the union, thereby saving memory. Further, the concept of union is useful when it is not necessary to assign the values to all the members of the union at a time.
141. What Are Data Files? State The Two Types Of Data Files? State The Basic Difference Between The Two Data Files?
Answer: Data Files are to store data on the memory device permanently and to access whenever is required. There are two types of data files
- Stream Oriented data files
- System Oriented data files
142. What Are The Different Modes In Which A File Can Be Opened In C Programming?
Answer: Different modes for opening a file are tabulated below:
- "r" Open text file for reading
- "w" Open text file for writing, previous content, if any, discarded
- "a" Open or create file for writing at the end of the file
- "r+" Open text file for update
- "w+" Create or open text file for update, previous content lost, if any
- "a+" Open or create text file for update, writing at the end.
143. In Which Mode If The File Is Opened, The Content Of The File Will Be Lost?
Answer: When the mode is writing, the contents are deleted and the file is opened as a new file.
144. What Is A File? Write The Syntax For File Declaration. What Are Modes In File?
Answer: A file is a collection of data that is available in permanent storage.
syntax: FILE *filepointer;
Ex: FILE *fp;
Mode tells about the types operations like read,write or append that can be performed on a file that is being opened.
145. Write The Syntax To Open A File. What Is The Significance Of fclose() Function?
This function closes a file that has been opened for an operation. Syntax:
Also Read The Following Questions:
61) What is static memory allocation?
62) What is dynamic memory allocation?
63) What is the purpose of realloc?
64) What is pointer to a pointer?
65) What is an array of pointers?
66) Difference between array and pointer?
67) Difference between a array name and a pointer variable?
68) Difference between an array of pointers and a pointer to an array?
69) What are the pointer declarations used in C?
70) Differentiate between a constant pointer and pointer to a constant?
...Return To C Questions INDEX.
...Return To C Programming INDEX.
... Return To HR Interview Index