15
C Refresher Team Emertxe Day3

C Programming - Refresher - Part III

Embed Size (px)

DESCRIPTION

C programming refresher from Embedded point of view. Third part talks about user defined data types.

Citation preview

Page 1: C Programming - Refresher - Part III

C Refresher

Team Emertxe

Day3

Page 2: C Programming - Refresher - Part III

User defined data types

Page 3: C Programming - Refresher - Part III

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

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

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

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

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

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

Hands-on!

Page 10: C Programming - Refresher - Part III

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

Hands-on!

Page 12: C Programming - Refresher - Part III

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

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

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

THANK YOU