C language basics (6) (program error macro detection) (enumeration), program enumeration Detection

Source: Internet
Author: User

C language basics (6) (program error macro detection) (enumeration), program enumeration Detection
I. macro check for program errors 1. Macros INCLUDED IN THE SYSTEM

_ FILE _ // The number of lines in the FILE _ LINE _/
2. A custom macro is required.
#define handle_error(msg) (do{perror(msg);   \ exit(EXIT_FAILURE);}while(0)) 

Explanation:

Perror: Check the macro of the system error. Once a system error occurs, an error number (errno) is generated, corresponding to the error string.

EXIT_SUCCESS and EXIT_FAILURE: The C Standard defines two values, which can be used as the exit () parameter to indicate whether the exit is successful.

Exit (): it is passed to the parent process or shell terminal. 3. Two Macros in the Linux kernel: widely used in driver applications

Off_set_of (type, member) calculates the offset of the element in the structure.

Converts 0 to a pointer of the struct type, points to its members, and converts the address to a numeric value to an offset.

#define off_set_of(type, member)    ((long)&((type*)0->member))

The first address of the containe_of (ptr, type, member) struct. You must input the member address of this struct.

Convert 0 to a struct type pointer, point to its members, call the typeof function to obtain the member type, apply for a pointer of this type, and assign a value to the input Member Address str; this address minus the offset is the first address of the struct.

#define container_of(ptr, type, member) \({typeof ((type*)0->member) *mystr = ptr ; \(type *)((long)mystr-off_set_of(type,member));})
Typeof () to obtain the parameter type. The parameter can be a variable name or expression.
Int * p, a; typeof (p) p_a = & a; // variable name parameter typeof (* p_a) B = 23; // expression as a parameter
Ii. Enumeration enumeration: in practice, some variables have only a few possible values. For example, there are only two possible values for a person's gender, and only seven possible values for a week. In C, variables with special values can be defined as enumeration types. The so-called enumeration refers to listing the values of a variable one by one. A variable is only within the range of the enumerated values. Enumeration elements are not variables but constants. Therefore, enumeration elements are also called enumeration constants. Because it is a constant, you cannot assign values to enumeration elements. Enumeration elements act as constants. They have values. during compilation, the C language sets their values to start from scratch and add one by one. Enumeration elements can be assigned values. After n elements are assigned q, the value of n + 1 is q + 1, and so on (the enumerated element values are generally not repeated, but it can be repeated ). An integer cannot be directly assigned to an enumerated variable. It must be forcibly converted to a type before being assigned a value.
For example: A = (enum A) 3 // that is, the enumerated elements of a = S4 are generally uppercase. Definition:
enum A{        S1,        S2,        S3,        S4,};
Implement the password lock function by enumeration and Programming
# Include
  
   
Typedef enum {S1, S2, S3, S4,} S; void main (void) {S a = S1; // declare the format of a variable, assign int num = 0 with an enumerated value; while (1) {scanf ("% d", & num); switch (a) {case S1: if (num = 4) a = S2; else a = S1; break; case S2: if (num = 2) a = S3; else a = S1; break; case S3: if (num = 9) a = S4; else a = S1; break;} if (a = S4) {printf ("\ n" enabled "); return ;}} return ;}
  

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.