c Language Learning: Enumerations and Structures

Keywords: Javascript ECMAScript

Constant symbolization:


  • Use symbols instead of specific numbers to represent numbers in programs
  • const int a=12;               const fixes a to a value of 12


  • Enumeration is a user-defined data type that is declared with the keyword enum in the following syntax:
  • enum enumeration type name {name 0,....., name n};
  • Enumeration type names are usually not really used, they are braces because they are constant symbols, and their type is fixed to int, with values ranging from 0 to n. For example, the value of name 0 is 0, the value of name 1 is 1, and the value of name n is n.
  • When you need constant values that you can arrange, the meaning of defining an enumeration is to give them names.

  • Enumerators can be used as values
  • Enumeration types can follow enum as types
  • Actually equal to type int, do integer arithmetic, input and output
  • #include<stdio.h>
    enum color{red,yellow,green};
    //Defining a new data type, called color, can be used as an int-like data type 
    void f(enum color c);
    int main(void)
    	enum color t=red;
    //enum clor defines the data type and uses it as an int	
    	return 0;
    void f(enum color c)
  • NumCOLORS as automatic count
  • #include<stdio.h>
    //NumCOLORS as the total number of colors before the count 
    int main(int argc,char const *argv[])
    	int color=-1;
    	char *ColorNames[NumCOLORS]={
    	char *colorName=NULL;
    //Define Pointer	
    	printf("Enter the code for your favorite color:");
    	printf("Your favorite color is%s\n",colorName);


  • You can specify a value when declaring an enumerator
  • So YELLOW is RED+1=2, and 3 and 4 between YELLOW and GREEN are discarded

  • Although enumerated types can be used as types, they are actually not useful
  • If there is a meaningful ranking name, enumeration is more convenient than const int
  • Enumerations are better than macro s because they have an int type

Structure type:

  • Structures: struct name {int member name, int member name};
  • stuct name   Variable;         That is, data with members in the variable
  • Output%i
  • struct date{
    	int month;
    	int day;
    	int year;
    //Each struct date defines a variable with month, day, year 
    struct date today;

The form of the declaration structure:

  • p1 and p2 are point s, with values of x and y inside
    struct point{
    	int x;
    	int y;
    struct point p1,p2;

  • Both p1 and p2 are anonymous structures with x and y inside and are often used temporarily

    	int x;
    	int y;

  • p1 and p2 are point s with values of x and y

    struct point{
    	int x;
    	int y;

How the structure is initialized:

struct date today;

struct date today={07,31,2014};

struct date today={.month=7,.year=2014};
//Since day is not initialized, automatic initialization is 0 

struct date today;
today=(struct date){07,31,2014};

Structure members:

  • Structures and arrays are a bit like
  • Arrays access their members with [] operators and Subscripts
  • a[10]=10;
  • Structures access their members with. operators and names
  • today.day    student.firstName    p1.x     p1.y

Structural operations:

  • To access the entire structure, use the name of the structure variable directly
  • For the entire structure, assignments, addresses, and function parameters can be passed
  • p1=(struct point){5,10};       Equivalent to p1.x=5;p1.y=10;
  • p1=p2;           Equivalent to p1.x=p2.x;     p1.y=p2.y

Structure pointer:

  • Unlike arrays, the name of a structure variable is not the address of the structure variable, and the &operator must be used
  • struct date *pDate=&today;

Structures array:

Arrays are also present in structs, called struct arrays. It is almost identical to the previous numerical array, except it is important to note that each element of the structure array is a variable of the structure type and contains all the member items in the structure.

The method for defining struct arrays is simple, just like defining struct variables, except that they are changed into arrays. Or it's the same definition as the normal array described earlier, such as:

  1. struct STUDENT stu[10];

This defines an array of 10 elements, each of which is a structure variable and contains all the structure members.

A reference to a struct array is in principle the same as a reference to a struct variable. There are only a few structure variables in the structure array, so we only need to use the elements in the structure array one by one using the for loop.

  Functions for structure input variables:

Posted by robviperx on Sat, 06 Nov 2021 19:49:26 -0700