C Language Learning notes--enum and sizeof keywords

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.