C Language Learning notes--type definition & Union

Source: Internet
Author: User

    • First, type definition

C-Language custom data type (typedef)

    The C language provides a function called TypeDef to declare a new name for an existing data type.

typedef int LENGTH;

Makes Length an alias of type int .

In this way, the length can be substituted for int appearing in defining variables and declaring functions where.

Length A, B;

Length A[10];

  The name of the new type is the alias of the original type, which improves the readability of the program.

typedef struct {

int day;

int month;

int year;

} Date; Declares a struct type whose alias is date

    • Ii. United

Store

All the members share a space.

Only one member is valid at the same time.

The size of the Union is its largest member.

Initialization

  Initializes the first member of a

Union anelt{

int i;

char c;

} anelt1,anelt2;

sizeof (Union ...) sizeof Maximum value per member

    

      1#include <stdio.h>2       3typedef Union {4         inti; 5         Charch[sizeof(int)]; 6} CHI; 7       8 intMainintGrcConst Char*grv[])9 {     TenChi, Chi;  One         inti;  ACHI.I =1234;  -          for(i=0; i<sizeof(int) ; i++){      -printf"%02HHX", Chi.ch[i]);  the         }      -printf"\ n");  -       -         return 0;  +}

C Language Learning notes--type definition & Union

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.