-
Notifications
You must be signed in to change notification settings - Fork 15
/
declaring-structures.c
84 lines (60 loc) · 2.56 KB
/
declaring-structures.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
------------------------------------------------------------------------------------
Tutorial: This tutorial explains the concept of structure in C programming
Structure is a user-defined data type in C, used to store a collection of different kinds of data.
How to create a structure?
'struct' keyword is used to create a structure.
Syntax of struct
struct structureName
{
dataType member1;
dataType member2;
.....
}structureVariable1, structureVariable2 ;
//A structure variable can either be declared with structure declaration or as a separate declaration like basic types.
int main(){
struct structureName variable1, variable2....,variableN;
}
Structure elements can be accessed through a structure variable using a dot (.) operator.
e.g.: structureVariable1.member1= valueAssigned;
------------------------------------------------------------------------------------
*/
#include <stdio.h>
// create struct with person1,person2 variable
struct Person
{
char name[50];
int citNo;
float salary;
} person1; // method I of declaring variable
int main()
{
struct Person person2; // declaring struct variable outside of struct declaration (method II)
// assign value to name of person1
snprintf(person1.name, sizeof(person1.name), "Steve Holland");
/*
snprintf() function fromats and stores a serios of character and values in the array
it is used to redirect the output of printf() function onto a buffer.
*/
// assign values to other person1 variables
person1.citNo = 1984;
person1.salary = 2500;
// assign value to name of person2
snprintf(person2.name, sizeof(person2.name), "Jessica Roman");
// assign values to other person2 variables
person2.citNo = 1976;
person2.salary = 25000;
// print struct variables
printf("Name: %s\n", person1.name);
printf("Citizenship No.: %d\n", person1.citNo);
printf("Salary: %.2f\n\n", person1.salary);
printf("Name: %s\n", person2.name);
printf("Citizenship No.: %d\n", person2.citNo);
printf("Salary: %.2f\n", person2.salary);
return 0;
}
/*
----------------------------------------------------------------------------------------------------------------------------------------------------
Challenge: Write a C program to store and display infomation of books in a library using structure. (Tip: Use members like bookid, bookname etc..)
----------------------------------------------------------------------------------------------------------------------------------------------------
*/