C-language advanced--enum, sizeof, typedef analysis 11

Source: Internet
Author: User

How to use enumeration types:
    • Enum is a custom type of C language
    • An enum value is an integer value that can be customized as needed
    • The value of the first enum defaults to 0
    • By default, the enum value is based on the previous defined value plus 1
    • A variable of enum type can only be defined when the discrete value
1 enum Color 2 {  3    GREEN,  4    BLUE,  5    RED  6 }; 7 8 enum Color C = GREEN; 9 Ten printf ("%d\n", c);
Special meanings of enum types:
    • The values defined in the enum are constants of the true meaning of the C language
    • Enum is used in engineering to define integer constants
1 enum   //Nameless enumeration, used to define constants2 {3Array_size =Ten,//to define the size of an array4 };5 6 intArray[array_size] = {0};7 intI=0;8  for(i=0; i<array_size;i++)9 {Tenayyay[i]=i+1; One}

Sample Analysis:
1#include <stdio.h>2 3 enum4 {5Array_size =Ten6 };7 8 enumColor9 {TenRED =0x00ff0000, OneGREEN =0x0000FF00, ABLUE =0x000000ff - }; -  the voidPrintcolor (enumColor c) - { -     Switch(c) -     { +          CaseRED: -printf"color:red (0x%08X) \ n", c); +              Break; A          CaseGREEN: atprintf"color:green (0x%08X) \ n", c); -              Break; -          CaseBLUE: -printf"color:blue (0x%08X) \ n", c); -              Break; -     } in } -  to voidInitarray (intarray[]) + { -     inti =0; the      *      for(i=0; i<array_size; i++) $     {Panax NotoginsengArray[i] = i +1; -     } the } +  A voidPrintArray (intarray[]) the { +     inti =0; -      $      for(i=0; i<array_size; i++) $     { -printf"%d\n", Array[i]); -     } the } - Wuyi  the intMain () - { Wu     enumColor C =GREEN; -      About     intArray[array_size] = {0}; $      - Printcolor (c); -      - Initarray (array); A      + PrintArray (array); the      -     return 0; $}

Usage of the sizeof keyword:
    • sizeof is the compiler's built-in indicator
    • sizeof is used to calculate the size of the memory that the type or variable occupies
    • The value of sizeof is determined at compile time

sizeof is used for type:

sizeof (type)

sizeof is used for variables:

sizeof (VAR) or sizeof Var--This non-parenthesized usage can fully illustrate that sizeof is not a function

sizeof is a C language built-in keyword instead of a function
    • All sizeof will be replaced by specific values during the compilation process.
    • The procedure does not have any relationship with sizeof.

Think about what the following program will output?

int var = 0;

int size = sizeof (var++);

printf ("var =%d, size =%d\n", var,size);

Example:
1#include <stdio.h>2 3 intf ()4 {5printf"Delphi tang\n");6     7     return 0;8 }9 Ten intMain () One { A     int var=0; -      -     intSize =sizeof(var++); the      -printf"var =%d, size =%d\n",var, size); -      -Size =sizeof(f ()); +      -printf"size =%d\n", size); +      A     return 0; at}

The output may be a bit unexpected because sizeof was replaced during compilation, so it will not participate in the run-time

Simulate a scene below

During the interview .....

Interviewer: Can you talk about the specific meaning of typedef?

Candidate: TypeDef is used to define a new type ...

Examiner: You go home and farm!

The meaning of typedef
    • typedef is used to rename an already existing data type
    • A typedef cannot essentially produce new types
    • typedef rename Type 1. You can define 2 after a typedef statement. cannot be modified by unsigned and signed
    • usage typedef type NEW_NAME
Example:
1#include <stdio.h>2 3typedefintInt32;4 5 struct_tag_point6 {7     intx;8     inty;9 };Ten  Onetypedefstruct_tag_point Point; A  -typedefstruct - { the     intlength; -     intarray[]; - } Softarray;  -  +typedefstruct_tag_list_node ListNode; - struct_tag_list_node + { Alistnode*Next; at }; -  - intMain () - { -Int32 i =- -;//int -     //unsigned Int32 II = 0; inPoint P;//struct _tag_point -softarray* SA =NULL;  tolistnode* node = NULL;//struct _tag_list_node* +      -     return 0; the}

Summary:
    • The type of enum used for the discrete value of D-meaning
    • The value defined by the enum is a constant in the true sense
    • sizeof is the compiler's built-in indicator
    • sizeof does not participate in the execution process of the program
    • A typedef is used to rename a type, and a renamed type can be defined after a typedef statement

C-language advanced--enum, sizeof, typedef analysis 11

Related Article

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.