-
Notifications
You must be signed in to change notification settings - Fork 15
/
typedefs.c
70 lines (50 loc) · 2.11 KB
/
typedefs.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
/*
------------------------------------------------------------------------------------
Tutorial: This tutorial is about Keyword typedef
Typedef is a keyword used in C programming to provide some meaningful names to the already existing variable in the C program.
Syntax of typedef:
typdef <existing_name> <alias_name>
Example:
typedef unsigned int uint;
Now we can write ' uint ' instead of writing 'unsigned int 'in our program.
Create variable of type unsigned int by writing the following statement
uint a,b; // instead of unsigned int a,b;
NOTE: typedef can be used with structure to define a new data type and then use that data type to define structure variables directly as follow
typedef struct structure_name
{
type member1;
type member2;
type member3;
} type_name;
Here type_name represents the stucture definition associated with it. Now this type_name can be used to declare a variable of this stucture type.
type_name t1, t2;
------------------------------------------------------------------------------------
*/
// Code here explaining concept
#include <stdio.h>
#include <string.h>
// struct with typedef person
typedef struct Person {
char name[50];
int citNo;
float salary;
} person;
int main() {
// create Person variable
person p1; // equivalent to -> struct Person p1
// assign value to name of p1
snprintf(p1.name, sizeof(p1.name), "Steve Holland");
// assign values to other p1 variables
p1.citNo = 1984;
p1.salary = 2500;
// print struct variables
printf("Name: %s\n", p1.name);
printf("Citizenship No.: %d\n", p1.citNo);
printf("Salary: %.2f", p1.salary);
return 0;
}
/*
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Challenge: Write a C program to store and display infomation of books in a library using structure but without using the 'struct' keyword in the main function.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
*/