0% found this document useful (0 votes)
12 views10 pages

QB Unit V

FOC

Uploaded by

VISHNUKUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

QB Unit V

FOC

Uploaded by

VISHNUKUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

UNIT – V: STRUCTURES AND UNIX SYSTEM INTERFACE

Basic Structures, Structures and Functions, Array of structures, Pointer of Structures, Self-referential Structures, Table look
up, Typedef, Unions, Bit-fields, File Access -Error Handling, Line I/O, Miscellaneous Functions. Unix system Interface:
File Descriptor, Low level I/O, read and write, Open, create; close and unlink, Random access –lseek, Discussions on
Listing Directory, Storage allocator.

1. Define a structure

The structure can be declared with the keyword struct following the name and opening brace with data
elements of different type then closing brace with semicolon.

The general format of a structure definition is as follows:

struct structure _ name

structure_element 1;

structure_element 2;

structure_element 3;
--------- -----

--------- -----

};

struct structure_name v1,v2…vn;

v1,v2….vn are structure variable.

Example:

struct book

char title[20];

char author[15];

int pages;

float price;

};

Struct book b1,b2,b3;

1
2. What are all the basic rules to be followed to create a structure?
The template is terminated with a semicolon.
While the entire definition is considered as a statement, each member is declared independently for its name
and type in a separate statement inside the template.
The tag name such as book _ bank can be used to declare structure variables of its type, later in the program.
3. Compare array with structure.

Array Structure
An array is a collection of related data Structure can have elements of different
elements of same type. types.
An array is derived data type structure is a user-defined one
Any array behaves like a built-in data type It must be declared and defined
An array can be increased or decreased A structure element can be added if
necessary.

4. How to access the structure elements?

After declaring the structure type, variables and members, the member of the structure can be accessed by
using the structure variable along with the dot(.) operator.

struct std

int no;

char name[10];

int marks;

};

struct std s;
for accessing the structure members from the above example.
s.no; s.name; s.marks;
where s is the structure variable

5. How to assign the initial values to the structure elements.


Like any other data type, a structure variable can be initialized at compile time.

main()
{
struct
{
int weight;
float height;
}
student ={60, 180.75};
………
2
………
}
This assigns the value 60 to student. weight and 180.75 to student. height. There is a one-to-one
correspondence between the members and their initializing values.

struct st _ record
{
int weight;
float height;
};
main()
{
struct st_record student1 ={60, 180.75};
struct st_record student2 ={53, 170.60};
………
………
}

C language does not permit the initialization of individual structure member within the template. The
initialization must be done only in the declaration of the actual variables.

6. Define a Union in C.

Union is user defined data type used to stored data under unique variable name at single memory location.
Union is similar to that of stucture. Syntax of union is similar to stucture. But the major
7. Compare structure and union data types in C.

8. How to define a structure variable as an array data type?


When database of any element is used in huge amount, we prefer Array of structures.
Example: suppose we want to maintain data base of 200 students, Array of structures is used.
#include<stdio.h>
#include<string.h>
struct student
{
3
char name[30]; char branch[25]; int roll;
};
void main()
{
struct student s[200]; int i;
s[i].roll=i+1;
printf("\nEnter information of students:"); for(i=0;i<200;i++)
{
printf("\nEnter the roll no:%d\n",s[i].roll); printf("\nEnter the name:"); scanf("%s",s[i].name);
printf("\nEnter the branch:"); scanf("%s",s[i].branch); printf("\n");
}
printf("\nDisplaying information of students:\n\n"); for(i=0;i<200;i++)
{
printf("\n\nInformation for roll no%d:\n",i+1);
printf("\nName:");
puts(s[i].name); printf("\nBranch:"); puts(s[i].branch);
}
}
In Array of structures each element of array is of structure type as in above example

9. How to use a Pointer of Structure?


The pointer is a variable which points to the address of another variable of any data type
like int, char, float etc. Similarly, we can have a pointer to structures, where a pointer variable can point
to the address of a structure variable. Here is how we can declare a pointer to a structure variable.
1
2 struct dog
3 {
4 char name[10];
5 char breed[10];
6 int age;
7 char color[10];
8 };
9 struct dog spike;
10 // declaring a pointer to a structure of type struct dog
11 struct dog *ptr_dog
12
This declares a pointer ptr_dog that can store the address of the variable of type struct dog. We can now assign
the address of variable spike to ptr_dog using & operator.
1 ptr_dog = &spike;
Now ptr_dog points to the structure variable spike.
Accessing members using Pointer
There are two ways of accessing members of structure using pointer:
Using indirection (*) operator and dot (.) operator.
Using arrow (->) operator or membership operator.

Using Indirection (*) Operator and Dot (.) Operator

At this point ptr_dog points to the structure variable spike, so by dereferencing it we will get the contents of
the spike. This means spike and *ptr_dog are functionally equivalent. To access a member of structure
write *ptr_dog followed by a dot(.) operator, followed by the name of the member. For example:

4
(*ptr_dog).name – refers to the name of dog
(*ptr_dog).breed – refers to the breed of dog
Parentheses around *ptr_dog are necessary because the precedence of dot(.) operator is greater than that of
indirection (*) operator.

Using arrow operator (->)


The above method of accessing members of the structure using pointers is slightly confusing and less readable,
that’s why C provides another way to access members using the arrow (->) operator. To access members using
arrow (->) operator write pointer variable followed by -> operator, followed by name of the member.
1 ptr_dog->name - refers to the name of dog
2 ptr_dog->breed - refers to the breed of dog

Here we don’t need parentheses, asterisk (*) and dot (.) operator. This method is much more readable and
intuitive.
We can also modify the value of members using pointer notation.
1 strcpy(ptr_dog->name, "new_name");
Here we know that the name of the array (ptr_dog->name) is a constant pointer and points to the 0th element
of the array. So we can’t assign a new string to it using assignment operator (=), that’s why strcpy() function is
used.
--ptr_dog->age;
In the above expression precedence of arrow operator (->) is greater than that of prefix decrement operator (--),
so first -> operator is applied in the expression then its value is decremented by 1.
The following program demonstrates how we can use a pointer to structure.
1 #include<stdio.h>
2
3 struct dog
4 {
5 char name[10];
6 char breed[10];
7 int age;
8 char color[10];
9 };
10
11 int main()
12 {
13 struct dog my_dog = {"tyke", "Bulldog", 5, "white"};
14 struct dog *ptr_dog;
15 ptr_dog = &my_dog;
16
17 printf("Dog's name: %s\n", ptr_dog->name);
18 printf("Dog's breed: %s\n", ptr_dog->breed);
19 printf("Dog's age: %d\n", ptr_dog->age);
20 printf("Dog's color: %s\n", ptr_dog->color);
21
22 // changing the name of dog from tyke to jack
23 strcpy(ptr_dog->name, "jack");
24
25 // increasing age of dog by 1 year
26 ptr_dog->age++;
27
28 printf("Dog's new name is: %s\n", ptr_dog->name);
5
29 printf("Dog's age is: %d\n", ptr_dog->age);
30
31 // signal to operating system program ran fine
32 return 0;
33 }
Expected Output:
1 Dog's name: tyke
2 Dog's breed: Bulldog
3 Dog's age: 5
4 Dog's color: white
5
6 After changes
7
8 Dog's new name is: jack
9 Dog's age is: 6

10. Write about TypeDef in C


‘link’ is a pointer to a structure of type ‘node’. Hence, the structure ‘node’ is a self-referential structure with
‘link’ as the referencing pointer.
An important point to consider is that the pointer should be initialized properly before accessing, as by default
it contains garbage value.

The C programming language provides a keyword called typedef, which you can use to give a type a new
name. Following is an example to define a term BYTE for one-byte numbers −
typedef unsigned char BYTE;
After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned char, for
example..
BYTE b1, b2;
By convention, uppercase letters are used for these definitions to remind the user that the type name is really a
symbolic abbreviation, but you can use lowercase, as follows −
typedef unsigned char byte;
You can use typedef to give a name to your user defined data types as well. For example, you can use typedef
with structure to define a new data type and then use that data type to define structure variables directly as
follows −
#include <stdio.h>
#include <string.h>

typedef struct Books {


char title[50];
char author[50];
char subject[100];
int book_id;
} Book;

int main( ) {

Book book;

strcpy( book.title, "C Programming");


strcpy( book.author, "Nuha Ali");
strcpy( book.subject, "C Programming Tutorial");
6
book.book_id = 6495407;

printf( "Book title : %s\n", book.title);


printf( "Book author : %s\n", book.author);
printf( "Book subject : %s\n", book.subject);
printf( "Book book_id : %d\n", book.book_id);

return 0;
}
When the above code is compiled and executed, it produces the following result −
Book title : C Programming
Book author : Nuha Ali
Book subject : C Programming Tutorial
Book book_id : 6495407
typedef vs #define
#define is a C-directive which is also used to define the aliases for various data types similar to typedef but
with the following differences −
typedef is limited to giving symbolic names to types only where as #define can be used to define alias for
values as well, q., you can define 1 as ONE etc.
typedef interpretation is performed by the compiler whereas #define statements are processed by the pre-
processor.
The following example shows how to use #define in a program −
#include <stdio.h>

#define TRUE 1
#define FALSE 0

int main( ) {
printf( "Value of TRUE : %d\n", TRUE);
printf( "Value of FALSE : %d\n", FALSE);

return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of TRUE : 1
Value of FALSE : 0

11. Why file is needed in C.


 When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if
the program terminates.
 If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of the file using a
few commands in C.
 You can easily move your data from one computer to another without any changes.

12. List and explain the types of Files


When dealing with files, there are two types of files you should know about:
1. Text files
2. Binary files

7
1. Text files
Text files are the normal .txt files. You can easily create text files using any simple text editors such as
Notepad.
When you open those files, you'll see all the contents within the file as plain text. You can easily edit or delete
the contents.
They take minimum effort to maintain, are easily readable, and provide the least security and takes bigger
storage space.
2. Binary files
Binary files are mostly the .bin files in your computer.
Instead of storing data in plain text, they store it 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.

13. List the various file operations in C

The following file operation carried out on the file


(1) creation of a new file
(2) opening a file
(3) I/O operations or Reading/Writing operations on the file
(4) closing a file
14. List the various file opening modes in C

Opening Modes in Standard I/O

Meaning of
Mode During Inexistence of file
Mode

Open for If the file does not exist,


r
reading. fopen() returns NULL.

Open for reading If the file does not exist,


rb
in binary mode. fopen() returns NULL.

If the file exists, its contents


are overwritten.
w Open for writing.
If the file does not exist, it will
be created.

If the file exists, its contents


Open for writing are overwritten.
wb
in binary mode. If the file does not exist, it will
be created.

Open for append.


Data is added to If the file does not exist, it will
a
the end of the be created.
file.

8
Opening Modes in Standard I/O

Meaning of
Mode During Inexistence of file
Mode

Open for append


in binary mode.
If the file does not exist, it will
ab Data is added to
be created.
the end of the
file.

Open for both


If the file does not exist,
r+ reading and
fopen() returns NULL.
writing.

Open for both


reading and If the file does not exist,
rb+
writing in binary fopen() returns NULL.
mode.

If the file exists, its contents


Open for both
are overwritten.
w+ reading and
If the file does not exist, it will
writing.
be created.

Open for both If the file exists, its contents


reading and are overwritten.
wb+
writing in binary If the file does not exist, it will
mode. be created.

Open for both


If the file does not exist, it will
a+ reading and
be created.
appending.

Open for both


reading and If the file does not exist, it will
ab+
appending in be created.
binary mode.

9
14. What are the various types of UNIX OS?
15. Define Kernal/Shell in Unix OS.
16. Write about the following UNIX commands.
i. ls ii. mkdir iii. pwd iv. rm
17. Write about the following UNIX commands.
a. cat ii. grep iii. cp iv. mv

PART B

1. Write in detail about the structure data type in C


2. Write about Union data type in C
3. Write in detail about the File operations in C
4. What are the various file opening modes are available in C.
5. Write short notes on Unix.
6. List and explain various Unix commands with suitable example.

10

You might also like