9_Objective-C Typedef

The Objective-C programming language provides a keyword called typedef, which you can use to give a type a new name.

Following is an example to define a term BYTE for one-byte numbers:

typedef unsigned char BYTE;

Also you can use lowercase, as follows:

typedef unsigned char byte;

After this type definition, the identifier BYTE can be used as an abbreviation for the type unsigned char, for example:

BYTE  b1, b2;

You can use typedef to give a name to user-defined data type as well.

Example typedef with struct

typedef vs #define

The #define is a Objective-C directive, which is also used to define the aliases for various data types similar to typedef but with following differences:

  • The typedef is limited to giving symbolic names to types only.
  • The typedef interpretation is performed by the compiler
  • #define can be used to define alias for values as well, like you can define 1 as ONE, etc.
  • #define statements are processed by the pre-processor.

Example of simplest usage of #define:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: