<> If you know the true face of Lushan Mountain
C There are several kinds of constants in language :
*
Literal constant
*
const Modified constant variable
*
#define Defined identifier constant
*
enumeration constant
<> Literal constant
That is, the literal meaning can not be changed . as 1 namely 1, You can't say let me 1 be equal to 2; For example, there are several fixed blood groups (A,B,O,AB); For example, the gender of people is only divided into men , female sex , And a deeper form .
stay C In language :1,3.14,‘a’,“hello”… These are called constants .
<>const Modified constant variable
It can be understood by a piece of code const Modified constant variable :
int num = 10; printf("%d\n", num); //num=10 num = 5; printf("%d\n", num);
//num=5
In the code above num It's a variable , Through you num Assign a new value ,num It's constantly changing .
But when you add const,num There's a subtle change .( I can use it on the compiler )
const int num = 10; printf("%d\n", num); // Compile error num = 5; printf("%d\n", num);
When you compile , As a result, an error will be reported :
Because at this time num stay const It has become a constant variable , Variables cannot be modified .
however num It can't be called a constant , It's a variable after all . For example, it cannot be used when defining arrays .
// See the problem by defining an array //int arr[10] = { 0 };------ Normal definition array //int num = 10; //int
arr[num] = { 0 };----- The result is an error //const int num = 10; //int arr[num] = { 0
};---- The result is an error
<>#define Defined identifier constant ( Also called pretreatment )
This is C Language definition of array size is often used , You can use it by yourself and feel it .
Use format :#define < identifier > < constant value / expression >
#include <stdio.h> #define MAX 10 int main() { int arr[MAX] = { 0 };
// By changing MAX Can change the size of the array printf("%d",MAX); //MAX=10 return 0; }
Here's a thought , You can think about the result :
#include <stdio.h> #define MAX 5+5 int main() { printf("%d", 3 * MAX); return 0
; }
The output is 20, It's not 30. So understand #define MAX 5+5,MAX It's not the same 10.
Now that you can assign an expression to an identifier , Can I assign some parameters to this identifier ?
You can think about this code :
#include <stdio.h> #define Add(a,b) a+b int main() { int sum = Add(3,2); printf
("%d\n",sum); return 0; }
here , Come first sum=a+b, Recurrence sum=3+2, So output 5
We are just going to talk about the most basic constant problem , Therefore, there are not many derived macro definitions , Later, we will focus on macro definition .
<> enumeration constant
If you've learned about structure , The definition of enumeration is similar to that of enumeration .
enum How to use it :
1. In the definition enum At the same time , Declare variables :
enum Day { Mon,Tue,Wed,Thus,Fri,Sat,Sun }Workday;
2. End of definition enum Then declare the variable :
enum Day { Mon,Tue,Wed,Thus,Fri,Sat,Sun }; enum Day Workday;
3. Define anonymous enumeration variables :( If the whole program uses only one enumeration , be enum There is no need to add an identifier after it , However, the enumeration structure can no longer be defined )
enum { Mon,Tue,Wed,Thus,Fri,Sat,Sun }Workday;
Through a section of code to analyze some details of enumeration structure :
#include <stdio.h> enum Day { //enum-- Enumeration type keyword Day-- Enumeration type label enum Day-- The type of the enumeration Mon=1
, Tue, //{ } Inside are enumeration values Wed, Thus, Fri, Sat, Sun }Workday; //Workday-- Enumerating variables
Here are some explanations :
1. If Mon No assignment , The default value is 0, After that, increase in turn , as Tue=1,Wed=2…
2. If Mon Assign to 3, Then it will increase in turn , as Tue=4,Wed=5…
3. If it is assigned from the middle , as Thus=7, be Thus The following ones increase in turn , Previous from Mon Start with 0
Start to increase
4. Enumeration values are constants , It's not a variable . You cannot assign a value to it again in a program with an assignment statement .
as :Tue=7,Sun=Wed. These are all wrong .
5. You can only assign an enumeration value to an enumeration variable , The value of the enumeration value cannot be assigned to an enumeration variable
as :Workday=Tue---- correct
Workday=2---- error
More about enumeration , Share later . I hope you like it .
Technology