1. typedef
In the previous article, when we defined a bool type with a macro definition, we now define the type in a more professional way.
typedef int Bool;
int main (void)
{
Bool flag=1;
if (flag)
{
printf ("True");
}
Else
{
printf ("false");
}
}
The role of a TypeDef is type definition.
The type definition has three advantages:
1. Easy to read. For example, we can define a dollar and then dollar money=1000, which is easier to read than int.
2. Easy to modify, if one day dollar to change to double type, then I just need to modify the type definition.
3. Easy to transplant. In the above, we said that the C language is platform-related, so we might as well define some int32,int64 so that when we migrate, we just need to modify these type definitions.
There are two advantages to a type definition than a macro definition:
A. Type definitions are more powerful, and in particular, arrays and pointer types cannot be defined as macros.
B. In effect, a macro definition is simply a string substitution, whereas a type definition has a certain encapsulation.
2. sizeof operator
This operator is very simple and does not dwell on it, but only emphasizes one point. Because the sizeof operator returns a type of size_t, this is a custom type, and therefore, to prevent incompatibility problems caused by different platforms. We'd better make a type conversion before the display.
int main (void)
{
printf ("%d", (int)sizeof (int));
}
Wei Basic iOS Learning Note II C language Basics-02