You are on page 1of 15

COMS11500: Introduction to C

10 Dev, 2013
Basic data types

Outline

1 Basic data types


Revision

2 Custom data types


The typedef keyword
Enumerations
Basic data types Revision

In the previous lectures

Basic data types:


char (signed, unsigned)
int, short, long, long long (signed, unsigned)
float, double, long double
void
Pointers and arrays
Custom data types

Outline

1 Basic data types


Revision

2 Custom data types


The typedef keyword
Enumerations
Custom data types The typedef keyword

Defining custom types using typedef

typedef description name;


The easy way to use types with complex names, such as those
described in the previous section, is to declare simple synonyms for
them.
The keyword typedef, permits defining
an alias for an existing type that is context dependent
new more complicated types in conjunction with the C facilities for
constructing compound data types, e.g enum and struct.
The syntax includes a description of the datatype and an identifier
For the scope of the typedef declaration, the identifier is synonymous
to (and can be used in place of) the decsription
Custom data types The typedef keyword

Defining custom types using typedef

typedef description name;


Example:
typedef unsigned int uint_t; /* uint_t is synonymous to unsigned int */
typedef double * dblptr_t; /* dblptr_t is synonymous to double * */

uint_t char_pos=10; /* same as: unsigned int char_pos=10; */


uint_t * ptr=&char_pos; /* same as: unsigned int *ptr=&char_pos; */
dblptr_t ptr; /* same as: double * val; */
dblptr_t fcn(uint_t * ptr, uint_t val);
/* same as: double * fcn(unsigned int * ptr, unsigned int val); */

In the examples above, typedefs are just synonyms for existing C types
and are used in variable and function declarations in the same way built-in
data types are used.
Custom data types The typedef keyword

Defining custom types using typedef

typedef description name;


Example: define a boolean data type
#define TRUE 1
#define FALSE 0
typedef int bool_t; /* bool_t is synonymous to int */

bool_t flag = TRUE; /* same as: int flag = 1 */


bool_t is_valid(double * arr); /* same as: int is_valid(double * arr); */

typedefs make the code more readable.


Custom data types The typedef keyword

Defining custom types using typedef

typedef description name;


Example: correctly define a 64-bit integer in different systems
#if defined(/* identifier for 64-bit systems goes here */)
typedef long int64_t;
#else
typedef long long int64_t;
#endif

/* use uint64_t here */

typedefs make the code more portable.


Custom data types The typedef keyword

Defining custom types using typedef

typedef description name;


file mytypes.h:

#ifndef MYTYPES_H
#define MYTYPES_H

#define TRUE 1
#define FALSE 0
typedef int bool_t; /* bool_t is synonymous to int */

#endif

now include mytypes.h wherever you want to use bool_t. Notice that C99 already defines a
boolean data type

You can put all your typedefs in a header file.


Custom data types Enumerations

Defining enumerated data types

enum name list;


Enumerations are integer data types that you define in a program
yourself.
The definition of an enumeration begins with the keyword enum
followed by:
an optional identifier for the enumeration
and a list of the type’s possible values, with a name for each value
Custom data types Enumerations

Defining enumerated data types


enum name list;
Example:
enum color_t {BLACK, RED, GREEN, YELLOW, BLUE, WHITE=7, GRAY};

The identifier color is the tag of this enumeration.


The identifiers in the list are the enumeration constants and have the
type int.
You can use these constants anywhere within their scope, e.g. in
variable declarations or switch/case constructs:
enum color_t a_color; /* color is an enumeration of type color_t */
a_color = WHITE;
...
switch(a_color){
case BLACK: {...}
case RED: {...}
...
}
Custom data types Enumerations

Defining enumerated data types

enum name list;


Example:
enum color_t {BLACK, RED, GREEN, YELLOW, BLUE, WHITE=7, GRAY};

Enumeration constants are determined either implicitly by position in


the list, or explicitly by initialization.
A constant without an initialization has the value 0 if it comes first in
the list, or the value of the preceding constant plus one.
The constants listed above have the values 0, 1, 2, 3, 4, 7, 8.
Custom data types Enumerations

Defining enumerated data types

enum name list;


Example:
enum days_t {MON, TUE, WED, THU, FRI, SAT, SUN};

enum days_t a, b = WED, c = 2; /* c is also equal to WED /*

Enumerated types and constants correspond to standard interger types, so


you can intermix them in standard C expressions.
Custom data types Enumerations

Defining enumerated data types

enum name list;


Example:
enum {CLOSE, OPEN, ON=1, OFF=0}; /* define some constants */

enum {RED,GREEN,BLUE} fg_color, bg_color;


int a = ON, b = OPEN;

Tags are optional; makes sense when:


you don’t need to declare any variables of the given type. A nice way
to define integer constants. Better that using long lists of #defines.
you immediately declare a set number of variables of the given type
Custom data types Enumerations

Defining enumerated data types

enum name list;


Example:
typedef enum {FALSE, TRUE} bool_t; /* use of typedef */

bool_t flag = FALSE;


bool_t is_valid(double * arr, int len);

enum can be used in conjuction with typedef.


In this case, you do not need to reuse the enum keyword when
declaring variables of the given enumeration type.

You might also like