In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "how to define constant types in C language". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to define constant types in C language" can help you solve the problem.
The definition of constant
A constant is a constant (such as pi, gender, ID number).
There are differences in the forms of definitions of constants and variables in C language.
Classification of constants
The constants in C language are divided into the following categories:
1. Literal constant
Constant variables modified by 2.const
Identifier constant defined by 3.#define
4. Enumeration constant
Let's demonstrate.
Literal constant
A literal constant is a constant that can be seen literally
The int main () {/ / literal constant demonstrates 3.14 define / decimal 1000 / integer'w'/ character "abc"; string 0;} constant define _ CRT_SECURE_NO_WARNINGS#include// uses printf to remember to add this code int main () {/ / const to demonstrate the int axiom 100 hand / we will change it in a moment Printf ("d", a); return 0;}
Running result:
ten
When we add a const to int astat100 to modify it.
# define _ CRT_SECURE_NO_WARNINGS#includeint main () {/ / const-modified constants demonstrate const int axiom 100 / here axiom 10; printf ("% d", a); return 0;}
The result of the operation:
Run error, no actual output
We can know that if const modifies a variable, it is not allowed to change another value to the variable, which is equivalent to a constant, but it also has the properties of the variable, so we call it a constant modified by const.
Why does it have variable properties?
Let's prove it:
To learn a new thing, array, when we write code, sometimes we need to write a lot of variables, which is very troublesome. At this time, we use the array arr [], (a [], hin [] all casually, name yourself). You can only put constants in the array [], such as arr [5], which is equivalent to defining five variables directly, but you cannot put variables in it. According to this principle, let's prove whether the constant modified by const has variable properties.
# define _ CRT_SECURE_NO_WARNINGS#includeint main () {const int a = 100; int arr [10] = {0}; return 0;}
Can run normally
# define _ CRT_SECURE_NO_WARNINGS#includeint main () {const int a = 100; int arr [a] = {0}; return 0;}
Running result:
An error has occurred, indicating that the constant modified by const has a variable attribute
I have a boss (I think the boss, ) understands it like this: suppose the variable is a space, the constant is an actual value, the variable is used to hold the constant, and const just locks the value, but it doesn't turn the space into a numerical value.
# identifier constant defined by define # define _ CRT_SECURE_NO_WARNINGS#include#define MAX 100//hereint main () {printf ("% d\ n", MAX); int axiaMaxing printf ("% d\ n", a); return 0;}
The result of the operation:
one hundred
one hundred
As you can see, MAX is defined as 100. you can print directly or give a value to a variable. You can use it when you want to use it. This MAX is an identifier constant, that is, an identifier constant defined by # define. (# define can also define a string, not limited to numbers)
Enumeration constant
Some values in my life can be listed one by one, such as gender, let's take a preliminary look at it, after all, it is only the initial c language.
# include enum Sex {MALE,// male FEMALE,// female SECRET// uncertain}; / / the possible future value of enum Sex is MALE,FEMALE,SECRET. They are enumerated constants int main () {/ / enumerated constant demo printf ("% d\ n", MALE); printf ("% d\ n", FEMALE); printf ("% d\ n", SECRET). / Note: the default enumeration constant starts at 0 and increases by 1 in turn
Note that the three enumeration constants in enum Sex do not open up space and cannot be changed. If you change it, you will report an error.
This is the end of the introduction to "how to define constant types in C language". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.