Broad Network


typedef Specifier in the C Computer Language

Part 38 of the Complete C Course

Foreword: typedef Specifier in the C Computer Language

By: Chrysanthus Date Published: 22 Jun 2024

Introduction

A declaration without definition, consists of an identifier with preceding types. If the declaration is very long, then the types, without the identifier, can be replaced with one user named type. User named type is just a name given by the programmer, for all the types preceding the identifier. This section gives some examples of that.

Integer Example
For the declaration,

    unsigned long int inte;

inte is the identifier. "unsigned" means that any value of inte is from zero and higher (positive). "long" means, the byte width for the integer is 8, and not 4 as for int. "short" which is not involved here, means the byte width for the integer is 2. Let the user name be uli for "unsigned long int". The shortened type name would be declared (created) as:

     typedef unsigned long int uli;

"typedef" is a reserved word. The following program illustrates this:

    #include <stdio.h>

    int main()
    {
        typedef unsigned long int uli;
        uli inte = 4;
        printf("%li\n", inte);

        return 0;
    }

The output is, 4. So uli becomes a substitute for "unsigned long int".

Pointer Example
Consider the following code segment:

        unsigned long int pointed = 4;
        unsigned long int * pointer = &pointed;
        printf("%li\n", *pointer);

The output is, 4. Let the user type name be ulip for "unsigned long int *". The identifier of interest is "pointer". The typedef declaration is:

    typedef unsigned long int* ulip;

The following program illustrates the case for a pointer:

    #include <stdio.h>

    int main()
    {
        unsigned long int pointed = 4;
        //unsigned long int * pointer = &pointed;
        typedef unsigned long int* ulip;
        ulip pointer = &pointed;
        printf("%li\n", *pointer);

        return 0;
    }

The output is, 4.

struct Example
The declaration,

    struct {char *name; int age; float salary; char *HQ;};

can be given just the one word name,  Accountants, as follows, with the typedef reserved word:

        typedef struct {char *name; int age; float salary; char *HQ;} Accountants;

Note that there is no semicolon after the closing curly bracket. And then a struct object will be declared as follows:

        Accountants employee1;

The identifier of interest here, is employee1. The following program illustrates this:

    #include <stdio.h>

    int main()
    {
        typedef struct {char *name; int age; float salary; char *HQ;} Accountants;
        
        Accountants employee1;

        employee1.name = "John Smith";
        employee1.age = 30;
        employee1.salary = 1234.12;
        employee1.HQ = "Msc";

        printf("%s\n", employee1.name);
        printf("%i\n", employee1.age);
        printf("%f\n", employee1.salary);
        printf("%s\n", employee1.HQ);

        return 0;
    }

The output is: "John Smith", "30", "1234.119995", "Msc", each on a separate line.



Related Links

More Related Links

Cousins

BACK NEXT

Comments