Learning point C language (29): Data Type-define a new type (typedef)

Source: Internet
Author: User
The custom type name must be capitalized to indicate that this is a custom type.
1. Rename unsigned long to uint:
 
# Include <stdio. h> int main (void) {typedef unsigned long uint; uint num = 1234567890; printf ("% lu \ n", num); getchar (); Return 0 ;}

  

2. rename a structure:

 
# Include <stdio. h> int main (void) {struct rec {char C; int I; long d ;}; typedef struct rec myrec; myrec R1; r1.c = 1; r1. I = 2; r1.d = 3; printf ("% d, % d, % d \ n", r1.c, r1. I, r1.d); printf ("structure size: % u, % u, % u ", sizeof (struct REC), sizeof R1, sizeof (myrec); getchar (); Return 0 ;}

  

3. Define the integer pointer as: pint:

# Include <stdio. h> int main (void) {typedef int * pint; int num = 123; pint P = & num; printf ("% d, % P \ D", * P, p); getchar (); Return 0 ;}

  

4. Used for structure definition at the same time:

 
# Include <stdio. h> int main (void) {struct rec1 {int X, Y;} R1 = {11, 22 }; /* simultaneously defines the variable R1 */typedef struct rec2 {double A, B;} rec3;/* at this time, rec3 is not a variable, it is a newly defined type */rec3 r2 = {1.1, 2.2}; printf ("% d, % d \ n", sizeof (struct rec1), sizeof (struct rec2 )); printf ("% d, % d, % d \ n", sizeof (rec3), sizeof R1, sizeof R2); printf ("\ n % d, % d ", r1.x, r1.y); printf ("\ n % G, % G", r2.a, r2. B); getchar (); Return 0 ;}

  

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.