Skip to content

Commit a8a8ae6

Browse files
committed
Structures and Union
1 parent 3c55708 commit a8a8ae6

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

Structures and Union/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Structures and Union
2+
#### Structures provides a way for us to create our own data type called “ user-defined data type”
3+
#### Assume that we have to store the details of a student, so we can use structure to store it as below:
4+
#### Structures in C is a user-defined data type available in C that allows to combining of data items of different kinds. Structures are used to represent a record.
5+
#### To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than or equal to one member.
6+
7+
```
8+
struct [structure name]
9+
{
10+
member definition;
11+
member definition;
12+
...
13+
member definition;
14+
};
15+
```
16+
17+

Structures and Union/struUnion.c

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
// declaring structure
5+
struct struct_example
6+
{
7+
int integer;
8+
float decimal;
9+
char name[20];
10+
};
11+
12+
// declaring union
13+
14+
union union_example
15+
{
16+
int integer;
17+
float decimal;
18+
char name[20];
19+
};
20+
21+
void main()
22+
{
23+
// creating variable for structure
24+
// and initializing values difference
25+
// six
26+
struct struct_example s={18,38,"nazusecurity"};
27+
28+
// creating variable for union
29+
// and initializing values
30+
union union_example u={18,38,"nazusecurity"};
31+
32+
33+
printf("structure data:\n integer: %d\n"
34+
"decimal: %.2f\n name: %s\n",
35+
s.integer, s.decimal, s.name);
36+
printf("\nunion data:\n integer: %d\n"
37+
"decimal: %.2f\n name: %s\n",
38+
u.integer, u.decimal, u.name);
39+
40+
41+
// difference two and three
42+
printf("\nsizeof structure : %d\n", sizeof(s));
43+
printf("sizeof union : %d\n", sizeof(u));
44+
45+
// difference five
46+
printf("\n Accessing all members at a time:");
47+
s.integer = 183;
48+
s.decimal = 90;
49+
strcpy(s.name, "nazusecurity");
50+
51+
printf("structure data:\n integer: %d\n "
52+
"decimal: %.2f\n name: %s\n",
53+
s.integer, s.decimal, s.name);
54+
55+
u.integer = 183;
56+
u.decimal = 90;
57+
strcpy(u.name, "nazusecurity");
58+
59+
printf("\nunion data:\n integer: %d\n "
60+
"decimal: %.2f\n name: %s\n",
61+
u.integer, u.decimal, u.name);
62+
63+
printf("\n Accessing one member at time:");
64+
65+
printf("\nstructure data:");
66+
s.integer = 240;
67+
printf("\ninteger: %d", s.integer);
68+
69+
s.decimal = 120;
70+
printf("\ndecimal: %f", s.decimal);
71+
72+
strcpy(s.name, "C programming");
73+
printf("\nname: %s\n", s.name);
74+
75+
printf("\n union data:");
76+
u.integer = 240;
77+
printf("\ninteger: %d", u.integer);
78+
79+
u.decimal = 120;
80+
printf("\ndecimal: %f", u.decimal);
81+
82+
strcpy(u.name, "C programming");
83+
printf("\nname: %s\n", u.name);
84+
85+
//difference four
86+
printf("\nAltering a member value:\n");
87+
s.integer = 1218;
88+
printf("structure data:\n integer: %d\n "
89+
" decimal: %.2f\n name: %s\n",
90+
s.integer, s.decimal, s.name);
91+
92+
u.integer = 1218;
93+
printf("union data:\n integer: %d\n"
94+
" decimal: %.2f\n name: %s\n",
95+
u.integer, u.decimal, u.name);
96+
}

0 commit comments

Comments
 (0)