C_unit-V
C_unit-V
UNIT-V
//Eg:
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
char *name;
int rollno;
float totalmark;
};
struct student stud1={"Venkat",1,98};
struct student stud3= {"Shweta",3,97};
struct student stud2={"Arpita",2,99};
clrscr();
printf(“STUDENTS DETAILS:\n”);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud1.rollno, stud1.name,
stud1.totalmark);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud2.rollno, stud2.name,
stud2.totalmark);
printf(“\n\n Roll number:%d\n Name:%s\n Total Marks:%f”, stud3.rollno, stud3.name,
stud3.totalmark);
getch( );
}
Output :
STUDENTS DETAILS:
Roll number: 1
Name: Venkat
Total Marks:98.000000
Roll number: 2
Name: Arpita
Total Marks:99.000000
Roll number: 3
Name:Shweta
Total Marks:99.000000
struct tagname{
datatype member1;
datatype member2;
datatype member n;
};
//Eg:
#include<stdio.h>
Struct Student{
char Name[10];
int roll; };
void show(struct Student st);
void main( ){
struct Student std;
printf(“\n Enter student record:\n”);
printf(:\n Student Name:”);
scanf(:%s”,std.Name);
printf(“\n Enter student roll no:\t”);
scanf(“%d”,&std.roll);
show(std);
}
void show(struct Student st)
{
printf(“\n Student name is %s”,st.Name);
}
⚫ Self Referential Structure with Single Link: These structures can have only one self-
pointer as their member. The following example will show us how to connect the objects
of a self- referential structure with the single link and access the corresponding data
members. The connection formed is shown in the following figure
//Eg:
#include<stdio.h>
struct node{
int data1;
char data2;
struct node*link;
};
int main( ){
struct node ob1;
ob1.link=NULL; //node1
//initialization
ob1.data1=10;
ob1.data2=20;
struct node ob2; //node2
//Initialization
ob2.link=NULL;
ob2.data1=30;
ob2.data2=40;
ob1.link=&ob2;
printf(“%d”,ob1.link->data1);
printf(:\n %d”,ob1.link->data2);
return 0;
}
⚫ Self Referential Structure with Multiple Links: Self referential structures with multiple
links can have more than one self-pointers. Many complicated data structures can be
easily constructed using these structures. Such structures can easily connect to more than
one nodes at a time. The following example shows one such structure with more than one
links.
The Union is a user-defined data type in C language that can contain elements of the
different data types just like structure. But unlike structures, all the members in the C union
are stored in the same memory location. Due to this, only one member can store data at the
given instance.
Syntax: union union_name {
datatype member1;
datatype member2;
...
};
⚫ Different Ways to Define a Union Variable
We need to define a variable of the union type to start using union members. There are two
methods using which we can define a union variable.
➢ With Union Declaration
➢ After Union Declaration
We can access the members of a union by using the ( . ) dot operator just like structures.
var1.member1;
Initialization of Union in C
The initialization of a union is the initialization of its members by simply assigning the
value to it.
var1.member1 = some_value;
⚫ Initialization of Union in C
The initialization of a union is the initialization of its members by simply assigning the
value to it.
var1.member1 = some_value;
//Eg:
union un {
int member1;
char member2;
float member3;
};
// driver code
int main()
{
// defining a union variable
union un var1;
return 0;
}
Output:
The value stored in member1 = 15
//Eg:
#include <stdio.h>
return 0;
}
//Eg:
#include<stdio.h>
enum week =
{“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”};
int main( ){
today = Wednesday;
printf(“Day %d”,today+1);
return 0;
Output: Day 4
File is a collection of data that stored on secondary memory like harddisk of a computer.C
langugage supports two types of files.
➢ Binary Files
⚫ Text File: The file that contains ASCII codes of data like digits,alphabets and symbols is
called text file (or) ASCII file.
⚫ Binary File: The file that contains data in the form of bytes (o’s and 1’s) is called as
Binary file.
File Operations:
❖ Closing a file.
Modes of Files:
Opening a file:
The fopen() function is used to create a file or open an existing file:
int main()
{
return 0;
}
7) What is Streams? Explain about Standard Library Input/Output functions and
Character Input/Output functions?
Stream: A stream is a logical entity that represents a file or device that can accept input or
output.All input and output functions in standard C, operate on data streams.
Types of Files:
Stream can be divided into text streams and binary stream.
❖ Text files: Text files are the normal .txt files. To create text files using any simple
texteditors such as Notepad.
❖ Binary files: Binary files are mostly .bin files in computer. Instead of storing data
inplain text, they store it in the binary form(0’s and 1’s).
1. getch( ): 'get' stands for input, 'ch' stands for character. Function getch() is use to input
single character, but it will not display the character entered by the user.
2. getche( ): getche( ) is same as getch() function only the difference is that it will
display the character entered by the user. 'get' stands for input, 'ch' stands for character,
and 'e' stands for echo(display)
3. getchar( ): It will terminate by enter key after inputting a single character.
Syntax: char a = getchar();