c Language Deep Analysis (11) - enum, sizeof, typedef analysis

1. enum enumeration type

1.1 Usage

  • enum is a custom type in C
  • enum values are integer values that can be customized as needed
  • The first defined enum value defaults to 0
  • By default, the enum value is added to the previous defined value by 1
  • Variables of enum type can only take discrete values when they are defined
enum Color
{
    GREEN,      // GREEN==0;
    RED= 2,     // Custom RED==2;
    BLUE         // By default, 1 is added to the previous one, which is BLUE==3
};

enum Color c =GREEN;//Define c variable and initialize to GREEN

printf("%d\n",c);

1.2 Special significance of enumeration types

  • Values defined in enum are real constants in C
  • enum is often used in engineering to define integer constants

1.3 Programming Experiments

  • Use of enum
#include <stdio.h>
#include <stdlib.h>

enum                   // Anonymous enumeration, used to define constants
{
	ARRAY_SIZE = 10    // Defines the size of the array, ARRAY_SIZE is a constant and cannot be changed at runtime
};

enum Color
{
	RED = 0x00FF0000,
	GREEN = 0x0000FF00,
	BLUE = 0x000000FF  //Notice that there is no semicolon behind
};

// Print, parameter is enumeration type
void PrintColor(enum Color c)
{
	switch (c)
	{
		case RED:
			printf("Color: RED (0x%08X)\n", c);
			break;
		case GREEN:
			printf("Color: GREEN (0x%08X)\n", c);
			break;
		case BLUE:
			printf("Color: BLUE(0x%08X)\n", c);
			break;
	}
}

// Initialize data
void InitArray(int array[])
{
	int i = 0;

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

void PrintArray(int array[])
{
	int i = 0;

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

int main()
{
	enum Color c = GREEN; // Define variable c and initialize to GREEN

	int array[ARRAY_SIZE] = { 0 };

	PrintColor(c);

	InitArray(array);

	PrintArray(array);

	system("pause");
	
	return 0;
}
  • Run result:

2. Use of the sizeof keyword

Introduction to 2.1 sizeof

  • sizeof is a built-in indicator for the compiler
  • sizeof is used to calculate the memory size of a type or variable
  • The value of sizeof has been determined at compile time
    • Sizeof is used for types: sizeof (type)
    • Sizeof is used for variables: sizeof (var) or sizeof var

2.2 Correct the name of the sizeof Keyword - sizeof is a built-in keyword in C rather than a function

  • All sizeof s will be replaced by specific values during compilation
  • The execution of the program has nothing to do with sizeof
int var =0;

int size = sizeof(var++); // At compile time, the size of the line is calculated and
                          // Was replaced with a statement like int size=4; such a statement.Already existing
                          // The final code has no var++ statements.(Note,
                          // Var++ can be thought of as var = var+1; so sizeof(var++)
                          // Equivalent to calculating the size of the final variable var).
printf("var = %d, size = %d\n",var,size); //Output 0,4.Instead of 1,4;

2.3 Programming Experiments

  • The nature of sizeof
#include <stdio.h>
#include <stdlib.h>

int f()
{
	printf("I like programming!\n");

	return 0;
}

int main()
{
	int var = 0;

	int size = sizeof(var++); // The line was replaced at compile time with int size =4;
							  // So at runtime, no code like var++ exists

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

	size = sizeof(f()); // The line is replaced at compile time with size = sizeof (return value type of function) =4;
						// That is, the final code will not output the printf inside f() without a call to the f() function

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

	system("pause");

	return 0;
}

3. Significance of typedef

Examiner: Can you tell us what typedef means?

Candidate: typedef is used to define a new type.(X)

  • typedef is used to alias an existing data type
  • typedef does not naturally produce new types
  • Type renamed by typedef:
    • You can define it after the typedef statement
    • Cannot be modified by unsigned or signed (i.e. can no longer be defined as unsigned or signed)
    • Usage: typedef type new_name;

3.1 Programming Experiments

  • Example use of typedef
#include <stdio.h>

typedef int Int32;

// Define the type before renaming it
struct _tag_point
{
	int x;
	int y;
};
typedef struct _tag_point Point;

// Define an unnamed type and rename it with typedef
typedef struct
{
	int length;
	int array[];
} SoftArray;


// Rename first, then define the type - it seems illegal, awkward, but it's actually correct
// It can be understood that typedef does not check a type but simply gives an alias to a type.
// For example, in this example, struct _tag_list_node is renamed ListNode, which is encountered during later compilation
// ListNode is replaced with its actual type struct _tag_List_Node.
typedef struct _tag_list_node ListNode; // Rename first
struct _tag_list_node                   // Redefine type
{
	ListNode *next;
};

int main()
{
	Int32 i = -100;        // int 
	unsigned Int32 ii = 0; //error, cannot specify signed or unsigned for renamed type
	Point p;               // struct _tag_point
	SoftArray *sa = NULL;
	ListNode *node = NULL; // struct _tag_list_node*

	return 0;
}

4. Summary

  • enum is used to define discrete value types
  • The value defined by enum is a real constant
  • sizeof is a built-in indicator for the compiler
  • sizeof does not participate in program execution
  • Typedef is used to rename types: renamed types can be defined after a typedef statement

Keywords: Programming

Added by SystemOverload on Thu, 08 Aug 2019 05:07:55 +0300