C language: typedef

Source: Internet
Author: User

TypedefIs used to declare custom data types, with a variety of original data types to simplify programming PurposeType Definition keyword.

 

Method for defining types using typedef

① Write the definition body (for example, int I) according to the definition variable method ).

② Replace the variable name with the new type name (for example, replace I with count ).

③ Add typedef. (For example, typedef int count) at the beginning ).

④ Then you can define variables with the new type name.

 

Note:

(1) typedef can be used to declare various types of names, but cannot be used to define variables.

(2) Using typedef only adds a type name to an existing type without creating a new type.

(3) When the same type of data is used in different source files, typedef is often used to declare some data types and put them in a single file, then, use the # include command to include them in the files that require them.

(4) The use of typedef facilitates the general application and Transplantation of programs.

(5) typedef is similar to # define. For example, typedef int count; # define count int indicates Int. But in fact, they are different.. # Define is used during pre-compilation.

, It can only be replaced with a simple string, AndTypedef is processed during compilation.. In fact, it is not a simple string replacement,Declare a type as if the variable is defined.

 

Struct and typedef

Struct {

Int X;

Int y;

} Test1;

The test1, test1.x, and test1.y struct are defined and can be used in the statement.

 

Struct test {

Int X;

Int y;

} Test1;

The test1, test1.x, and test1.y struct are defined and can be used in the statement. Compared with the previous one, test is saved.

 

Typedef struct test {

Int X;

Int y;

} Text1, text2;

The struct alias is text1 or text2. Actually used in the statement, but also to write:Text1 Te;
Then you can useTe. x Te. Y

Typedef struct {

Int X;

Int y;

} Test1;

Same as the preceding one, you must also write:Test1 my_st;Available
My_st.x
AndMy_st.y

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.