0% found this document useful (0 votes)
3 views

C_Module-5

Uploaded by

mraj43621
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C_Module-5

Uploaded by

mraj43621
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

MODULE-5

1. What approach on data structure would you use to store a collection of different data type
in a programming language?

a) a) String b) Structure c) Char d) None of the above

Answer: b) Structure

2. Predict the operator that connects the structure name to its member name?

a) a) . b) - c) -> d) both .and ->

Answer: c) ->

3. What is the output of this code?

struct student

};

void main()

struct student s[2];

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?

a) Nothing b) EOF c) NULL d) Depends on compiler

Answer: c) NULL

5. Which of the following share a similarity in syntax?


a) a) 1 and 2 b) 3 and 4 c) 1,2 and 4 d) Only 1
Answer: a) 1 and 2
6. What is the output of this C code? struct student { }; void main() { struct student s[2];
printf("%d", sizeof(s)); }
Answer: 0

7. What will happen when the structure is declared?

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.

Answer: a) it will not allocate any memory

8. Can you mention the type of files which can't be opened using fopen()?

a) a).txt b) .bin c) .c d) database

Answer: d) database

9. What is the approach which is used to open an existing file for both reading and writing?

Answer: c) ”R+”

10. Predict the function which is used to write a string to a file?

Answer: fputs()

Short Answer Type Questions

1. 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.
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.

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.

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
{

2. How would you define Union? Explain with an example.

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;

Data- type member n; };


Example:

union Item

{ int x;

float y;

char z; } Alpha;

3. How would you differentiate structure and Union?


● Answer: You can use a struct keyword to define a structure.
● You can use a union keyword to define a union.
● Every member within structure is assigned a unique memory location.
● In union, a memory location is shared by all the data members.
● Changing the value of one data member will not affect other data members in structure.
● Changing the value of one data member will change the value of other data members in union.
● It enables you to initialize several members at once.
● It enables you to initialize only the first member of union.
● The total size of the structure is the sum of the size of every data member.
● The total size of the union is the size of the largest data member.
● It is mainly used for storing various data types.
It is mainly used for storing one of the many data types that are available.

4. Can you explain getc functions?


Answer: The getc and putc Functions
The simplest file I/O functions are getc and pute. These are analogous to getehar and putchar
functions and handle one character at a time. Assume that a file is opened with mode w and file
pointer fp1. Then, the statement
putc(c, fp1);
writes the, character contained in the character variable c to the file associated with FILE pointer
fpl. Similarly, getc is used to read a character from a file that has been opened in read mode. For
example, the statement
c = getc(fp2);
would read a character from the file whose file pointer is fp2.
The file pointer moves by one character position for every operation of getc(). The getc() will
return an end-of-file marker EOF, when end of the file has been reached. Therefore, the reading
should be terminated when EOF is encountered.

5. Demonstrate the different mode of operations performed on file?

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.

6. How would you access individual members of the structure?

Answer: THE DOT OPERATOR


In order to gain access to a member of a structure, the dot operator "." is used. That is a structure
member can be accessed by writing.
structure_name. member_name
Where structure_name refers to the name of a structure type variable and member _name refers to
the name of member with in the structure. The period (.) is an operator and it is a member of the
highest precedence group and it groups from left to right.

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( );
}

7. How would you explain pointer expressions ?


Answer: Like other variables, pointer variables can be used in expressions. For example, if pl and
p2 are properly declared and initialized pointers, then the following statements are valid.
y = *p1 * *p2;
sum = sum +- *p1;
z = 5* - *p2 / *p1;
*p2 = *p2 + 10;
Note that there is a blank space between / and * in the item3 above. The following is wrong.
z = 5* - *p2 /*p1;
The symbol /* is considered as the beginning of a comment and therefore the statement fails.
C allows us to add integers to or subtract integers from pointers, as well as to subtract one pointer
from another.
pl + 4, p2 - 2 and p1 - p2 are all allowed.
If pl and p2 are both pointers to the same array, then p2 – p1 gives the number of elements between
pl and p2.
We may also use short-hand operators with the pointers.
p1++;
--p2;
sum += *p2;

Long Answer Type Questions

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

fprintf() – To write into a file: Declaration:


int fprintf(FILE *fp, const char *format, …);
fprintf() function writes string into a file pointed by fp. In a C program, we write string into a file
as below.
fprintf(fp,“some data”);or
fprintf (fp, “text %d”, variable_name);

– 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;

4. Demonstrate how to define pointers? Explain with syntax and example.

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.

In call by value we simply passing the value to the variable .


Call by reference:
Here both actual and formal parameters refer to same memory location. Therefore, any changes
made to the formal parameters will get reflected to actual parameters.
Here instead of passing values, we pass addresses.

You might also like