C_Module-5
C_Module-5
1. What approach on data structure would you use to store a collection of different data type
in a programming language?
Answer: b) Structure
2. Predict the operator that connects the structure name to its member name?
Answer: c) ->
struct student
};
void main()
printf("%d", size
of (s));
a)2 b) 4 c) 8 d) 0
Answer: d) 0
4. What would happen if there is any error while opening a file, fopen will return?
Answer: c) NULL
a) a) it will not allocate any memory b) it will allocate the memory c) it will be declared and initialized
d) none of the above.
8. Can you mention the type of files which can't be opened using fopen()?
Answer: d) database
9. What is the approach which is used to open an existing file for both reading and writing?
Answer: c) ”R+”
Answer: fputs()
Answer: Such an entity is called structure and the related data items use in it are referred to as
members. Thus a single structure might contain integer elements, floating point elements and
character elements. Pointers, arrays and other structures can also be included as elements with in
a structure.
Give the general format used for defining a structure
DEFINING A STRUCTURE
Every structure must be defined and declared before it appears in a C program.
The general form of structure definition and declaration is as follows,
struct tagname
{
data-type member_1 ;
data-type member_2;
data-type member_n;
};
In this declaration struct is a required keyword ,tagname is a name of the structure and member_1
,member_2, member_n are individual member declarations.
Demonstrate how to define structure? Explain with an example.
Answer : Such an entity is called structure and the related data items use in it are referred to as members.
Thus a single structure might contain integer elements, floating point elements and character elements.
Pointers, arrays and other structures can also be included as elements with in a structure.
DEFINING A STRUCTURE
struct tagname
{ data-type member_1 ;
data-type member_2;
data-type member_n;
};
In this declaration struct is a required keyword ,tagname is a name of the structure and member_1
,member_2, member_n are individual member declarations.
The individual members can be ordinary variables, pointers, arrays or other structures. The
member names with in a particular structure must be distinct from one another though a member
name can be same as the name of a variable defined outside of the structure. Example:
struct Student
{
Answer: Union like structures, contains members whose individual data type may differ from one
another. The difference between union and structure the member that compose a union all share
the same storage area within the computer's memory. Where as in structures, each member has its
own storage location. This implies that, although a union may contain many members of different
types, it can handle only one member at a time. They are useful for applications involving multiple
members where values need not be assigned to all of the members at anyone time. Thus unions are
used to conserve memory. The general form of union is
union tagname {
Data-type member 1;
Data-type member_2;
union Item
{ int x;
float y;
char z; } Alpha;
Answer:
There are many modes in opening a file. Based on the mode of file, it can be opened for reading
or writing or appending the texts. They are listed below.
r – Opens a file in read mode and sets pointer to the first character in the file. It returns null if file
does not exist.
● w – Opens a file in write mode. It returns null if file could not be opened. If file exists, data are
overwritten.
● a – Opens a file in append mode. It returns null if file couldn’t be opened.
● r+ – Opens a file for read and write mode and sets pointer to the first character in the file.
● w+ – opens a file for read and write mode and sets pointer to the first character in the file.
● a+ – Opens a file for read and write mode and sets pointer to the first character in the file. But, it
can’t modify existing contents.
Example:
#include<stdio.h>
#include<conio.h>
Void main( )
{
struct employee {
long int code;
char name[20]; float salary;
}
struct employee emp;
clrscr( );
printf(" Enter employee code:");
scanf("%d" ,&emp.code); fflush(stdin);
printf("Enter employee name:"); gets(emp.name);
fflush(stdin); printf("Enter salary:"); s
canf("%f' ,&emp.salary);
printf("EMPLOYEE INFORMATION SYSTEM\n\n\n"); printf("CODE NAME SALARY\n");
printf("%d\t %s\t %f\n",emp.code, emp.name, emp.salary);
getch( );
}
1. How would you explain the different basic file operations in C and provide examples of
how each operation is used in a program.
Answer: There are 4 basic operations that can be performed on any files in C programming
language. They are,
1. Opening/Creating a file
2. Closing a file
3. Reading a file
4. Writing in a file
open() – To open a file : Declaration: FILE *fopen (const char *filename, const
char *mode)
fopen() function is used to open a file to perform operations
such as reading, writing etc. In a C program, we declare a
file pointer and use fopen() as below. fopen() function
creates a new file if the mentioned file name does not exist.
FILE*fp;
fp=fopen (“filename”, ”‘mode”);
Where,
fp – file pointer to the data type “FILE”. Filename size of the buffer fp – file pointer
– the actual file name with full path of the file. mode –
refers to the operation that will be performed on the file.
Example: r, w, a, r+, w+ and a+. Please refer below the
description for these mode of operations.
fclose()-To close a file: Declaration: int fclose(FILE *fp);
fclose() function closes the file that is being pointed by file
pointer fp. In a C program, we close a file as below.
fclose(fp);
fgets() – To read a file: Declaration: char *fgets(char *string, int n, FILE *fp) fgets function is
used to read a file line by line. In a C program,we use fgets function as below. fgets (buffer,
size, fp);
2. Can you design a program in C to open, write and close the file?
Answer:
/ * Open, write and close a file : */ # include <stdio.h>
# include <string.h>
int main( )
{
FILE *fp ; char data[50];
// opening an existing file
printf( "Opening the file test.c in write mode" ) ; fp = fopen("test.c", "w") ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ; return 1;
}
printf( "\n Enter some text from keyboard” \ “ to write in the file test.c" ) ;
// getting input from user
while ( strlen ( gets( data ) ) > 0 )
{
// writing in the file fputs(data, fp) ; fputs("\n", fp) ;
}
// closing the file printf("Closing the file test.c") ; fclose(fp) ;
return 0;
}
3. What example you can find in C program to open, read and close the file?
Answer: /* Open, Read and close a file: reading string by string */
# include <stdio.h> int main( )
{
FILE *fp ; char data[50] ;
printf( "Opening the file test.c in read mode" ) ; fp = fopen( "test.c", "r" ) ;
if ( fp == NULL )
{
printf( "Could not open file test.c" ) ; return 1;
}
printf( "Reading the file test.c" ) ; while( fgets ( data, 50, fp ) != NULL ) printf( "%s" , data ) ;
printf("Closing the file test.c") ; fclose(fp) ;
return 0;
Answer: The pointer in C language is a variable which stores the address of another variable.
This variable can be of type int, char, array, function, or any other pointer.
Syntax of declaring pointer variable:
data_type *pointer_varible_name;
Example:
void main()
{
int a=10; //simple integer varible
int *p; //pointer variable can store the address of another integer variable
p=&a; //assigning or copying address of a to the pointer variable p printf(“Value of variable
a=%d”,a);
printf(“ Address of variable a=%u”, p);
}
5. Demonstrate how to declare and define call by value and call by reference with
examples.
Answer: Actual Parameters are the parameters passed to a function. Formal Parameters are the
parameters received by the function
Call by value:
Here values of actual parameters will be copied to formal parameters and these two different
parameters store values in different locations.
Here variable x=10 and y=20
When fun(x,y); is called values of x and y will be passed passed to the function definition
i.e. int fun(int x,int y)
But inside the function we are changing the value of x to 20 and y to 10.