Wei Basic iOS Learning Note II C language Basics-02

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.