Structures - C Programming - Union - Basic Programming

BharaniDharan195623 8 views 25 slides Jul 23, 2024
Slide 1
Slide 1 of 25
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25

About This Presentation

Basic programming - Structures


Slide Content

Structures

Write a C program to add two distances (in inch-feet system) using Structures Structure name is Distance Two members – feet as integer, inch as float Three structure variables - d1,d2,result Get d1 and d2 from the user. Then add and get the result Convert inches to feet if it is greater than or equal to 12 Print the final result

#include < stdio.h > struct Distance { int feet; float inch; } d1, d2, result; int main() { // take first distance input printf ("Enter 1st distance\n"); printf ("Enter feet: "); scanf ("%d", &d1.feet); printf ("Enter inch: "); scanf ("%f", &d1.inch); // take second distance input printf ("\ nEnter 2nd distance\n"); printf ("Enter feet: "); scanf ("%d", &d2.feet); printf ("Enter inch: "); scanf ("%f", &d2.inch); // adding distances result.feet = d1.feet + d2.feet; result.inch = d1.inch + d2.inch; // convert inches to feet if greater than 12 while ( result.inch >= 12.0) { result.inch = result.inch - 12.0; ++ result.feet ; } printf ("\ nSum of distances = %d\'-%.1f\"", result.feet , result.inch ); return 0; }

Passing structs to functions #include < stdio.h > struct student { char name[50]; int age; }; // function prototype void display(struct student s); int main() { struct student s1; printf ("Enter name: "); gets(s1.name); printf ("Enter age: "); scanf ("%d", &s1.age); display(s1); // passing struct as an argument return 0; } void display(struct student s) { printf ("\ nDisplaying information\n"); printf ("Name: %s", s.name); printf ("\ nAge : %d", s.age ); }

Return struct from a function #include < stdio.h > struct student { char name[50]; int age; }; // function prototype struct student getInformation (); struct student getInformation () { struct student s1; printf ("Enter name: "); gets(s1.name); printf ("Enter age: "); scanf ("%d", &s1.age); return s1; } int main() { struct student s; s = getInformation (); printf ("\ nDisplaying information\n"); printf ("Name: %s", s.name); printf ("\ nRoll : %d", s.age ); return 0; }

Passing struct by reference – Example 1 #include < stdio.h > typedef struct Complex { float real; float imag ; } complex; void addNumbers (complex c1, complex c2, complex *result); int main() { complex c1, c2, result; printf ("For first number,\n"); printf ("Enter real part: "); scanf ("%f", &c1.real); printf ("Enter imaginary part: "); scanf ("%f", &c1.imag); printf ("For second number, \n"); printf ("Enter real part: "); scanf ("%f", &c2.real); printf ("Enter imaginary part: "); scanf ("%f", &c2.imag); addNumbers (c1, c2, &result); printf ("\ nresult.real = %.1f\n", result.real ); printf (" result.imag = %.1f", result.imag ); return 0; } void addNumbers (complex c1, complex c2, complex *result) { result->real = c1.real + c2.real; result-> imag = c1.imag + c2.imag; }

Passing struct by reference – Example 2

Union

Unions

Union Only store information in one field at any one time When a new value is assigned to a field, existing data is replaced with the new data. Thus unions are used to save memory. They are useful for applications that involve multiple members, where values need not be assigned to all the members at any one time.

Declaration & initialization of union Like structures, an union can be declared with the keyword union union student { int marks; char gender; }; To access the members of the union, use the dot operator (.)

Declaration & initialization of union Unlike structures, initialization cannot be done to all members together. #include < stdio.h > typedef struct point1 { int x,y ; }; typedef union point2 { int x; int y; }; main() { point1 p1={2,3}; // point p2={4,5}; illegal with union point2 p2; p2.x=4; p2.y=5; printf (“The coordinates of p1 are % and %d”, p1.x,p1.y); printf (“The coordinates of p2 are % and %d”, p2.x,p2.y); }

Wrong output Output The coordinated of p1 are 2 and 3 The coordinated of p2 are 5 and 5

//Corrected program #include < stdio.h > struct point1 { int x,y ; }; union point2 { int x,y ; }; main() { struct point1 p1={2,3}; // point p2={4,5}; illegal with union union point2 p2; p2.x=4; printf ("The coordinates of p1 are %d and %d \n", p1.x,p1.y); printf ("The x coordinates of p2 is %d \n", p2.x); p2.y=5; printf ("The y coordinates of p2 is %d", p2.y); }

Correct output Output: The coordinates of p1 are 2 and 3 The x coordinates of p2 are 4 The y coordinates of p2 are 5

Another example #include < stdio.h > #include < string.h > union Data { int i ; float f; char str[20]; }; int main( ) { union Data data ; data.i = 10; printf ( " data.i : %d\n", data.i ); data.f = 220.5; printf ( " data.f : %f\n", data.f ); strcpy ( data.str , "C Programming"); printf ( " data.str : %s\n", data.str ); return 0; }

Difference between structure & union
Tags