C Programming Question With Answers
- Posted by Engineering Helpline Pvt. Ltd
- Categories c programming, C Programming Question With Answers, First Semester
- Date April 1, 2018
- Comments 0 comment
111.When are array of structure used? Illustrate the difference in passing a single structure and array of structure to a function with example.
When it is required to work with list of records containing various information, array of structure are used. The following table shows the difference that can be seen while passing a single structure and passing array of structure. Let us consider a function name record() and a structure named profile, then
|
SN
|
Section
|
Passing single structure
|
Passing array of structure
|
|
1.
|
Variable declaration
|
struct profile p;
|
struct profile p[20];
|
|
2.
|
Function Declaration
|
void record(struct profile);
|
void record(struct profile[]);
|
|
3.
|
Function call
|
record(p);
|
record(p);
|
|
4.
|
Function definition
|
void record(struct profile x){ }
|
void record(struct profile y[]){ }
|
112.What are the ways of returning an array of structure from a called function to the calling
function?
The following methods can be used to return array of structures to the calling function.
•Pass the array of structure, which needs to be returned, as an argument in function call.
▪Let us take an example, where there are 50 records of an employee and a function should return the records whose salary is greater than 20000. Since the number of records to be returned is not known, it is better to pass the array from the calling function to list the required records. Also the count of records will be required which can be returned through return statement. The different section of function looks like:
54
oVariable declaration: struct employee e1[50], e2[50];
oFunction declaration: int salary(struct employee[], struct employee[]);
oFunction Call: count = salary(e1, e2);
oFunction Definition: int salary(struct employee x[], struct employee y[]){ }
•Return the reference of the arrays of structure from the called function to the calling function through return statement.
113.Explain briefly about the nested structure along with its syntax.
Whenever there is a presence of structure as a member of another structure then it forms a nested structure. Also when one structure is declared inside of another structure then a nested structure is formed. The following example illustrates the concept of nested structure.
|
Nested Structure 1
|
Nested Structure 2
|
|
struct profile
|
struct date
|
|
{
|
{
|
|
char name[20];
|
int mm,dd,yy;
|
|
int age;
|
};
|
|
struct date
|
struct profile
|
|
{
|
{
|
|
int mm, dd, yy;
|
char name[20];
|
|
}dob;
|
int age;
|
|
};
|
struct date dob;
|
|
};
|
Variable declaration: struct profile p;
Accessing members: p.age, p.dob.mm, p.dob.yy; the member of structure date should be accessed through its own variable.
114.Illustrate the difference between structure and union.
The differences between structure and union are:
|
SN
|
Structure
|
Union
|
|
1.
|
Keyword struct is used
|
Keyword union is used
|
|
2.
|
Each member is allocated a memory
|
Memory is allocated based on the size of the
|
|
space. The size of structure equals the
|
highest space requirement. Its size is equal
|
|
|
sum of sizes of its member.
|
to the size of largest member.
|
|
|
3.
|
Each member within a structure is
|
Memory allocated is shared by individual
|
|
55
|
|
assigned unique memory location
|
members of union
|
|
|
4.
|
Altering the value of a member will not
|
Altering value of any of the member will
|
|
affect other members
|
change other member values
|
|
|
5.
|
Each member can be accessed at a time
|
Only one member can be accessed at a time
|
115.What is a file? Why are files required?
Sometimes information can be stored in the auxiliary memory devices, which is in the form known as data files.
Files are required
•To store data permanently
•To access and alter the information whenever necessary
116.Explain briefly about the structure FILE and FILE pointer.
FILE structure provides the necessary information about a FILE or stream which performs input
and output operations. File structure is defined in stdio.h header file. A pointer of type FILE is a FILE pointer, which is used to represent the file to be worked with.
117.Describe different modes of opening a file.
The different modes of opening a file are listed as:
|
SN
|
Mode
|
Description
|
|
1.
|
“r”
|
Opens a file for reading, but the file must exist.
|
|
2.
|
“w”
|
Creates an empty file for writing. If the file already exists, then the content will
|
|
be overwritten.
|
||
|
3.
|
“a”
|
Adds data at the end of the file. Also creates a file, if file does not exist
|
|
4.
|
“r+”
|
Opens a file for both reading and writing. The file must exist.
|
|
5.
|
“w+”
|
Creates an empty file for both reading and writing.
|
|
6.
|
“a+”
|
Opens a file for reading and appending.
|
118.Where will be the position of the FILE pointer for different file opening modes?
The position of the FILE pointer will be at the beginning for reading and writing modes whereas
the FILE pointer will be positioned at the end of the file for append mode.
119.How can we access a file randomly? Write a one line code for the following access.
▪Accessing 23rd byte from the beginning of the file
▪Accessing 5th block of a file from the start of a file.
56
The function fseek() is used to access the file randomly. It is used to position the file pointer at the specified location as required.
To access 23rd byte from the beginning of the file: fseek(fp, 23, SEEK_SET);
To access 5th block of a file from the start of a file: fseek(fp, 5*size_of_block, SEEK_SET);
120.What is error handling? Why is it done?
Error Handling refers to the anticipation, detection and resolution of programming errors. Error handling is done to prevent unexpected termination of the program.
You may also like
Applied mechanis And Its Solutions
6 January, 2020
c programming notes
17 April, 2019
Structure: Array VS Structure: Pointer and Structure:
4 February, 2019
A structure is a collection of variable (of different data types) referenced under one name, providing of convenient means of keeping related information together. A structure definition forms a template that may be used to create structure variable. The general …
