- Preprocessing
- Compile
- Assembly
- Link
There are three situations in Preprocessing:
- File Inclusion (# include)
- Conditional compilation (# if, # ifndef, # endif)
- Macro definition (# define)
Macros are a situation in Preprocessing. In fact, the concept of macros is text replacement.
MACRO:
1. maintainability
2. Readability
Macros have other functions, such as program debugging and tracking, because I have not tried any of them and I will not write them here.
Macro definition:
1. macro definition without Parameters
# Define identifier string
2. macro definition with Parameters
# Define identifier (parameter table) String
Note: The identifier is generally in uppercase,
The macro contains special symbols.
1. Macros without parameters:
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ macro. c
# Include<Stdio. h> # define PI 3.1415 // defines the constant producer ID main () {printf ("PI Constant Value: % f", PI );}
Result:
Cause:
Macros are a situation in pre-compilation (as I mentioned earlier), while pre-compilation mainly replaces macros with text replications.
We can generate pre-compiled files to view:
Run the doscommand:
Open the file macro. I after file pre-Compilation:
The pre-compiled text is very long. Here I mainly show the main function:
Wint_t _ attribute _ (_ cdecl _) _ attribute _ (_ nothrow _) fgetwchar (void ); wint_t _ attribute _ (_ cdecl _) _ attribute _ (_ nothrow _) fputwchar (wint_t ); int _ attribute _ (_ cdecl _) _ attribute _ (_ nothrow _) getw (FILE *); int _ attribute _ (_ cdecl _) _ attribute _ (_ nothrow _) putw (int, FILE *); #2 "macro. c "2 void main () {printf (" PI Constant Value: % f ", 3.1415 );}
We can see that the original "PI" is missing and replaced with 3.1415.
2. macros with parameters:
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ macro2.c
#include <stdio.h>#define AREA(x,y) x*yvoid main(){ int a=6; int b=5; int result; result=AREA(a+b,a-b); printf("result:%d",result);}
Result:
Cause:
Some people may think that the calculation process of "result = AREA (a + B, a-B)" is like this:
Result = (a + B) * (a-B) --> result = (6 + 5) (6-5) --> result = 11
Unfortunately, it is not a function. AREA (x, y) is not a function. It is not a function !!!
As I said at the beginning, macros are replacement and follow the essence. Here they are also replacement.
View Pre-compiled files:
Run the doscommand:
Open the pre-compiled file macro2. I, which mainly displays the main function section:
wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fgetwchar (void); wint_t __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) fputwchar (wint_t); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) getw (FILE*); int __attribute__((__cdecl__)) __attribute__ ((__nothrow__)) putw (int, FILE*);# 2 "macro2.c" 2void main(){ int a=6; int b=5; int result; result=a+b*a-b; printf("result:%d",result);}
We found that the original "AREA (a + B, a-B)" was replaced with "a + B * a-B"
Result = (a + B) * (a-B) --> result = (6 + 5) (6-5) --> result = 11
Result = a + B * a-B-> result = 6 + 5*6-5-> result = 31
Because replacement is not a function, the final result is 31.
Macro functions:
1. maintainability
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ effect. c
# Include <stdio. h> # define PI 3.14 void main () {printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n ", PI); printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n", PI ); printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n ", PI); printf (" console output PI: % f \ n ", PI); printf (" console output PI: % f \ n ", PI); printf ("console output PI: % f \ n", PI );}
For example, we output the PI value for 10 times in the console, PI = 3.14. Later, I found that 3.14 is not the result I want,
I want to modify the original value of 3.14 to 3.1415. In this case, I only need to modify the PI constant. It does not need to be modified 10 times.
# Include <stdio. h> # define PI 3.1415 void main () {printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n ", PI); printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n", PI ); printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n", PI); printf ("console output PI: % f \ n ", PI); printf (" console output PI: % f \ n ", PI); printf (" console output PI: % f \ n ", PI); printf ("console output PI: % f \ n", PI );}
2. Readability
For example, if a constant is 3.1415926 and your code is also very long, it is very troublesome to search for 3.1415926,
# Define PI = 3.1415926. You only need to find the PI.
Struct
In practice, a group of arrays usually have different data types, so struct can be used in this case.
Struct: struct is composed of basic array types, which can contain different types of variables, such as objects in java
Struct defines a type, just as if you have defined an integer (int ).
Struct definition:
Display the declared struct:
Struct name {
Type variable name;
Type variable name;
...
};
Anonymous declared struct:
Struct {
Type variable name;
Type variable name;
...
} Struct variable;
The following describes the struct declaration:
Display the declared struct:
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ struct. c
# Include<Stdio. h> # include <string. h> // import the strcpy () function header file // display the declared struct stu {char name [20]; // Student name int age; // student age }; void main () {// declare the struct variable struct stu student; // struct stu: Here is a type, student is a variable // assign strcpy (student. name, "Zhang 'er"); // student. name = "Zhang 'er", written incorrectly // After the character array is defined, values cannot be assigned directly. However, student can be assigned by strcpy. age = 13; // The console outputs printf ("student. name: % s \ n ", student. name); printf ("student. name: % d \ n ", student. age );}
Compile the file:
The definitions of the two are similar. Variables in the access struct are generally accessed through "struct variables. Variables ".
Anonymous declared struct:
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ struct2.c
# Include<Stdio. h> # include <string. h> // import the strcpy () function header file // anonymously declare the struct {char name [20]; // student name int age; // student age} student; void main () {// assign strcpy (student. name, "James"); student. age = 23; // The console outputs printf ("student. name: % s \ n ", student. name); printf ("student. age: % d \ n ", student. age );}
Compile the file:
The reason is that when the struct is declared anonymously, I declared the student variable. When an anonymous struct is created, the variable can be declared simultaneously because it is anonymous, the anonymous struct can only be used once.
Typedef
Typedef, which is short for typedefine, is a type definition. It can give the type an alias.
We can observe the following through code:
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ typedef. c
# Include<Stdio. h> typedef int Integer; // obtain the alias Integertypedef double Double for the int type; // obtain the alias Doublevoid main () {Integer I = 1 for the double type; // defines the Integer variable int j = 10; // defines the Integer Variable Double mark = 23.3; // defines the Double Precision Floating Point variable printf ("Integer I: % d \ n ", i); printf ("int j: % d \ n", j); printf ("Double mark: % lf \ n", mark );}
Even if an alias is obtained, the original type can still be used.
Compile the file:
Source File: C: \ Users \ Zero \ Desktop \ c \ temp \ typedef. c
Now, the article is here, and it's time to end. Welcome to read my C years.