Documente Academic
Documente Profesional
Documente Cultură
#include <stdio.h>
int main()
{
int *ptr, q;
q = 50;
/* address of q is assigned to ptr */
ptr = &q;
/* display q's value using ptr variable */
printf("%d", *ptr);
return 0;
}
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record1 = {1, "Raju", 90.5};
struct student *ptr;
ptr = &record1;
return 0;
}
Self Referential structures are those structures that have one or more pointers which
point to the same type of structure, as their member.
In other words, structures pointing to the same type of structures are self-referential
in nature.
struct node
{
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob;
return 0;
}
#include <stdio.h>
struct node {
int data1;
char data2;
struct node* link;
};
int main()
{
struct node ob1; // Node1
// Initialization
ob1.link = NULL;
ob1.data1 = 10;
ob1.data2 = 20;
// Initialization
ob2.link = NULL;
ob2.data1 = 30;
ob2.data2 = 40;
#include <stdio.h>
struct node {
int data;
struct node* prev_link;
struct node* next_link;
};
int main()
{
struct node ob1; // Node1
// Initialization
ob1.prev_link = NULL;
ob1.next_link = NULL;
ob1.data = 10;
// Initialization
ob2.prev_link = NULL;
ob2.next_link = NULL;
ob2.data = 20;
// Initialization
ob3.prev_link = NULL;
ob3.next_link = NULL;
ob3.data = 30;
// Forward links
ob1.next_link = &ob2;
ob2.next_link = &ob3;
// Backward links
ob2.prev_link = &ob1;
ob3.prev_link = &ob2;
Lets say we need to store the data of students like student name, age, address, id
etc. One way of doing this would be creating a different variable for each attribute,
however when you need to store the data of multiple students then in that case, you
would need to create these several variables again for each student. This is such a
big headache to store data in this way.
We can solve this problem easily by using structure. We can create a structure that
has members for name, id, address and age and then we can create the variables of
this structure for each student. This may sound confusing, do not worry we will
understand this with the help of example.
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
};
Here struct_name can be anything of your choice. Members data type can be same
or different. Once we have declared the structure we can use the struct name as a
data type like int, float etc.
struct struct_name {
DataType member1_name;
DataType member2_name;
DataType member3_name;
…
} var_name;
How to access data members of a structure using a struct variable?
var_name.member1_name;
var_name.member2_name;
…
var_name.memeber_name = value;
2) All members assigned in one statement
#include <stdio.h>
/* Created a structure here. The name of the structure is
* StudentData.
*/
struct StudentData{
char *stu_name;
int stu_id;
int stu_age;
};
int main()
{
/* student is the variable of structure StudentData*/
struct StudentData student;
2. Create a structure student to store the name, roll no and marks of the
students. Enter some sample values within the structure and print the
details of the student with maximum marks.
#include <stdio.h>
struct student
char name[50];
int roll;
float marks;
} s[10];
int main()
int i;
printf("Enter information of students:\n");
// storing information
s[i].roll = i+1;
scanf("%s",s[i].name);
scanf("%f",&s[i].marks);
printf("\n");
printf("Displaying Information:\n\n");
// displaying information
printf("Name: ");
puts(s[i].name);
printf("Marks: %.1f",s[i].marks);
printf("\n");
return 0;
Output
Roll number: 1
Name: Tom
Marks: 98
.
.
.
3. WAP to copy contents from file name ss.txt to tk.txt with the concept of file
handling.
#include <stdio.h>
int main()
char filename[100], c;
scanf("%s", filename);
if (fptr1 == NULL)
exit(0);
if (fptr2 == NULL)
exit(0);
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
fclose(fptr1);
fclose(fptr2);
return 0;
}
Output:
ss.txt
tk.txt
a) fopen();
File
operation Declaration & Description
fopen()
FILE *fp;
File
handling functions Description
b) eof();
EOF means end of file. It's a sign that the end of a file is reached, and that there will
be no data anymore. On Linux systems and OS X, the character to input to cause an
EOF is CTRL+D. For Windows, it's CTRL+Z. Depending on the operating system, this
character will only work if it's the first character on a line, i.e. the first character after
an ENTER. Since console input is often line-oriented, the system may also not
recognize the EOF character until after you've followed it up with an ENTER And yes,
if that character is recognized as an EOF, then your program will never see the actual
character. Instead, a C program will get a -1 from getchar().
c) fread();
Description
The C library function size_t fread(void *ptr, size_t size, size_t nmemb, FILE
*stream) reads data from the given stream into the array pointed to, by ptr.
Declaration
Parameters
nmemb − This is the number of elements, each one with a size of size bytes.
stream − This is the pointer to a FILE object that specifies an input stream.
Return Value
The total number of elements successfully read are returned as a size_t object,
which is an integral data type. If this number differs from the nmemb parameter,
then either an error had occurred or the End Of File was reached.
d) fwrite();
Description
The C library function size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream) writes data from the array pointed to, by ptr to the given stream.
Declaration
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
Parameters
nmemb − This is the number of elements, each one with a size of size bytes.
stream − This is the pointer to a FILE object that specifies an output stream.
Return Value
This function returns the total number of elements successfully returned as a size_t
object, which is an integral data type. If this number differs from the nmemb
parameter, it will show an error.
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.