View
715
Download
4
Category
Tags:
Preview:
DESCRIPTION
C programming refresher from Embedded point of view. Third part talks about user defined data types.
Citation preview
C Refresher
Team Emertxe
Day3
User defined data types
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
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
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”);
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
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
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”);
Hands-on!
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
Hands-on!
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;
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
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: training@emertxe.com
https://www.facebook.com/Emertxe https://twitter.com/EmertxeTweet https://www.slideshare.net/EmertxeSlides
THANK YOU
Recommended