Online Tutorials & Training Materials | STechies.com
Register Login

C Typedef: Syntax, Use, Typedef Struct

|| 0

C Typedef: Syntax, Use, Typedef Struct
Stechies

During the development of software or some complicated code, the programmers may require to later the existing paradigms of the programming language they are using. They may have to alter the data types of the programming language. typedef is a reserved keyword that can be used for this purpose. The keyword is often used when handling complex data structures like struct or union, and also for changing the data types of common ones like integer and string. The following article is focused on the typedef keyword, its uses, the basic syntax and about the typedef struct. 

What is typedef in C?

The typedef is C keyword that is used for providing an instruction to the compiler to assign an alias or an alternative name to an already existing keyword. This is used along with existing data types that may be complicated or confusing according to the programmer’s perspective.

Syntax of C typedef

The following syntax can be used for declaring a typedef in the C programming language:

typedef <existing_name_of_data_type> <alias_data_type>;

For example,

typedef signed long SL;

 Use of typedef in C

The different uses of typedef in C are as follows:

  • By renaming the existing built-in data types, the programmers are able to reduce the complexity in their code. It can be used for making the code more clear for others to read and remove errors while debugging.
  • As the keyword can be applied for user-defined data types as well, it lets the user create a level of abstraction on top of the original data types being used. This way the focus could be put on the use of the variables and the concept.
  • Using the typedef keyword will, in turn, reduce the amount if the time required to code the programs that will positively affect the release cycles of the ultimate software product.
  • typedef can also be used to provide an assumed name to pointers in C

What is typedef Struct in C?

A struct in C is a composite data type that is used for declaring data objects that have similar data members or attributes. The typedef struct is such a data type where the typedef keyword is used along with the struct data type. For example,

typedef struct struct_name

{

    data_type member1;

    data_type member2;

    data_type member3;

} type_name

Here the typedef keyword is used to alter the names of the existing structures. Therefore, the struct_name will be changed to type_name. Other objects can be created through the structure and the new type_namem without the data members getting affected.

New variables can be declared by the following syntax:

type_name T1, T2;

Simple Example for typedef struct in C

#include <stdio.h>
#include <stdlib.h>

typedef struct stechies
{
    int number;
    char c;
}stechies;

int main(void)
{
    int x = 5;
    int i = 0;

    stechies myStruct;

    myStruct.number = 10;
    myStruct.c = 'F';

    printf("%d  %c", myStruct.number, myStruct.c);

    return 0;
}

OUTPUT :

10   F

Typedef with Array in C

//Simple Example for typedef Array;

#include <stdio.h>
#include <stdlib.h>

typedef struct smartArray
{

 int *array;
 int length;

}smartArray;

smartArray *createSmartArray(int length);

int main(void){

 int x = 5;
 int i = 0;

 smartArray *myArray = createSmartArray(x);

 for (int i = 0; i < myArray->length; i++)
 {
  myArray->array[i] = i;

  printf("%d ", myArray->array[i]);
 }

 return 0;
}

smartArray *createSmartArray(int length)
{
 smartArray *newArray = malloc(sizeof(smartArray));

 newArray->length = length;

 newArray->array = malloc(sizeof(int)* length);

 return newArray;

}

Output

0 1 2 3 4

...Program finished with exit code 0
Press Enter to exit console.

Typedef function pointer in C

typedef int *ipointer;
ipointer stechies, stech;        // both stechies and stech are int* type

ipointer stechies2, *stech2;     // stechies2 is int* type, but stech2 is int** type
                            // same as: ipointer stechies2;
                            //          ipointer *stech2;

Conclusion

It has been observed that the typedef keyword is used for making the code more simple and readable. The keyword can be used along with arrays, structures, pointers, structure pointers, function pointers. It is also used in type casting a variable as a typecast syntax. According to the founders of C programming, Kernighan and Ritchie said that the typedef is used for making the code more portable and maintainable. However, in cases where the underlying data types are important, the typedef keyword must be avoided as it might lead to some confusion.