C language keyword (custom type)

preface

Today, I'm mainly talking about the knowledge of structures, enumerations and federations. It may be boring, but it's very important. I'll try to be more detailed

Structure keyword struct

struct is the keyword for declaring the structure type. We often use it in learning linked list, so I divide it into two levels

primary

Here we need to know how to define the structure and how to format the input and output of the structure

Define a structure

struct Peo
{
	char name[20];
	int age;
};

int main()
{
	struct Peo peo = { "zhansan",18 };
	return 0;
}

PEO is a local variable and its type is struct Peo. This struct Peo type is like our previous int and char.
{"Zhan", 18} is the initialization of peo,

When you let me, we can write the same

struct Peo
{
	char name[20];
	int age;
} s;     //s is a weight variable

Format output structure

#include<stdio.h>
struct Peo
{
	char name[20];
	int age;
} s;

int main()
{
	struct Peo peo = { "zhansan",18 };
	printf("%s %d\n", peo.name, peo.age);
	return 0;
}

Format input structure

#include<stdio.h>
struct Peo
{
	char name[20];
	int age;
} s;

int main()
{
	struct Peo peo = { "zhansan",18 };
	printf("%s %d\n", peo.name, peo.age);
	scanf("%s %d", peo.name, &peo.age);
	printf("%s %d\n", peo.name, peo.age);
	return 0;
}

Advanced

Structural transmission parameters

#include<stdio.h>
struct Peo
{
	char name[20];
	int age;
} s;

void test(struct Peo* ps)
{
	printf("%s %d\n", ps->name, ps->age);
	scanf("%s %d", ps->name, &ps->age);
	printf("%s %d\n", ps->name, ps->age);
}

int main()
{
	struct Peo peo = { "zhansan",18 };
	test(&peo);
	return 0;
}

For structure pointers, we use '- >' instead of '- >' to access the contents of the structure

Size of structure

As we all know, int is the size of 4 bytes. What about the structure? Is it the sum of the size of bytes inside?, Here, I'll briefly say it first. You can have a look at it in detail

Default alignment number

Let's mention here that it is a numerical value. When used later, the default alignment number of VS2019 is 8

Calculation rules

I'll talk about it first and explain it one by one later

  1. The first member is at an address offset 0 from the structure variable.
  2. Other member variables should be aligned to the address of an integer multiple of a number (alignment number).
    Alignment number = the smaller value of the compiler's default alignment number and the size of the member.
  3. The total size of the structure is an integer multiple of the maximum number of alignments (one for each member variable).
  4. If a structure is nested, the nested structure is aligned to an integer multiple of its maximum alignment number, and the overall size of the structure is an integer multiple of all the maximum alignment numbers (including the alignment number of nested structures).

Example 1

Calculate the size of the following structure

struct S1
{
	double d;
	char c;
	int i;
};




Verify it

Example 2

Another structure is nested inside the structure

struct S1
{
	double d;
	char c;
	int i;
};
struct S2
{
	int i;
	struct S1 s;
};




Verify it

Enumeration keyword eunm

Enumeration means to enumerate one by one

enum Color//colour
{
	RED,        //0
	GREEN,      //1
	BLUE        //2
};

enum Color is a data type, and the compiler will automatically add + 1 from top to bottom

enum Color//colour
{
	RED,        //0
	GREEN,      //1
	BLUE        //2
};

int main()
{
	enum Color red = RED;
	enum Color green = GREEN;
	enum Color blue = BLUE;
	printf("%d\n", red);
	printf("%d\n", green);
	printf("%d\n", blue);
	return 0;
}

Size of enumeration

The size is 4 bytes

enum Color//colour
{
	RED = 5,        
	GREEN = 9,      
	BLUE        
};

int main()
{
	printf("%d", sizeof(enum Color));
	return 0;
}

The role of enumeration

It's hard to say. It can be put in a switch statement

enum Calc
{
	EXIT,
	ADD,
	DEL
};

void menu()
{
	printf("**************************\n");
	printf("****  1.ADD   2 DEL   ****\n");
	printf("****     0.EXIT       ****\n");
	printf("**************************\n");
}

int main()
{
	int input = 0;
	do
	{
		menu();
		printf("Please select:>");
		scanf("%d", &input);
		switch (input)
		{
		case ADD:
			printf("Additive function\n");
			break;
		case DEL:
			printf("Subtraction function\n");
			break;
		case EXIT:
			printf("Exited\n");
			break;
		}
	} while (input);
	return 0;
}

Question one

What will the following code print out

enum Color//colour
{
	RED = 5,        
	GREEN = 9,      
	BLUE        
};

int main()
{
	enum Color red = RED;
	enum Color green = GREEN;
	enum Color blue = BLUE;
	printf("%d\n", red);
	printf("%d\n", green);
	printf("%d\n", blue);
	return 0;
}

union (symbiosis) keyword union

A consortium is the space of the largest member shared by several members, and the size is naturally the size of the largest member

union U
{
	int i;
	char ch;
} u;

int main()
{
	printf("%p\n", &u);
	printf("%p\n", &u.ch);
	printf("%p\n", &u.i);
	return 0;
}


features

  • Members of the consortium can only use one at a time
  • Changing the value of one member will change the other

Example 3

union U
{
	int i;
	char ch;
} u;

int main()
{
	u.i = 0x11223344;
	printf("%#x", u.ch);
	return 0;
}

Keywords: C

Added by hongco on Mon, 24 Jan 2022 18:59:59 +0200