Types in C - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Types in C

Description:

An enumerated type in C is a set of values represented by identifiers (such as Mon, Tue, etc. ... typedef enum {Mon, Tue, Wed, Thu, Fri, Sat, Sun} weekdayT; ... – PowerPoint PPT presentation

Number of Views:50
Avg rating:3.0/5.0
Slides: 16
Provided by: vdou8
Category:
Tags: enum | types

less

Transcript and Presenter's Notes

Title: Types in C


1
Types in C
  • C has two kinds of data types
  • Basic types
  • integer (int)
  • floating point (float, double)
  • character (char)
  • void (has no values and is used mainly as a
    function return type)
  • Derived types (i.e. constructed from basic or
    other derived types)
  • arrays
  • pointers
  • enumerations
  • structures
  • unions

2
Types in C
  • For any variable in C, the value is one of a set.
  • Which set is determined by the variable's data
    type.
  • An int value is chosen from a set of 232 integers
  • A char value is chosen from a set of 28
    characters
  • But what if you want a non-numerical set such
    as Mon, Tues, Wed, Thu, Fri, Sat, Sun

3
Enumerations
  • We want to define a new (derived) type so that a
    variable of this type can have one of the
    following values Mon,Tues,Wed,Thu,Fri,Sat,Sun(
    HINT we just enumerated all the allowable
    values)
  • An enumerated type in C is a set of values
    represented by identifiers (such as Mon, Tue,
    etc.) enum Mon, Tue, Wed, Thu, Fri, Sat, Sun

4
Enumerations
  • Since we want to be able to create variables of
    that type, we should give it a nametypedef
    enum Mon, Tue, Wed, Thu, Fri, Sat, Sun
    weekdayT
  • Type definitions are placed in header files (or
    before main())

typedef is used to give a name to a new type
Here's the definition of the new type
the name of the new type. Note that it ends in T
for Type
5
Enumerations
  • typedef enum Mon, Tue, Wed, Thu, Fri, Sat, Sun
    weekdayT
  • Now, we can declare and use variables of type
    weekdayT weekdayT today, tomorrow today
    Mon tomorrow Tue

6
Enumerations
  • typedef enum Mon, Tue, Wed, Thu, Fri, Sat, Sun
    weekdayT
  • Mon, Tue, Wed, etc. are called enumeration
    constants.
  • Internally, they are given constant integer
    valuestypedef enum Mon, Tue, Wed, Thu, Fri,
    Sat, Sun weekdayT
  • CAREFUL! There are no restrictions on the value
    today 135 is allowed (but meaningless)

0 1 2 3 4 5 6
7
Enumerations
  • Since enumeration constants are integers, we can
    do math with them

typedef enum Mon, Tue, Wed, Thu, Fri, Sat, Sun
weekdayT int main () weekdayT today today
Sun tomorrow (today 1) 7 / this makes
tomorrow Mon / return 0
8
Structures
  • A variable stores one piece of information for an
    entity (e.g. the value of one integer).
  • But what if we need to store several pieces of
    information that have different types and refer
    to the same entity?
  • For example, we can declare a char variable to
    store the grade of a student, but what if we also
    want to store the name and if of that student at
    the same time? Can we use a single variable to do
    that?
  • The answer is YES.

9
Structures
  • In order to store the name, grade and id of a
    student in a single variable, that variable's
    type must consist of three components one for
    the name (string), on for the grade (char), one
    for the id (int).
  • Such a type is called a structure.
  • A structure is a collection of named components
    (called fields) that may have different types.

10
Structures
typedef struct char name char grade int
id studentInfoT
3 fields
Now, we can declare variables of this type
studentInfoT aStudent
11
Structures
  • Use the dot operator to access individual fields
    by name

studentInfoT aStudent aStudent.name (char)
malloc(20 sizeof(char)) strcpy(aStudent,name,
"Smith, John") aStudent.grade
'A' aStudent.id 1234
12
Structures
  • To store such information for the whole class,
    declare an array of structures

studentInfoT classMAXSIZE .... printf("Type
the grade for each student\n") for (i0
iltMAXSIZE i) printf("s ",
classi.name) scanf(" c", classi.grade)
13
Structures
  • We may also declare a pointer to a struct

studentInfoT pStudent pStudent
(studentInfoT) malloc(1 sizeof(studentInfoT))
(pStudent).id 1234 / shorthand that does
the same thing / pStudent -gt id 1234
the arrow operator is made of a dash and the
greater than sign
14
Structures
  • If we would like the size to be determined at
    runtime, we may use a pointer to studentInfoT
    records

studentInfoT class printf("How many students
in the class?") scanf("d", size) class
(studentInfoT ) malloc (size
sizeof(studentInfoT)) / allocate space for the
name of each student / for (i0 iltsize i)
classigtname (char ) malloc(20
sizeof(char)) ...
15
Structures
  • The type of a structure field may be another
    structure

typedef struct char first char last
fullNameT typedef struct fullNameT
name char grade studentInfoT
... studentInfoT aStudent aStudent.name.first
"John" aStudent.name.last "Smith" ...
Write a Comment
User Comments (0)
About PowerShow.com