In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you the example analysis of constants in C language, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's go to know it!
1. Literal constant
That is, the amount that the literal meaning cannot be changed. For example, if 1 is 1, you can't say that 1 equals 2; for example, there are several fixed blood types (A, B, and O, AB); for example, gender is only divided into male, female, and more esoteric forms.
In C language: 1 hello 3.14. These are called constants.
2. Constant variables of const modification
You can understand the const-decorated constants through a piece of code:
Int num=10; printf ("% d\ n", num); / / num=10num = 5 position printf ("% d\ n", num); / / num=5
Num is a variable in the above code, and by assigning a new value to num, num is constantly changing.
But subtle changes occur when you add const,num to the data type. (you can use it on the compiler yourself.)
Const int num = 10 printf ("% d\ n", num); / / compilation produces an error num = 5 position printf ("% d\ n", num)
When you compile, the result will be an error:
Because at this time, num has become a constant variable with the modification of const, and the variable cannot be modified.
But num cannot be called a constant at this time, it boils down to a variable. For example, it cannot be used when defining an array.
/ / We can see the problem / / int arr [10] = {0} by defining an array;-normal definition array / / int num = 10; num arr [num] = {0};-result in an error report / / const int num = 10 hand arr [num] = {0};-result in an identifier constant defined by error 3 and # define (also known as preprocessing)
This is a method often used in C language to define the size of an array, and you can feel it by yourself.
Use format: # define
# include # define MAX 10int main () {int arr [MAX] = {0}; / / you can change the size of the array printf ("% d", MAX) by changing the size of the MAX; / / MAX=10 return 0;}
Here is a thinking question, you can think about what the result is:
# include # define MAX 5+5int main () {printf ("% d", 3 * MAX); return 0;}
The output at this point is 20, not 30. So understand that # define MAX 5, 5, max is not equal to 10.
Since I can assign an expression to an identifier, can I assign some parameters to this identifier?
You can think about this code:
# include # define Add (define Add b) a+bint main () {int sum = Add (3Magne2); printf ("% d\ n", sum); return 0;}
At this point, sum=a+b occurs first, and then sum=3+2, so the output is 5.
We are only here to explain the most basic constant problems, so we do not derive many macro definitions, and we will focus on the relevant contents of macro definitions later.
4. Enumerate constant
If you have studied structures, the definition of enumerations is similar.
How to use enum:
While defining the enum, declare the variable:
Enum Day {Mon,Tue,Wed,Thus,Fri,Sat,Sun} Workday
Declare the variable after defining the enum:
Enum Day {Mon,Tue,Wed,Thus,Fri,Sat,Sun}; enum Day Workday
Define anonymous enumeration variables: (if the whole program uses only one enumeration, the enum does not need to be followed by an identifier, but you can no longer define the enumeration structure)
Enum {Mon,Tue,Wed,Thus,Fri,Sat,Sun} Workday
Use a piece of code to analyze some of the details of the enumeration structure:
# include enum Day {/ / enum-- enumerated type keywords Day-- enumerated type tags enum Day-- enumerated types Mon=1, Tue, / / {} enumerated values Wed, Thus, Fri, Sat, Sun} Workday; / / Workday-- enumerated variables
Here are some instructions:
If Mon does not assign values, it defaults to 0, followed by increments, such as Tue=1,Wed=2...
If the Mon assignment is 3, then it will increase in turn, such as Tue=4,Wed=5 …
If the assignment is from the middle, such as Thus=7, then the value after Thus increases in turn, and the previous one starts from Mon and starts from 0
Begin to increase
Enumerated values are constants, not variables. You cannot assign a value to it with an assignment statement in the program.
Such as: Tue=7,Sun=Wed. These are all wrong.
You can only assign an enumerated value to an enumerated variable, not the value of that enumerated value to an enumerated variable
For example, Workday=Tue---- is correct
Workday=2---- error
The above is all the contents of the article "sample Analysis of constants in C language". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.