Advanced C language: 11, enum, sizeof, typedef analysis

An enum is a custom type in the C language:

    The enum value is an integer value that can be customized as needed; [The enumeration value cannot be changed]

    The first custom enum value defaults to 0;

    By default, the enum value is based on the previous defined value plus 1;

    Variables of type enum can only take discrete values ​​at the time of definition;

    enum Color
    {
        RED;  
        BLUE=2;
        GREEN
    };
  enum Color c = GREEN;
  printf("%d\n", c);

The value defined in the enum is the [real constant] in the C language !

In projects, enums are mostly used to define integer constants.
enum //unnamed enumeration, used to define constants
{
	ARRAY_SIZE = 10 //Define the size of the array
};

Note the use of unnamed enums in the code above .

Observe the following code to understand the usage of enum:

#include <stdio.h>

enum
{
	ARRAY_SIZE = 10
};

enum color
{
	RED = 0x00FF0000,
	GREEN = 0x0000FF00,
	BLUE = 0x000000FF
	
};

void print_color(enum color c)
{
	switch(c)
	{
		case RED:
		printf("RED = 0x%08x\n", RED);
		break;
		
		case GREEN:
		printf("GREEN = 0x%08x\n", GREEN);
		break;
		
		case BLUE:
		printf("BLUE = 0x%08x\n", BLUE);
		break;
	}
}

void initarray(int array[])
{
	int i=0;
	
	for(i=0; i<ARRAY_SIZE; i++)
	{
		array[i] = i+1;
	}
}

void print_array(int array[])
{
	int i=0;
	
	for(i=0; i<ARRAY_SIZE; i++)
	{
		printf(" array[] = %d ", array[i]);
	}
	
	printf("\n");
}

intmain()
{
	enum color c = GREEN;
	
	print_color(c);

	int array[ARRAY_SIZE] = {0};
	
	initarray (array);
	print_array(array);
	
	return 0;
}

sizeof keyword: [Not a function, but a built-in indicator of the compiler.

sizeof is used to calculate the memory size occupied by a type or variable;

The value of sizeof is already determined by the compiler.

sizeof for types: sizeof(TYPE);

sizefof for variables: sizeof(var); or sizeof var;

int var = 0;
printf("sizeof(int) = %d\n", sizeof(int));
printf("sizeof(var) = %d\n", sizeof(var));
printf("sizeof var = %d\n", sizeof var);
[sizeof is a built-in keyword in C language, not a function]
[In the compilation process, all sizeof will be replaced by specific values;]
 The execution process of the program has nothing to do with sizeof.

Observe the following code and judge the output:

#include <stdio.h>

int f1()
{
    printf("willwilling\n");
    
    return 0;
}

intmain()
{
    int var = 0;

    int size  = sizeof(var++);

    printf("var = %d, size = %d\n", var, size);
    
    int size = sizeof(f1()); //function is not called at all
    
    printf("sizeof(f1()) = %d\n", size);
    
    return 0;
}
The GCC output is as follows:
var = 0, size = 4
sizeof(f1()) = 4
It is worth noting that the function f1() is not printed. That is to say, the value of sizeof has already been determined by the compiler. After the compilation is completed, the value of size has been determined, and the function will no longer be called. sizeof is not a function, but an indicator that tells the compiler to calculate the value of a variable at compile time.


The meaning of typedef:

typedef is used to rename an existing data type [not to create a new type], and essentially cannot create a new type.

Typedef renamed type:

Can be defined after a typedef statement ; cannot be modified by unsigned and signed.

用法: typedef type new_name

Observe the following code to experience the use of typedef:

#include <stdio.h>

typedef int Int32;

struct _tag_point
{
	int x;
	int y;
};

typedef struct _tag_point Point;

typedef struct
{
	int i;
	int array[];
}SoftArray; //Writing of continuous renaming

typedef struct _tag_list_node ListNode; //[Note: Renamed types can be defined after the typedef statement. 
struct _tag_list_node
{
	ListNode* next;
};

intmain()
{
	Int32 i=0;
	
	//unsigned Int32 j = 0;                 error
	
	Point p;
	
	SoftArray* sa = NULL;
	
	ListNode* node = NULL;
	
	return 0;
}

summary:

enum is used to define discrete value types;

The value defined by enum is a constant in the true sense;

sizeof is a built-in indicator of the compiler and does not participate in the execution of the program;

typedef is used to rename a type: the renamed type can be defined after the typedef statement.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325567885&siteId=291194637