1.enum keywords
The enum keyword in the C language is used to define enum types
(1)enum is a custom type in C language
(2) anenum value is an integer value that can be customized as needed
(3) The first defined enum value defaults to 0
(4) by default, the enum value is based on the previous defined value plus 1
(5)a variable of enum type can only be used when defining discrete values
color{GREEN, // default green==0; red= 2 , // custom red==2; BLUE // };enumcolor c =green; // defines the C variable and initializes it to GREEN printf ( " %d\n , c);
the values defined in the enum are the True constants in the C language. enum is used in engineering to define integer constants
#include <stdio.h>enum //Nameless enumeration, used to define constants{array_size=Ten //defines the size of the array, Array_size is constant, run-time withoutlegal changes};enumcolor{RED=0x00ff0000, GREEN=0x0000FF00, BLUE=0x000000ff //Note that there is no semicolon behind};//Print, parameter is enum typevoidPrintcolor (enumColor c) { Switch(c) { Casered:printf ("color:red (0x%08X) \ n", c); Break; Casegreen:printf ("color:green (0x%08X) \ n", c); Break; Caseblue:printf ("color:blue (0x%08X) \ n", c); Break; }}//Initializing DatavoidInitarray (intarray[]) { inti =0; for(i=0; i<array_size; i++) {Array[i]= i +1; }}voidPrintArray (intarray[]) { inti =0; for(i=0; i<array_size; i++) {printf ("%d\n", Array[i]); }}intMain () {enumColor C = GREEN;//define the variable C and initialize it to GREEN intArray[array_size] = {0}; Printcolor (c); Initarray (array); PrintArray (array); return 0;}
sizeof keywords in 2.C language
(1 sizeof is the compiler's built-in indicator
( Span class= "Fontstyle1" >2) sizeof for calculation
(3) Thevalue of sizeof is determined at compile time , all sizeof will be replaced by specific values during the compilation process, and the program execution process sizeof has no relationship.
#include <stdio.h>intf () {printf ("I like programming!\n"); return 0;}intMain () {int var=0; intSize =sizeof(var++);//the line is replaced with an int size = 4 at compile time; //so at runtime, there's no code like var++.printf"var =%d, size =%d\n",var, size); Size=sizeof(f ());//the line is replaced with size = sizeof (the function's return value type) at compile time =4;//that is, the final code does not have a call to the F () function, nor does it output the printf inside F ()printf"size =%d\n", size); return 0;}
C Language Learning notes--enum and sizeof keywords