QB Unit V
QB Unit V
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.
structure_element 1;
structure_element 2;
structure_element 3;
--------- -----
--------- -----
};
Example:
struct book
char title[20];
char author[15];
int pages;
float price;
};
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.
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
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.
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.
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
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>
int main( ) {
Book book;
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
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.
Meaning of
Mode During Inexistence of file
Mode
8
Opening Modes in Standard I/O
Meaning of
Mode During Inexistence of file
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
10