C's enum, sizeof, typedef (11)

Source: Internet
Author: User

In the C language, you will often seeenum, sizeof, typedef, let's explain three of them today.

First, the enum is a custom type in the C language, which is an enumeration type. The enum value is an integer value that can be customized as needed, and the first defined enum value defaults to 0. By default, the enum value is 1 on the basis of the previous defined value, and theenum -type variable can only take the discrete value when it is defined. The values defined in the enum are the true constants in the C language, and in general engineering the enum is used to define the XXX constants.

Let's just write a sample code analysis with the following code:

#include  <stdio.h>enum{    array_size = 5};void initarray (int  array[]) {    int i = 0;         for (i=0; i<array_size; i++)     {         array[i] = i + 1;    }}void printarray (Int array []) {    int i = 0;        for (i=0;  i<array_size; i++)     {         printf ("%d\n",  array[i]);     }}int main () {         int array[ARRAY_SIZE] = {0};         Initarray (array);         printarray (array);         return 0;} 

Let's analyze This code, which uses an enum to define a constant array_size, and then create an array and print it. Shall we look at the results of the compilation as we wish? Print as follows:

Then we see that the print is exactly as we thought.

Next we talk about the sizeof keyword, which is the compiler's built-in indicator for calculating types or variables that make up a small memory footprint. Then the value it calculates is determined at compile time, which means it is useless at runtime. sizeof at type:sizeof(type), when used for variable:sizeof(VAR) or sizeof var. sizeof is the C language's built-in keyword instead of a function, and all sizeof will be replaced by specific values during the compilation process, and the execution of the program does not have any relationship with sizeof . Let's take a look at what the following example code will output.

#include <stdio.h>int f () {printf ("Hello world\n"); return 0;}        int main () {int var = 0;        int size = sizeof (var++);        printf ("var =%d, size =%d\n", var, size);        size = sizeof (f ());        printf ("size =%d\n", size); return 0;}

Let us first analyze this code, in the 14th line of the program we do the var++, theoretically the 16th line will be printed out of the results are 1 and 4. The 18th line of the program seems to call the F () function, then according to our analysis, will be printed in line 20th 4, but in front of hard print out Hello world this sentence. Let's see if it's as we've analyzed it.

So what's going on here? Unlike our analysis, VAR is not equal to 1 and does not print out Hello world. Let's consider the above-mentioned sizeof knowledge, it is in the program compile time valid, that is, after the program is running and does not go to execute var++, so var is still equal to 0. sizeof is a keyword rather than a function, so the 18th line of the program is not to call the F () function, and naturally it will not execute the printed statement in the function. Because the return value of the function is of type int, the printed value is 4.

Finally, we'll talk about the typedef keyword. We usually think of a typedef as defining a new type, not actually. A typedef is used to rename an already existing data type, which in essence cannot produce a new type. Note: The named type can be followed by a TypeDef statement, but cannot be modified by unsigned and signed .

Let's take the example code to analyze the code below:

 #include  <stdio.h>typedef int int32;struct _tag _point{    int x;    int y;}; typedef struct _tag_point point;typedef struct{    int length;     int array[];}  SoftArray; typedef struct _tag_list_node ListNode;struct _tag_list_node{     ListNode* next;}; Int main () {    int32 i = -100;     unsigned  int32 ii = 0;    point p;    softarray*  sa = NULL;       ListNode* node = NULL;         return 0;} 

Let's analyze This code, which is clearly defined in line 19th, which is legal in the C language. The 28th line modifies the renamed type with unsigned, which is obviously wrong and therefore will error in 28 rows. Line 29th is equivalent to struct _tag_point p; line 31st is equivalent to struct _tag_list_node* node; we'll look at the compilation results.

Then after we comment out the 28th line, the compilation is passed. So today we explain the enum, sizeof, typedef , summarized as follows: 1, enum is used to define a discrete value type, and it defines the value is a true constant; 2, sizeof is the compiler's built-in indicator, does not participate in the program's execution process ; 3. typedef is used to rename the type, and the renamed type can be defined after the TypeDef statement. We will continue our study of C language in the following.


Welcome Everybody to study C language together, can add me qq:243343083.

C's enum, sizeof, typedef (11)

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.