13 - C basics - user defined data types
13 - C basics - user defined data types
/c/AhmedHatemS
What is the content?
1. typedef
2. enumeration
3. struct
Data Types
• C provides a capability that enables you to assign an alternate name to a datatype. This is done with a
statement known as typedef.
typedef type_description type_name;
• The statement: typedef int Counter; defines the name Counter to be equivalent to the C data type
int. Variables can subsequently be declared to be of type Counter, as in the following statement:
Counter j, n; the C compiler treats the declaration of the variables j and n, shown in the preceding
code, as normal integer variables.
• The main advantage of the use of the typedef in this case is in the added readability that it lends to the
definition of the variables.
• the typedef statement does not actually define a new type, only a new type_name.
• Write as normal variable then write typedef in its start!
ex. typedef char Linebuf [81]; defines a type called Linebuf, which is an array of 81 characters.
Then if we wrote Linebuf text, inputLine; it means we have two variables, text and
inputLine which are in the type of array of char (array of Linebuf which is an array of char).
enum
• An enumerated type is a user defined data type. It is defined by giving a name for the type and then giving a list of
labels, which are the only values that a variable of that type can have.
• Enumerated types allow the creation of specialized types that support the use of meaningful labels within a
program.
• They promote code readability with very little overhead in memory or runtime cost.
Example: You can define a data type for a year’s seasons. So, if you define a variable period of type season, it can
have any value of the seasons’ labels which were listed.
enum Season { WINTER, SPRING, SUMMER, FALL };
enum Season Period;
Period = WINTER;
enum con.
• If numeric values are not specified, identifiers are assigned consecutive values starting with 0:
enum Direction { NORTH = 0, SOUTH = 1, EAST = 2, WEST = 3 };
is equivalent to
enum Direction { NORTH, SOUTH, EAST, WEST };
• Unless specified, the value assigned to an enumeration constant is 1 more than the value of the previous constant:
enum MyEnum { ONE = 17, TWO, THREE, FOUR = -3, FIVE };
results in these values
ONE = 17, TWO = 18, THREE = 19, FOUR = -3, FIVE = -2
• Again “DON’T FORGET”, the C compiler treats enumeration identifiers as integer constants. Beginning with the first
name in the list, the compiler assigns sequential integer values to these names, starting with 0.
enum con.
• You can use the enumerated type to declare symbolic names to represent integer constants.
• By using the enum keyword, you can create a new "type" and specify the values it may have.
• enum constants are type int; therefore, you can use them wherever you want to use an int.
Homogeneous data type, is the data type which contains only similar type of
data.
Heterogenous data type, is the data type which contains a verity of dissimilar
type of data.
struct con.
• Structure: a tool for grouping heterogeneous elements together.
• Using this method, you must keep track of three separate variables for each date that you use in the
program — even though these variables that are logically related. It would be
much better if you could somehow group these sets of three variables together. This is what the structure in
C allows you to do!
struct con.
struct date
Defines type struct date, with 3 fields of
{ type int.
The names of the fields are local in the context
int month; of the structure.
A struct declaration defines a type: if not
int day; followed by a list of variables it reserves no
storage; it merely describes a template or shape
int year; of a structure.
};
struct date today, purchaseDate; Defines variables of type struct date.
int day;
int year;
} Date;
Date today, purchaseDate;
today.year = 2021;
today.month = 4;
today.day = 1;
struct con.
typedef struct bankAccount account1.name[0] = ‘A';
account1.name[1] = ‘m';
{ account1.name[2] = ‘e';
char name[15]; account1.name[3] = ‘e';
account1.name[4] = ‘r';
int accountNo; account1.name[5] = '\0';
double balance; account1.accountNo = 123456;
account1.balance = 500.0;
Date birthday; account1.birthday.day = 1;
} BankAccount; account1.birthday.month = 4;
account1.birthday.year = 2021;