🧧

Reading C Type Declarations

TypeQuiz 4 Material

Notes

C gibberish ↔ English Website

C Data Types

Base Types

Derived Types

Size

Depends on the platform. 16-bit and 64-bit computer has different size

Minimum size

Sizeof()

sizeof is a compile-time constant reflecting the number of bytes held by a data type or instance

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

sizeof char is 1

Boolean

C historically did not have a Boolean data type. Instead, we can use integers to represent Boolean values.

String

char s[6] = "hello";   
// same as  
s.stringz "hello"; // 6 memory locations, end with 0
//s is the memory address where the string starts

Declaration vs Definition

A declaration in C introduces an identifier and describes its type, be it a scalar, array, struct, or function. A declaration is what the compiler needs to accept references to that identifier (for type checking). You may have as many declarations of an identifier as you want within a scope as long as they are consistent.

A definition in C actually instantiates/implements this identifier. For instance, a definition allocates storage for variables or defines the body of a function. You may only have one definition of an identifier within a scope.

Declaration

There are two parts:

  1. Base Type
    • This is the type (or struct s or a typedef), and optionally a storage class and/or a type qualifier
    • The Base Type applies to all names up until the semicolon
  1. List of Declarations, separated by commas
    • Each of these declares a type fr each identifier(the variable name)
    • Each is based on the Base Type, but stands alone
    • Consists of sensible combinations of "pointer to", "array of", or "function returning", ending with the Base Type

Rules on Declaration

  1. Remember the precedence of the declarators
    1. () and [] declarators get processed first
    1. gets processed last
    1. Parentheses change the precedence order (just as in expressions)
  1. Read or form the declaration from the inside out
    1. Example: int *(**f)() —> f is a pointer to a pointer to a function returning a pointer to int.
    • More on the example above

Parsing

determining which grammar productions (rules) were used to generate a sentence

Declaring vs Using Pointers and Arrays

*, (), and [] have different meanings in declarations and executable statements. Therefore, their behavior allows you to “unwind” a type just as the declaration would wind it

Typedef

Typedef is a shortcut that creates a new alias for a type. It does NOT create a new type

Start the declaration with “typedef” and put the alias name where you would put the variable name

Reading and Forming Declarators

How to read a declaration

  1. Find the identifier name, e.g. "x" in "int x;"
  1. Read as far right as you can until:
    1. You hit a close paren ')', or
    1. You reach the end of the declaration
  1. Go back and read as far left as you can until:
    1. You hit an open paren '(', or
    1. You reach the beginning of the declaration
  1. If you hit parentheses, exit the parentheses
  1. Go to 2 and repeat until you read the whole declaration

Questions & Answers