c programming - refresher - part iii
DESCRIPTION
C programming refresher from Embedded point of view. Third part talks about user defined data types.TRANSCRIPT
![Page 1: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/1.jpg)
C Refresher
Team Emertxe
Day3
![Page 2: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/2.jpg)
User defined data types
![Page 3: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/3.jpg)
Data Types
Structure – Collection of different data types
Union – Similar to Structure, storage wise different
Enum – Enumerated data types
Typedef – Type definitions
Bit-fields – Efficiency purpose, used in Embedded Systems
![Page 4: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/4.jpg)
Structure
A structure is a collection of variables under a single
name.
These variables can be of different types
A structure is a convenient way of grouping several
pieces of related information together.
A structure can be defined as a new named type
It can use other structures, arrays or pointers as some
of its members
![Page 5: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/5.jpg)
Definition & Usage
An example structure (student record and its usage)
Members are accessed using “.” operator
struct student {
char name[64];
char course[128];
int age;
int year;
};
struct student rec;
rec.age = 22;
rec.year = 1990;
strcpy (rec.name, “Bakra”);
strcpy (rec.course, “Boring”);
![Page 6: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/6.jpg)
Unions
Unions are quite similar to the structures in C
Union variables can be created in similar manner as structure
variable
The amount of memory required to store a structure variables is
the sum of memory size of all members
The memory required to store a union variable is the memory
required for largest element of an union
![Page 7: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/7.jpg)
Typedef
The typedef keyword allows the programmer to create new names
for types
It can be a standard or user defined types
Typedefs can be used both to provide more clarity to code
Makes it easier to make changes to the underlying data types
![Page 8: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/8.jpg)
Definition & Usage
Same student record, you get your own type
By making it as a typedef, it becomes easier to handle
struct student {
char name[64];
char course[128];
int age;
int year;
};
typedef struct student stud_rec;
student_rec rec;
rec.age = 22;
rec.year = 1990;
strcpy (rec.name, “Bakra”);
strcpy (rec.course, “Boring”);
![Page 9: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/9.jpg)
Hands-on!
![Page 10: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/10.jpg)
Pointers again
Pointers to structures provide flexibility, freedom and
efficiency in terms of both type and size elements
Extensively used in Embedded systems programming
The “->” operator is used for accessing members of the
structures
Fundamentals for Data Structures
![Page 11: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/11.jpg)
Hands-on!
![Page 12: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/12.jpg)
Enum
Enumeration type allows programmer to define their own data type
Keyword enum is used to defined enumerated data type
Increases usability and readability
enum type_name { value1, value2,...,value N };
enum boolean {
false;
true;
};
enum boolean check;
![Page 13: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/13.jpg)
Bit fields
Bit fields offer efficient way of using members inside a
structure
By declaring number of bits required, appropriate
location can be accessed
In Embedded systems programming this helps to a
greater extent in efficiency
![Page 14: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/14.jpg)
Stay connected
About us: Emertxe is India’s one of the top IT finishing schools & self learning kits provider. Our primary focus is on Embedded with diversification focus on Java, Oracle and Android areas
Emertxe Information Technologies,
No-1, 9th Cross, 5th Main,Jayamahal Extension,
Bangalore, Karnataka 560046
T: +91 80 6562 9666E: [email protected]
https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides
![Page 15: C Programming - Refresher - Part III](https://reader034.vdocuments.site/reader034/viewer/2022051816/54625efdaf7959422a8b4d16/html5/thumbnails/15.jpg)
THANK YOU