当前位置:网站首页>Differences between constants and variables (detailed description) (learning note 3 -- variables and constants)

Differences between constants and variables (detailed description) (learning note 3 -- variables and constants)

2022-06-13 01:47:00 It's Beichen bupiacra

Preface :
If you are learning C Language without knowing where to start , You can study with me C Language , During the winter vacation, I will post a blog every day , There are all kinds of C The knowledge of language , If you want to learn , Want to improve , Come and punch in with me every day , I hope we can make progress together .

C Constants and variables in language , First of all, we can clearly see the mutually exclusive relationship between them from the name .“ often ” It means forever , That is to say C A quantity that can remain constant in a language is called a constant , conversely , A variable whose value can change is called a variable .

One 、 Constant

Constants usually appear as values , What we learned before C The basic data types of language , Have constants corresponding to them . in addition , We will also recognize a special string constant .

1. integer constants

C In language , Any integer value is an integer constant , And the integer value can be octal 、 In hexadecimal format . The default integer value is int type , Is the standard integer type .

An integer value 13 All kinds of hexadecimal writing methods

Base number The way of writing explain
octal 015 In numbers 0 As a prefix
Decimal system 13 No prefix is required
Hexadecimal 0xD、0XD In numbers 0 And letters (x or X) As a prefix

2. Real constant

stay C In language , Any valid value with a decimal point is a real constant , Such as “2.0”, It can also be “.2”, The default real constants are double type .

3. character constants

 Enclosing a character in single quotation marks is a character constant ,'a'、'A'、'5'、'\n', These are character constants .
 Pay attention to the following three points when using :
1. Single quotation marks cannot be used in Chinese ,‘a’ It's wrong. , 'a' That's right. .
2. A single quotation mark can contain only one character ,'ab' It's wrong. ,'a'、'\n', That's right. “\n” It's only one character , Play the role of line feed , This is called an escape character .
3. Single quotation marks cannot be empty ,'' It's wrong. ,' ' That's right. , Because there is a space character between the following single quotation marks .

Commonly used escape characters :

Escape character effect
\a Buzzer alarm
\r enter
\000 Use three octal digits to represent characters
\’ Escape single quotation marks to normal characters
\\ Escape backslash to normal character
\n Line break
\tTAB key ( Horizontal TAB )
\x00 Use two hexadecimal digits to represent characters
\’’ Escape double quotation marks to normal characters
\0 Null character ( String end flag )

4. String constant

 stay C In language , There is no such data type as string , But there are string constants .
 The contents enclosed in English double brackets are called string constants or short for string , Such as "abc"、"123"、"".
 The third one is special , There is nothing in the double quotation marks , But it is an empty string .
 There are two points to note when using strings :
1. Double quotation marks must be in English , Chinese double quotation marks are not allowed .
2. Strings cannot be nested , That is, you cannot have another string in one string ,
 If you want to use double quotation marks in a string , The escape character is required “\"”.

Two 、 Variable

Constants usually exist as values , The variable looks like a “ Containers ”. Variables of different types are like variables of different sizes “ Containers ”, Data of different types and sizes can be placed inside . A constant is the value itself , Variables usually have names , We call this the variable name . Users can easily access and operate the data placed in variables through variable names .

1. Definition of variables

C The definition format of variables in language :

 Type specifier   Variable name ;

Points to pay attention to when naming variables :
1. Out of commission C Keywords in the language are used as variable names .
2. Variable names must be alphabetic or underlined “_” start .
3. Variable name cannot be defined repeatedly
4.C Language is case sensitive .

2. Initialization and assignment of variables

“ = ” The assignment operator assigns the value on the right to the variable on the left .

The assignment operation is used to define variables at the same time , It is called variable initialization :

int a = 100;

If you do not assign values when defining variables , It is not the initialization of variables , It is just an ordinary assignment operation :

int a;
a = 100;
原网站

版权声明
本文为[It's Beichen bupiacra]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280549162544.html