1

Summary

is a 1616049c81b878 custom type in the C language; variables of the enum type can only take the discrete value when defined.

can define 1616049c81b896 real constants in C language. Enumerations are commonly used in engineering to define constants (unnamed enumerations).

3) sizeof is the built-in indicator compiler, and the value of sizeof is determined at compile time.

4) sizeof is a built-in keyword of C language and is not a function

  • During the compilation process, all sizeof will be replaced by specific values. The functions or statements in parentheses will not be executed.
  • program has nothing to do with sizeof

5) Typedef is used to existing types . In essence, 1616049c81b985 cannot generate new types. The new type name cannot be modified by unsigned or signed. The can be defined after typedef.

enum, typedef, siezof analysis

1、enum

  • is a 1616049c81b9da custom type in C language
  • The value of enum is an integer value that can be customized according to needs
  • first defined enum value defaults to 0
  • The default enum value is added 1 to the previous defined value of
  • Variables of enum type can only take discrete values at the time of definition

    enum Color
    {
      RED ,
      YELLOW = 2,
      GREEN
    };
    
    int main()
    {
      enum Color cc = GREEN;
    
      printf("cc = %d\n", cc);   // 3
    
      return 0;
    }
  • The value defined in enum is the real constant
  • In engineering, enum is mostly used to define plastic constants

    enum                // 无名枚举
    { 
      ARRAY_SIZE = 5    // 用来定义数组大小,常量
    };
    
    int arr[ARRAY_SIZE] = {0};
    

2、sizeof

  • sizeof is a built-in indicator of the
  • The value of sizeof has been determined at compile time
  • sizeof is used to calculate the memory size occupied by type or variable

    • sizeof is used for type: sizeof(type)
    • sizeof is used for variables: sizeof(var) or sizeof var

      int var = 0;
      printf("%d\n", sizeof(int));
      printf("%d\n", sizeof(var));
      printf("%d\n", sizeof var);
  • sizeof is a built-in keyword of C language and is not a function

    • All sizeof will be replaced by specific values during compilation
    • program has nothing to do with sizeof

      // 下面程序的输出是?
      int var = 0;
      int size = sizeof(var++);
      printf("var = %d, size = %d\n", var, size);        // 0, 4

    The program output var = 0, size = 4, because sizeof has been determined at compile time, , that is, sizeof(var++) is replaced with 4, and var++ will not be executed again.

another example:

int func()
{
    printf("sizeof will be replaced in compile period!!");
    return 0;
}

int main()
{
    int size = sizeof(func());
    printf("size = %d\n", size);   // 4
    return 0;
}

Output: size = 4, but func(), indicating that during compile time, sizeof(func()) is replaced with 4, and func() is not called.

3、typedef

  • typedef is used to existing data type 1616049c81be1b
  • typedef essentially cannot generate new types
  • Types renamed by typedef:

    • Can be defined after the typedef statement, original type can be defined after the typedef statement
    • Cannot be modified by unsigned or signed, cannot be used with unsigned or signed when the new name is used

      typedef int Int32;
      
      typedef struct _tag_point Point;    // _tag_point在后面才定义
                                    // 此处只是告诉编译器,在使用的地方看到Point,知道他就是_tag_point
      struct _tag_point
      {
       int x;
       int y;
      };
      
      typedef struct 
      {
       int len;
       int array[];
      } SoftArray;
      
      
      int main()
      {
       Int32 i = 100;
      //    unsigned Int32 ii = 0;  // error,不能用unsigned修饰
      
       SoftArray* sa = NULL;
       sa = (SoftArray*)malloc(sizeof(SoftArray) + 5*sizeof(int));
       sa->len = 5;
      
       Point pp;
      
       return 0;
      }

This article is summarized from "C Language Advanced Course" by Tang Zuolin from "Ditai Software Academy".
If there are any errors or omissions, please correct me.


bryson
169 声望12 粉丝