Typedef in Objective-C

Typedef in detail

typedef allows us to assign a new name to an existing data type. For instance, typedef unsigned char CAT; makes CAT an alias for unsigned char, so from then on, you can use CAT as a shorthand for unsigned char, like in the declaration CAT num1, num2;. In other words, typedef is a keyword used to create an alias for predefined or user-defined data types, making code more readable. A typedef essentially helps simplify long data type names by creating an alias.

The typedef specifier is used in a declaration to indicate that it is creating a new type, not declaring a variable or function. Typically, typedef appears at the start of the declaration but can also appear after the type identifier or between two type identifiers. A typedef can define multiple identifiers on the same line, such as for integer and pointer types, arrays, functions, or class types. After using typedef, the created identifier is synonymous with the type it represents. Importantly, typedef cannot be combined with other specifiers apart from typedef.

Syntax:

typedef unsigned long long BUTTON;

Example 1:

// Objective-C program to demonstrate the use of typedef
#import <Foundation/Foundation.h>

// Defining new names for existing data types using typedef
typedef int MOUSE;
typedef unsigned long long CAT;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    // Creating variables using the new typedef names
    MOUSE x = 10000;
    CAT y = 121234;

    // Displaying the values
    NSLog(@"Value of x is %i", x);
    NSLog(@"Value of y is %llu", y);

    [pool drain];
    return 0;
}

Output:

Value of x is 10000
Value of y is 121234

Example 2:

// Objective-C program to demonstrate the use of typedef with struct
#import <Foundation/Foundation.h>

// Creating a struct and using typedef to define a new type
typedef struct Author {

    // Struct variables
    NSString *Title;
    NSString *Publisher;
    int Book_Number;
} Author;

// Driver code
int main() {
    // Creating an object of the Author struct
    Author author;

    // Assigning values to the struct variables
    author.Title = @"Learn Objective-C for Beginners";
    author.Publisher = @"TechBooks";
    author.Book_Number = 123;

    // Displaying the values
    NSLog(@"Book title: %@", author.Title);
    NSLog(@"Book publisher: %@", author.Publisher);
    NSLog(@"Book ID: %d", author.Book_Number);

    return 0;
}

Output:

Book title: Learn Objective-C for Beginners
Book publisher: TechBooks
Book ID: 123

Difference Between typedef and #define

Featuretypedef#define
TypeCompiler tokenPreprocessor token
PurposeDefines new typesDefines macros
UsageCreates an alias for typesCreates constants for numbers, strings, and expressions
ScopeLimited to types onlyCan define constants for values and expressions too
Exampletypedef int MY_INT;#define PI 3.14

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *