Reader small image

You're reading from  Learn C Programming. - Second Edition

Product typeBook
Published inAug 2022
PublisherPackt
ISBN-139781801078450
Edition2nd Edition
Right arrow
Author (1)
Jeff Szuhay
Jeff Szuhay
author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay

Right arrow

Chapter 10: Creating Custom Data Types with typedef

As we saw in the last two chapters, C allows you to define your own types from enumerations (enum types) and structures (struct types). C also allows you to redefine types for the convenience of naming and to provide clarity about how to use the redefined type. The redefined type becomes a synonym for the original type. The purpose of this chapter is to create a synonym of one type from another, which is extremely useful to express the purpose of variables, not only through their names but also through their redefined types.

The following topics will be covered in this chapter:

  • Creating custom named types from intrinsic types
  • Creating new synonyms from other custom named types
  • Simplifying the use of enumerations
  • Simplifying the use of structures
  • Exploring some important compiler options
  • Using header files for custom types and the typedef ...

Technical requirements

Continue to use the tools you chose from the Technical requirements section of Chapter 1Running Hello, World!.

The source code for this chapter can be found at https://github.com/PacktPublishing/Learn-C-Programming-Second-Edition/tree/main/Chapter10.   

Renaming intrinsic types with typedef

In Chapter 3, Working with Basic Data Types, we looked at some of C's basic data types – whole numbers (integers), real numbers (floating point and complex numbers), characters, enumerations, and Boolean values. We called them intrinsic data types since they are built into the language and are always available. By referring to these types specifically as data types, we give focus to the content of variables of those types – containers of data that can be manipulated. However, the use of the data type term is not strictly accurate. In reality, these types are classified in C as arithmetic types. C has other types, some of which we have encountered – function typesstructure types, and void types – and some of which we have not yet encountered – array typespointer types,...

Simplifying the use of enum types with typedef

Before we examine the use of typedef with enum types, we must first complete the picture of using enum types. Remember that defining a new type does not require memory allocation. Only when we declare variables of a given type is memory allocated to the variables. In the last two chapters, we used enum types by first defining them and then separately declaring variables of that type, as follows:

  // First define some enumerated types.
enum Face { eOne , eTwo , eThree , ... };
enum Suit { eSpade , eHeart, ... };
  // Then declare variables of those types.
enum Face f1 , f2;
enum Suit s1 , s2;

In the preceding code fragment, we have defined two types – enum Face and enum Suit. Later, in separate statements, two variables of each type are declared – f1f2s1, and s2.

Another way to achieve the same result...

Simplifying the use of struct types with typedef

All of the considerations we explored for enumerations equally apply to structures. We will go through each of them as they apply to structures.

Before we examine the use of typedef with structures, we must first complete the picture of using structures. In the previous chapter, we used structures by first defining them and then separately declaring variables of that type, as follows:

  // First define a structured type.
struct Card { Face face; Suit suit; ... };
  // Then declare variables of that type.
struct Card c1 , c2 , c3 , c4 , c5;

In the preceding code fragment, we have defined one type, struct Card. In a separate statement, five variables of that type are declared – c1c2c3c4, and c5.

Another way to achieve the same result is to both define the structured type and declare variables...

Exploring some more useful compiler options

Up until now, we have been compiling our programs with just the -o output_file option. Your compiler, whether gcc,  clang, or icc, probably unbeknownst to you before this point, has a bewildering array of options.

If you are curious, type cc -help into your command shell and see what spews out. Tread lightly! If you do this, just understand that you will never need the vast majority of those options. Some of these options are for the compiler only; others are passed on to the linker only. They are there for both very specialized system software configurations and for the compiler and linker writers. 

If you are using a Unix or Unix-like system, type man cc in the command shell to see a more reasonable list of options and their usage.

The most important of these options, and the one we will use from now on, is -std=c17 or -std=c11. The ...

Using a header file for custom types and the typedef specifiers

Because we have explored custom types (enumerations, structures, and the typedef specifiers), it is now appropriate to explore how to collect these custom types into our own header file and include them in our program.

We have seen the following statements:

#include <stdio.h>
#include <stdbool.h>

These are predefined header files that provide standard library function prototypes – the typedef specifiers, enumerations, and structures – related to those function prototypes. When a header file is enclosed in < and >, the compiler looks in a predefined list of places for those files. It then opens them and inserts them into the source file just as if they had been copied and pasted into the source file.

We can now create our own header file, say card.h, and use it in our program...

Summary

We have seen how to create alternative names, or synonyms, for intrinsic types and custom types declared with enumerations and structures. Using typedef, we have explored convenient ways to create synonyms for intrinsic types and how the typedef specifiers simplify the use of enumerations and structures. We have also seen how synonyms make your code clearer and provide added context for the intended use of variables of that synonym type.

We have seen how it is somewhat cumbersome to declare and manipulate multiple instances of structured data of the same type without a synonym for that type, especially when there are many instances of a single structure type, such as a deck of cards.

In the next chapter, we will see how to group, access, and manipulate collections of data types that are identical in type but differ only in values. These are called arrays. Arrays help us to further model and manipulate, for instance, a deck of cards consisting...

Questions

  1. What does the typedef keyword provide for intrinsic types?
  2. What does the typedef keyword provide for enumeration and structure types?
  3. How is using typedef for enumerations different from using typedef for structures?
  4. What are two compiler switches that help us prevent undefined program behavior?
  5. What goes into a header file? What does not go into a header file?
lock icon
The rest of the chapter is locked
You have been reading a chapter from
Learn C Programming. - Second Edition
Published in: Aug 2022Publisher: PacktISBN-13: 9781801078450
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Jeff Szuhay

Jeff Szuhay is the principal developer at QuarterTil2 which specializes in graphics-rich software chronographs for desktop environments. In his software career of over 35 years, he has engaged in a full range of development activities from systems analysis and systems performance tuning to application design, from initial development through full testing and final delivery. Throughout that time, he has taught computer applications and programming languages at various educational levels from elementary school students to university students, as well as developed and presented professional, on-site training.
Read more about Jeff Szuhay