0% found this document useful (0 votes)
6 views6 pages

2 Marks

The document provides an overview of structures, unions, and file handling in C programming. It defines structures, nested structures, and arrays of structures, highlighting their differences from arrays and unions. Additionally, it covers file types, modes, and functions for file input/output operations, including the use of EOF and file pointer manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

2 Marks

The document provides an overview of structures, unions, and file handling in C programming. It defines structures, nested structures, and arrays of structures, highlighting their differences from arrays and unions. Additionally, it covers file types, modes, and functions for file input/output operations, including the use of EOF and file pointer manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

VELAMMAL ENGINEERING COLLEGE

UNIT V
2 marks
1.Define structure with examples.
A Structure is a collection of one or more values of different data types, grouped
together under a single name.
struct student
{
int rollno;
char name[25];
int marks[6];
int avg;
}s1;

2. How is a structure different from an array?


S.No Array Structure
1. Array refers to a collection Structure refers to a collection
consisting of elements of consisting of elements of
homogenous data type. heterogenous data type.
2. Array elements are accessed Structure members are accessed
using index value of array using dot operator
3. Array elements are stored in Structure elements may or may
continuous memory not be stored in a continuous
locations. memory location.
4. Array size is fixed and is Structure size is not fixed as each
basically the number of element of Structure can be of
elements multiplied by the different type and size.
size of an element.

3. What is nested structure?Give example.


A Structure can be used inside another structure are called nested structure.

Structure 1: stu_address
struct stu_address
{
int street;
char *state;
char *city;
char *country;
}
Structure 2: stu_data
struct stu_data
{
int stu_id;
int stu_age;
char *stu_name;
struct stu_address stuAddress;
}

4. What is array of structure?Give example.


The array of structures in C are used to store information about multiple entities of
different data types. The array of structures is also known as the collection of
structures.
struct student
{
int rollno;
char name[10];
} st[5];

5. Will the following declaration work. Justify your answer.


struct Student
{
int rollno=12;
float marks[]={55,60,56};
char gender;
};

The structure members are not allowed to initialize inside the structure
definition.The menmbers of structure are initialized using structure variable.

6.What is the use of typedef keyword?


typedef is a keyword used in C language to assign alternative names to
existing datatypes.
Its mostly used with user defined datatypes, when names of the datatypes become
slightly complicated to use in programs.
Syntax:
typedef data_type new_name;

Example:
typedef int myint;

Now myint is an alias of int. From now on we can declare new int variables
using myint instead of int keyword.

myint i = 0;

7.Define union with example.


Union is a variable, which is similar to the structure. It contains data elements of
different data types. The Union requires bytes that are equal to the number of bytes
required for the largest members.

The syntax is as follows:


union result
{
int marks;
int grade;
}u1;

Here the memory allocation is 2 bytes, whereas if it is a structure the memory


allocation is 4 bytes.

8. How Structure differ from union?


S.No Structure Union
1. The keyword is struct. The keyword is union.
2. Memory allocation is done Memory allocation is done for
for all the data members in the data member which requires
the structures. maximum allocations.
Example. Example:
struct student union student
{ {
int rollno; int rollno;
char name[5]; char name[5];
}s1; }u1;
The memory allocation is 7 The memory allocation is 5
Bytes. Bytes
3. All the data members are Only the last stored data element
available in the primary is available in the primary
memory at any time of memory at any time of
execution. execution.
4. Since memory is allocated Since memory is not allocated
for all the data members, no for all the data member, only one
data is deleted in the primary data is available and other data is
memory deleted from the primary
memory

9. What is the need of files?List its types.


A fi le is a collection of bytes stored on a secondary storage device, which is
generally a disk of some kind. It is identified by a name, which is given at the time
of its creation.

Files are two types


1.Text files
2.Binary files

1.Text File:
The data are stored as plain text.
2.Binary Files:
The data are stored in the binary form (0's and 1's).
They can hold a higher amount of data, are not readable easily, and provides better
security than text files.

10.List the different files modes.

S.NO MODE MEANING


1 r Open a text file for reading
2 w Create a text file for writing
3 a Append to a text file
4 rb Open a binary file for reading
5 wb Open a binary file for writing
6 ab Append to a binary file
7 r+ Open a text file for read/write
8 w+ Create a text file for read/write
9 a+ Append or create a text file for read/write
10 r+b Open a binary file for read/write
11 w+b Create a binary file for read/write
12 a+b Append a binary fi le for read/write

11. List out the functions used to perform file input and output operation.
Writing to a File:

● fputc(char, file_pointer): It writes a character to the file pointed to by


file_pointer.
● fputs(str, file_pointer): It writes a string to the file pointed to by
file_pointer.
● fprintf(file_pointer, str, variable_lists): It prints a string to the file pointed
to by file_pointer. The string can optionally include format specifiers and a
list of variables variable_lists.

Reading data from a File:

● fgetc(file_pointer): It returns the next character from the file pointed to by


the file pointer
● fgets(buffer, n, file_pointer): It reads n-1 characters from the file and stores
the string in a buffer in which the NULL character '\0' is appended as the last
character.
● fscanf(file_pointer, conversion_specifiers, variable_adresses): It reads
characters from the file and assigns the input to a list of variable pointers
variable_adresses using conversion specifiers. Keep in mind that as with
scanf, fscanf stops reading a string when space or newline is encountered.

12.How you move to the specific location in a file.


The fseek( ) function moves the file pointer to the specific position in
the file at the specified byte.
Syntax:
fseek( file pointer, displacement, pointer position);
Example:
fseek( p,10L,0)
0 means pointer position is on beginning of the file,from this statement pointer
position is skipped 10 bytes from the beginning of the file.

13.How will you get the current position of file pointer ?


The ftell( ) returns the value of the current position of the file pointer in the
file.The value is count from the beginning of the file.
Syntax: ftell(fptr);
Where fptr is a file pointer

14.What is the need of rewind( )?


This function is used to move the file pointer to the beginning of the file.

Syntax:
rewind( fptr);

15. What is the difference between fgets() and gets() ?

S.No gets() fgets()


1. The function gets() is The function fgets() is used to
normally used to read a line read a line of string from a fi le
of string from the keyboard. or keyboard.
2. It automatically replaces the It does not automatically delete
‘\n’ by ‘\0’. the trailing ‘\n’
3. It takes one argument. It takes three arguments.
4. It does not prevent overflow. It prevents overflow.

16. What is EOF? When is EOF used?

EOF means end of file.When reading from a file, the program detect that it has
reached the end of the File. One way is to have a special marker at the end of the
file (EOF).It is used

You might also like