What does typedef int Score; do?
typedef only adds a name. A Score is an int in every way – it converts, compares and mixes with int without any cast.
Typedef · Exercise 1 of 4
Twelve multiple-choice questions on typedef aliases, struct and function-pointer typedefs, typedef vs #define, const with pointer typedefs, scope and C23 typeof. Pick an option to check your answer instantly.
What does typedef int Score; do?
typedef only adds a name. A Score is an int in every way – it converts, compares and mixes with int without any cast.
Which declaration is invalid?
Grammatically, typedef is a storage-class specifier, like static and extern, and a declaration may contain only one of them.
Inside typedef struct node { … } Node;, how must the "next" pointer be declared?
The alias Node only exists after the closing Node;. Inside the braces, the type is still known only by its tag, struct node. (An anonymous struct cannot refer to itself at all.)
Given typedef char *String;, what is const String s;?
A typedef is not a text substitution: const qualifies the whole type String, which is a pointer. So the pointer becomes const. For read-only characters write typedef const char *CString;.
With #define PTR int *, what are a and b in PTR a, b;?
The macro expands to int * a, b; and the * belongs only to a. With typedef int *PTR; both would be pointers.
Which typedef defines Op as a pointer to a function taking two ints and returning an int?
The brackets bind * to the name: "Op is a pointer to a function (int, int) returning int". Without them, int *Op(int, int) would be a function returning int *.
After typedef int Row[4];, what is the type of Row grid[3];?
grid is an array of 3 elements, each a Row (int[4]). So it is int[3][4], 48 bytes – handy for naming the row type of a matrix.
Is a typedef inside a function body allowed?
typedef names follow the normal scope rules. A local typedef is visible to the end of its block and hides an outer typedef with the same name.
Why should your own typedefs avoid names like point_t?
The C standard itself uses _t names (size_t, int32_t), and POSIX reserves the whole suffix. Code that ignores this usually works – until a new system type with the same name appears.
With typedef int Meters; and typedef int Seconds;, what happens on Meters m = some_seconds;?
Aliases are interchangeable. For real type safety, wrap the value in a struct: typedef struct { int value; } Meters; – then mixing units is a compile error.
Timed online tests with instant scores and explanations.