1.
MODIFYING VALUE (AFTER SLIDE 9)
#include<stdio.h>
#include<string.h>
struct employee
{ int id;
char name[20];
float salary;
}emp1;
int main() {
emp1.id=1;
strcpy(emp1.name,"ABC");
emp1.salary=80000.00;
printf("Employee1 Deatils:\n");
printf("Id: %d",emp1.id);
printf("\nName: %s",emp1.name);
printf("\nSalary=%f\n\n",emp1.salary);
emp1.id=20;
strcpy(emp1.name,"XYZ");
emp1.salary=90000.00;
printf("Employee1 Deatils:\n");
printf("Id: %d",emp1.id);
printf("\nName: %s",emp1.name);
printf("\nSalary=%f\n\n",emp1.salary);
return 0;
}
2. ARRAYS OF DIFFERENT DATATYPES IN
STRUCTURE (SLIDE 12)
#include <stdio.h>
#define SIZE 5
struct Arrays {
int intArray[SIZE];
float floatArray[SIZE];
};
int main() {
struct Arrays myArrays;
printf("Enter %d integers:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &myArrays.intArray[i]);
}
printf("Enter %d floats:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%f",&myArrays.floatArray[i]);
}
printf("\nInteger Array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", myArrays.intArray[i]);
}
printf("\nFloat Array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%.2f ", myArrays.floatArray[i]);
}
printf("\n");
return 0;
}
3. STRUCTURE ARRAY (SLIDE 13)
#include<stdio.h>
#include<string.h>
struct employee
{ int id;
char name[20];
float salary;
};
int main()
{
struct employee emp[3];
int i;
for(i=0;i<3;i++)
{
printf("Employee Detail %d: ", i);
printf("\nEnter Employee Id:");
scanf("%d", &emp[i].id);
printf("Enter Employee Name:");
scanf("%s", emp[i].name);
printf("Enter Employee Salary:");
scanf("%f", &emp[i].salary);
}
printf("\nEmployee Details:\n");
for(i=0;i<3;i++)
{
printf("\nId: %d",emp[i].id);
printf("\nName: %s",emp[i].name);
printf("\nSalary=%f\n\n",emp[i].salary);
}
return 0;
}
4. STRUCTURE WITH POINTER (Slide 14)
#include <stdio.h>
struct Student {
char name[50];
int roll;
float marks;
};
int main() {
struct Student stud;
struct Student *ptr = &stud;
printf("Enter student name: ");
scanf("%s", ptr->name);
printf("Enter roll number: ");
scanf("%d", &ptr->roll);
printf("Enter marks: ");
scanf("%f", &ptr->marks);
printf("\nStudent Details:\n");
printf("Name: %s\n", ptr->name);
printf("Roll Number: %d\n", ptr->roll);
printf("Marks: %.2f\n", ptr->marks);
return 0;
}
5. STRUCTURE WITH FUNCTION (Passing a structure
to a function) (Slide 15)
#include <stdio.h>
#define SIZE 5
struct Arrays {
int intArray[SIZE];
float floatArray[SIZE];
};
void Inputs(struct Arrays myArrays)
{
printf("Enter %d integers:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &myArrays.intArray[i]);
}
printf("Enter %d floats:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%f",&myArrays.floatArray[i]);
}
Outputs(myArrays);
}
void Outputs(struct Arrays myArrays)
{
printf("\nInteger Array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", myArrays.intArray[i]);
}
printf("\nFloat Array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%.2f ", myArrays.floatArray[i]);
}
printf("\n");
}
int main() {
struct Arrays myArrays;
Inputs(myArrays);
return 0;
}
6. STRUCTURE WITH POINTER AS FUNCTION
ARGUMENT (Slide 16)
#include <stdio.h>
#define SIZE 5
struct Arrays {
int intArray[SIZE];
float floatArray[SIZE];
};
void Inputs(struct Arrays *myArrays)
{
printf("Enter %d integers:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%d", &myArrays->intArray[i]); // The -> operator is called
the arrow operator and is used to access members of a structure (or union)
through a pointer.
}
printf("Enter %d floats:\n", SIZE);
for (int i = 0; i < SIZE; i++) {
scanf("%f",&myArrays->floatArray[i]);
}
Outputs(myArrays);
}
void Outputs(struct Arrays myArrays)
{
printf("\nInteger Array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%d ", myArrays.intArray[i]);
}
printf("\nFloat Array:\n");
for (int i = 0; i < SIZE; i++) {
printf("%.2f ", myArrays.floatArray[i]);
}
printf("\n");
}
int main() {
struct Arrays myArrays;
Inputs(&myArrays);
return 0;
}
7. RETURING STRUCTURE FROM FUNCTION (Slide
17)
#include <stdio.h>
struct MyStruct {
int x;
int y;
};
struct MyStruct MyFunction() {
struct MyStruct p;
p.x = 10;
p.y = 20;
return p;
}
int main() {
struct MyStruct p1 = MyFunction();
printf("Value of x is : %d\nValue of y is : %d\n", p1.x, p1.y);
return 0;
}
8. ANOTHER EXAMPLE OF RETURNING
STRUCTURE (Arithmetic operations)
#include <stdio.h>
struct ArithmeticResults {
int sum;
int difference;
int product;
float quotient;
};
struct ArithmeticResults calculate(int a, int b) {
struct ArithmeticResults results;
results.sum = a + b;
results.difference = a - b;
results.product = a * b;
results.quotient = (float)a / b;
return results;
}
int main() {
int num1 = 15;
int num2 = 3;
struct ArithmeticResults res = calculate(num1, num2);
printf("Sum: %d\n", res.sum);
printf("Difference: %d\n", res.difference);
printf("Product: %d\n", res.product);
printf("Quotient: %.2f\n", res.quotient);
return 0;
}
9. STRUCTURE WITH IN STRUCTURE (NESTED
STRUCTURE) (Slide 18)
#include <stdio.h>
struct Address {
char street[50];
char city[30];
int zipCode;
};
struct Employee {
char name[50];
int id;
float salary;
struct Address address;
};
int main() {
struct Employee emp;
// {"John", 1, 55000.50, {"AAA", "BLR", 10001}}
printf("\nEnter Employee Id:");
scanf("%d", &emp.id);
printf("Enter Employee Name:");
scanf("%s", emp.name);
printf("Enter Employee Salary:");
scanf("%f", &emp.salary);
printf("Enter Employee Address:\n");
printf("Enter Employee Street:");
scanf("%s", emp.address.street);
printf("Enter Employee City:");
scanf("%s", emp.address.city);
printf("Enter Employee ZipCode:");
scanf("%d", &emp.address.zipCode);
printf("\nEmployee Details are:\nEmployee Name: %s\n", emp.name);
printf("Employee ID: %d\n", emp.id);
printf("Employee Salary: %.2f\n", emp.salary);
printf("Employee Address: %s, %s, %d\n", emp.address.street,
emp.address.city, emp.address.zipCode);
return 0;
}
10. DYNAMIC MEMORY ALLOCATION FOR A
SINGLE INTEGER (Slide 21)
#include <stdio.h>
#include <stdlib.h> // Required for malloc and free
int main() {
int *ptr;
// ptr = (int*) malloc(sizeof(int)); // Allocating memory for one integer
// ptr = (int*) calloc(1, sizeof(int));
//int *ptr=NULL; ptr = (int*) realloc(ptr, sizeof(int));
*ptr = 100;
printf("Value of the dynamically allocated integer: %d\n", *ptr);
free(ptr); // Freeing the allocated memory
return 0;
}
11. DYNAMIC MEMORY ALLOCATION FOR AN
ARRAY (Slide 21)
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr;
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
arr = (int*) malloc(n * sizeof(int));
// arr = (int*) calloc( n, n * sizeof(int));
for (i = 0; i < n; i++) {
arr[i] = i + 1;
printf("arr[%d] = %d\n", i, arr[i]);
}
free(arr);
return 0;
}
12. DYNAMIC MEMORY ALLOCATION FOR A
STRUCTURE (Slide 22)
#include <stdio.h>
#include <stdlib.h>
struct Point {
int x;
int y;
};
int main() {
struct Point *p;
p = (struct Point*) malloc(sizeof(struct Point));
// p = (struct Point*) calloc(1, sizeof(struct Point));
p->x = 10;
p->y = 20;
printf("Point: (%d, %d)\n", p->x, p->y);
free(p);
return 0;
}