C language learning notes -- enumeration & struct, language learning -- Enumeration
Enumeration is a user-defined data type, which uses keywordsEnumStatement in the following syntax format:
Enum Enumeration type name {name 0, name 1,..., name n };
Enumeration type names are not really used. They use the names in braces, because they are constants. Their types are int, and their values are sequentially from 0 to n.
For example, enum color {red, yellow, green };
Three constants are created. The red value is 0, the yellow value is 1, and the green value is 2.
When you need some common values that can be arranged, the enumerated values are defined to give them names.
The enumerated quantity can be used as a value;
Enum can be added as the enumeration type, but it is calculated by an integer and input/output.
# Include <stdio. h> enum color {red, yellow, green, Numcolors}; // here Numcolors is a small routine. The value is exactly the same as the defined enumerated quantity. You can define arrays and traverse arrays. Void f (enum color c); // here, enum must contain int main (void) {enum color t = red; // The enum type can be added as the scanf ("% d", & t); f (t); return 0;} void f (enum color c) {printf ("% d \ n %, c );}
Enumeration quantity
You can specify a value when declaring an enumeration, as shown in figure
Enum COLOR {RED = 1, YELLOW, GREEN = 5}; // RED = 1 is from 1, not from 0 // YELLOW = 2 // GREEN = 5
Enumeration is only an int. Even if a non-existent value is assigned to a variable of the enumeration type, there is no warning or error.
Although the enumeration type can be used as a type, it is rarely used.
If there is a meaningful ranking name, it is easier to use enumeration than to use const int.
Enumeration is better than macro, because enumeration has the int type.
#include<stdio.h>int main(int grc,char *grv[]){ struct date { int month; int day; int year; }; struct date today; today.month = 07; today.day = 31; today.year = 2014; printf("Today's date is %i-%i-%i\n",today.month,today.day,today.year); return 0;}
Like local variables: The structure variables declared inside the function can only be used inside the function; the structure variables declared outside the function can be used in all functions.
Therefore, structure variables are usually declared outside the function so that they can be used in multiple functions.
Initialization of Structure Variables
Struct date day = {2014, 2014}; // first initialization method struct Thismonth = {. month = 07,. year =}; // second initialization method. Not specified as 0
Structure Variable Member
Structure and array are a bit like
The array uses the [] Operator and subscript to access its members.
The structure uses the. Operator and name to access its members.
Today. day
To access the entire structure, use the name of the structure variable.
For the entire structure, you can assign values, get addresses, or pass them to function parameters.
P1 = (struct point) {5, 10}; ----> p1.x = 5; p2.y = 10;
P1 = p2; ----> equivalent to p1.x = p2.x; p1.y = p2.y