0% found this document useful (0 votes)
32 views

Week-13 Struct Enum Typedef

Enum, Typedef & Structs are C programming concepts for organizing data. [1] Enum allows defining enumeration constants with unique names that can represent different possible values like true/false or different data types. Typedef defines new data type names for existing types like int. [2] Structures group different variable data types together under one type, allowing multiple related pieces of data to be accessed or passed together as one object. This is useful for representing complex objects with multiple attributes. [3] Structures can be defined with or without labels, initialized with starting values, used as arrays, accessed via pointers, and passed as arguments to functions to organize and manage related program data.

Uploaded by

sahin dokuyucu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Week-13 Struct Enum Typedef

Enum, Typedef & Structs are C programming concepts for organizing data. [1] Enum allows defining enumeration constants with unique names that can represent different possible values like true/false or different data types. Typedef defines new data type names for existing types like int. [2] Structures group different variable data types together under one type, allowing multiple related pieces of data to be accessed or passed together as one object. This is useful for representing complex objects with multiple attributes. [3] Structures can be defined with or without labels, initialized with starting values, used as arrays, accessed via pointers, and passed as arguments to functions to organize and manage related program data.

Uploaded by

sahin dokuyucu
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Enum, Typedef & Structs

Assist. Prof. Dr. Umit ATİLA


Enum
● Enumaration constants are like symbolic
constants.
– Enum values are set automatically
– Values start at 0 and incremented by 1
– Need unique constant names
● We can create our own data type by enum
● For example we can create a new boolean type
in which 0 is false and 1 is true
Enum
Enum
Enum
● Notice that we use enum keyword for each variable definition.
● We have two alternative ways for not writing enum for each variable
definition
– Defining variable with enum definition
– Using typedef
● Enum is useful if you have some data to be grouped or arranged.
Some examples:
– enum education { primarySchool, secondarySchool, highSchool, graduate,
master };
– enum education student;
– enum sex { male, female };
– enum sex person;
Enum
Typedef
● Used to name data types with other user
defined names.
● Format of typedef:
– typedef old_datatype_name new_datatype_name
– typedef int tamsayi
● defines int as tamsayi
Typedef
Enum
● If enum is defined globally it can be used as parameter to a function
Enum
Structures
● Used to group different types of variables in one
structure.
● Structures are important for object oriented
programming
Structures
Structures

● If not used struct in this sample we would have to define 9 different variables.
● We make it with 3 variables.
● Think about a program that takes 20 informations for a person.
● With 3 person you have to define 60 different variables.
● Another advantage using struct is with copying data on an other variable.
● you = yourSister assignment copies your sister's data on to your data
Nested Structures
Labeling Structures

● Labeling structures has many advantages.


● If dont, you can just define variables when defining struct.
● If you use labels you can define as many variables as you want from struct in any point
of your program
● We have to create a variable using label to start using structure
Initial Values for Structures
● Structures can be defined with initial values of
its variables.
● Order of the values must fit to the structure's
order.
● You can give initial values for structures defined
with or without label.
Initial Values for Structures
Arrays of Structures
Accessing Structures with Pointers
Passing Structures to Functions
● Define structure globally and pass to function

You might also like