Dark Horse Programmer Learning notes--------C language variable type, struct, enumeration

Source: Internet
Author: User

The knowledge of this chapter is to define the structure, enumeration and other custom data types, have macro commands, variable types and other small points of knowledge, before beginning to learn the new knowledge of this chapter, we must understand that C language has a rich data type and operators, so the computational power is very powerful, the value used in the calculation is generally stored with variables. Variables are also classified, different types of variables have different storage types, different lifecycles, different scopes, and C also provides keywords to set the properties of variables (such as setting the storage type, life cycle).

First, the variable type

Variables are divided into global variables and local variables.

First explain the characteristics of local variables

1> definition: A variable defined inside a function, called a local variable. Formal parameters also belong to local variables.

2> scope: A local variable is only valid within the function that defines it, that is, the local variable is used only within the function that defines it, and other functions cannot use it.

3> life cycle: Allocates storage space from the row that defines the variable, which is recycled when the code block ends

4> local variable does not have a fixed initial value

Then the characteristics of the global variables

1> definition: A variable defined outside all functions, called a global variable.

2> scope: The scope of a global variable is from the location where the variable is defined to the end of the source program, where the global variable can be shared by other functions after its defined position.

3> life cycle: When a program starts, it allocates storage space and is destroyed when the program exits.

3> the default initial value of the global variable is 0

Second, the structural body

1. Structs are used when the system defines some variables that contain complex variables or contain different types, and the following are some of the definition rules and usage notes used by the struct:

#include <stdio.h>intMainintargcConst Char*argv[]) {    structDate {intYear ; intmonth; intDay ; };//Define structure Body        structDate1 {intYear ; intmonth; intDay ;  }D1; //Define a colleague-defined variable for the struct body        struct {        intYear ; intmonth; intDay ;  }D2; //omitting a type name defines a variable the disadvantage is that it cannot be reused        structDate d={ -,1, -};//assignment The number of data one by one corresponds toD.day= -;//you can use variables. Property Invocation        /*if you want to define a direct write struct Date D2 = {2000,1,20};     struct Date D2; D2 = {2000,1,20}; That's not the right definition.*/    return 0;}

2. There are several points to note about the structure:

1> does not allow recursive definition of struct itself

2> structure body can contain other structures

3> defines a struct type, only illustrates the composition of the type, and does not allocate storage space to it, as if the system does not allocate space for the int type itself. The system allocates storage to this variable only if it is defined as a variable of the struct type

The memory space occupied by the 4> struct variable is the sum of the memory occupied by its members, and the members are arranged in memory in the order in which they are defined

The memory space occupied by the 5> structure is a multiple of the maximum type of memory occupied by the structure, for example

#include <stdio.h>intMainintargcConst Char*argv[]) {    structstudent{intAge//4 bytes        DoubleGao//8 bytes        Char*name;//8 bytes};//Define structure Body    structStudent s1={ A,1.77,"Jack"}; intA=sizeof(S1); printf ("%d", a);//output 24 4+8+8=20 Take the minimum multiples of 8}

3. struct as function parameter

When a struct variable is passed as a function parameter, it actually passes the value of all members, that is, the value one by one of the member in the argument is assigned to the corresponding parameter member. Therefore, changes to the parameters do not affect the arguments.

4. Pointers to struct bodies

struct pointer variable definition form: struct struct name * pointer variable name

With pointers to structs, there are 3 ways to access struct members

1> struct variable name. Member Name

2> (* pointer variable name). Member Name

3> pointer variable name, member name

Third, enumeration

Enumerations are a basic data type in the C language, not a constructed type, which can be used to declare a set of constants. When a variable has several fixed possible values, the variable can be defined as an enumeration type. For example, you can use a variable of an enumeration type to represent the season, because there are only 4 possible values for the season: Spring, summer, autumn, and winter.

Definition rules for enumerations

Enum Enumeration Name {Enumeration element 1, Enumeration element 2,......};

For example:

enum sex{        man,//0       woman,//1       unknown,//2}
Enum Sex S=man;

PS: The value of an enumeration element depends on the order in which the enumeration elements are arranged when defined. By default, the first enumeration element has a value of 0, the second is 1, and sequentially adds 1.

Summary: In this chapter, we learned the knowledge of structs and enumerations, which, although not as complex as other knowledge, are also commonly used data types, so we should be able to master them. The main study of the definition of structure, which defines the structure of variables in three different ways, but also learned the structure of the memory analysis, including the scope of the structure, memory allocation and the amount of storage space, the structure with the previously learned complex data type array, pointers and other closely related to the application, the structure is also focused on the structure and array , a pointer to a struct, and an understanding of the relationship between the struct and the function, when the struct is used as a point of note for the function parameter. We also learned how enumerations are defined in the C language because there is no bool type in C, so we often use enumerations because enumerations are equivalent to defining enumeration constants, so the return value is still of type int data.

Dark Horse Programmer Learning notes--------C language variable type, struct, enum

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.