You are on page 1of 5

1 2

FOCS- PART-A ( 2 MARKS QUESTIONS AND ANSWERS) OUTPUT


Enter two integers to add
1.Define pointer to pointer with an example. 6
5
Pointer to a pointer is a form of multiple indirection or a chain of pointers. Normally, Sum of the numbers = 11
a pointer contains the address of a variable. When we define a pointer to a pointer,
the first pointer contains the address of the second pointer, which points to the @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
location that contains the actual value as shown below.

3.Single-Dimension Arrays
The general form for declaring a single-dimension array is

type var_name[size];

 int a[10]; // array ‘a’ has 10 elements of type int, we would write
 double balance[100]; // a 100-element array called balance of type double
balance[3] = 12.23; //assigns element number 3 in balance array the value
A variable that is a pointer to a pointer must be declared as such. This is done by 12.23
placing an additional asterisk in front of its name. For example, following is the  char name[10]; // name is the character array variable that can hold a
declaration to declare a pointer to a pointer of type int – maximum of 10 characters

Syntax : int **variablename;


@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 4.Create a structure of an employee with 4 fields.

2.Build a C program to add 2 numbers using pointers. Structure is another user defined data type available in C that allows to combine data
items of different kinds. Structures are used to represent a record
#include <stdio.h>
int main() struct employee
{ {
int first, second, *p, *q, sum; char name[30];
printf("Enter two integers to add\n"); int empId;
scanf("%d%d", &first, &second);
float salary;
p = &first;
q = &second; long int adar number;
sum = *p + *q;
printf("Sum of the numbers = %d\n", sum); };
return 0; struct employee emp1, exp2,
}
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
3 4

5. What is meant by typedef: there is specific memory Stores a single value at


typedef is simply a way of giving a new name to an existing data type. In other words, location for each input data a time for all members.
it is a reserved keyword to create an alias name for a specific data type. The Value storage member and hence it can
function or purpose served by the data type remains unchanged. We use it to store multiple values of the
simplify the code for the programmer's benefit while declaring higher-level data different members.
types such as structures and unions. Syntax of declare a Structure The syntax of declare a
in C is as follows − Union in C is as follows
syntax of typedef Keyword struct structname union unionname
typedef <existing_name> <alias_name>; { {
type element1; type element1;
Syntax
In the above syntax, existing_name is the name of an existing variable or data type, type element2; type element2;
. .
whereas 'alias_name' is another name assigned to it.
. .
} }
variable1, variable2, ...; variable1, variable2, ...;

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

7.List out the benefits of pointers.

Benefits
1. Pointers can be used to return multiple values from a function via function
arguments.
2. pointers permit references to functions and thereby facilitating passing of
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ functions via function arguments.
6. Distinguish structure and union. 3. the use of pointer arrays to character strings results in saving of data storage
space in Memory.
4.They allow C to support dynamic memory management. They provide an
Key Structure Union
efficient tool for manipulating dynamic data structures such as structures , linked lists
Separate memory location Memory is allocated , queues , stacks and trees.
allotted for each input only to one member
Internal @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
member. having largest size
implementation
among all other input
variables.
size of a Structure is equal or Size or equal to the size
greater than the sum of size of largest member
Size
of all the data members. among all data
members.
5 6

8.Outline bit-fields with an example. 10..What is meant by stdin, sdtout and stderr.

Bit fields can be used to reduce memory consumption when a program requires a
number of integer variables which always will have low values: Pointer Stream

Syntax of C Bit Fields stdin Standard input

struct stdout Standard output

{ stderr Standard error


data_type member_name : width_of_bit-field;
};
Standard input - standard input is a stream from which a program reads its input
where,
data. The program requests data transfers by use of the read operation
 data_type: It is an integer type that determines the bit-field value which is to be
interpreted. The type may be int, signed int, or unsigned int. Standard output - Standard output is a stream to which a program writes its output
 member_name: The member name is the name of the bit field. data. The program requests data transfer with the write operation.
 width_of_bit-field: The number of bits in the bit-field. The width must be less
than or equal to the bit width of the specified type. Standard error - Standard error is another output stream typically used by programs
to output error messages or diagnostics..
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
9. Dereferencing of Pointer
11. Show the declaration and Initialization of C Pointer variable
Once a pointer has been assigned the address of a variable, how to access the value
of the varaible using the pointer? This is done by using another unary operator
Pointer is a variable used to store the address of another variable or a memory
known as the indirection operator. Another name for the indirection operator is the
location. The datatype of the pointer and the variable to which the pointer variable is
derefencing operator.
pointing must be the same.

Following are some examples of declaring a pointer in C:


example
int *ptr; //pointer to int
int quantity, *p, n;
float *ptr; //pointer to float
quantity = 179;
char *ptr; //pointer to char
p=&quantity;
double *ptr; //pointer to double
n=*p; // n is the value stored at address of quantity
Initialisation
can be written as
int a = 10;
n=*(&quantity); //called dereferencing pointer
int *ptr; //pointer declaration
ptr = &a; //pointer initialization
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
7 8

12.What is meant by lseek command. 14.What is meant by file descriptors in Unix OS.
 A file descriptor is a unique identifier or reference that the operating system
lseek() system call repositions the read/write file offset i.e., it changes the positions
assigns to a file when it is opened.
of the read/write pointer within the file. In every file any read or write operations  It allows programs to interact with files, sockets, or other input/output (I/O)
happen at the position pointed to by the pointer. lseek() system call helps us to resources.
manage the position of this pointer within a file.  The file descriptor is used by the operating system to keep track of the file and
perform operations on it.
Examples:  File descriptors are typically represented as non-negative integers.
 lseek(fd,5,SEEK_SET) – this moves the pointer 5 positions ahead starting from the
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
beginning of the file
 lseek(fd,5,SEEK_CUR) – this moves the pointer 5 positions ahead from the current
position in the file 15.Working with files
 lseek(fd,-5,SEEK_CUR) – this moves the pointer 5 positions back from the current When working with files, you need to declare a pointer of type file. This
position in the file declaration is needed for communication between the file and the program.
 lseek(fd,-5,SEEK_END) -> this moves the pointer 5 positions back from the end of
the file FILE *fptr;
 Opening a file - for creation and edit
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  Opening a file is performed using the fopen() function defined in the
stdio.h header file.
13.Name some significant features of UNIX operating system.

1.Multi-tasking: More than one program can be run at a time The syntax for opening a file in standard I/O is:
2. Portability: The system is written in high-level language making it easier to ptr = fopen("fileopen","mode");
read, understand, changed and, therefore move to other machines.
3. Machine-independence: The System hides the machine architecture from @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
the user, making it easier to write applications that can run on any system.
4. Multi-User Operations: UNIX is a multi-user system designed to support a
group of users simultaneously.
5. Hierarchical File System: UNIX uses a hierarchical file structure to store
information. It allows for easy maintenance and efficient implementation.

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
9

PART- B

1. Explain why functions are needed? With syntax, illustrate how functions are declared and
defined? Categorize the 4 types of functions with example programs.

2. Interpret the significance of storage classes. List out them. Discuss their syntax. Classify the 4
types of storage classes with example programs.

3. (a) Mention the significance of pointers to functions. Demonstrate with an example program.
(function pointers)
(b) Explain how pointers are passed as arguments to a function. (call by reference)
(c) Discuss about arrays and pointers.

4.Declare and discuss various ways of initializing single, two and multi-dimensional arrays with
examples. How to multiply two 2D arrays. Write a program to multiply two 2D matrices. Analyse
with neat diagrams row-major and column major order storage of 2D arrays and demonstrate how
memory locations are identified in both.

5. Define structure. Explain its syntax and initialization. Write a program to demonstrate array of
structures to display the student database contains the following fields : rollno, name, branch and
fees. Explain how structures are accessed using pointers. Create a structure and show how it is
accessed via pointers.

6. Interpret the syntax of typedef and mention its significance. Using typedef explain how structure
variables are created and accessed. List out the advantages of variable length argument list. Design
a C program to demonstrate it.

7. Discuss in detail the standard I/O operations and Formatted I/O operations with example
programs. List out any 4 file modes and explain them.

8.Discuss error handling routines found in C with example programs. Explain about fopen() and
fclose () operations with programs.

9. Interpret the syntax of typedef and mention its significance. Using typedef explain how structure
variables are created and accessed. List out the advantages of variable length argument list. Design
a C program to demonstrate it.

10. Explain low level I/O operations handled by Unix OS and Elaborate the operations on files
- open, create, close, and unlink.

You might also like