C++ Tutorial-Part 6 of the How to C++ Series Column

About the column

This column is a high-quality C++ tutorial column. If you haven’t read the 0th article, click here to go to the 0th article.

This column consistently uses the operating system: macOS Ventura, code editor: CLion, C++ compiler: Clang

Thank you friends who have been with you all the way, thank you for your support ^ _ ^

The feedback from the blogger is very timely. If you encounter problems while reading, you can ask directly in the comment area. The blogger will give you feedback within 24 hours after seeing it.

C++ Tutorial - How to C++ Series Column Part N

Table of contents

About the column

update record

September 17, 2023

Preface

variable

concept

name

integer

concept

integer type

bits and bytes

Next article preview

Conclusion

Quickly turn pages

This article refers to articles and books


update record

September 17, 2023

Publish first article


Preface

What are variables? What to call it? Do variables have types? What does it mean? What are the differences between different types of variables? Bits and bytes?


variable

concept

Earlier we briefly explained some of the characteristics of variables in C++. Today, let’s get to know this friend who will become an extremely familiar friend to you.

#include <iostream>

int main(void)
{
    int hello;
    hello = 5;
    
    return 0;
}

In this code, we declare an integer variable, so what is the variable?

Use the name hello to represent the value of the integer (here 5). In fact, the program will find a memory that can store integers, mark this memory unit as hello, and then copy 5 to this memory unit.

You can then use "hello" in your program to access this memory location. These statements don't tell us where in memory the value will be stored, but the program does record this information. In fact, the memory address of hello can be retrieved using the & operator, which we will introduce later when we introduce pointers, another way to identify data (which we will get to shortly).

It can be simply seen as: in life, we buy a bottle of peanut oil, then we put the oil into an oil barrel, and label the oil barrel as peanut oil. Later, when our family or ourselves want to use it, When we talk about peanut oil, we know what it is and where it is

The label of peanut oil can be compared to variables. Oil is what we store in variables, and the oil barrel is memory.

name

But how to apply this label? In fact, the best way to play is: write whatever it is

Yes, this method is the simplest and easiest to understand. For example, if we want to represent a variable used to store the sum result, then we can name it sum. No one will not understand it or even come to you to ask what it is, but you have to The writing is very long-winded, complicated and messy (I hate this kind of people the most [code hysterics]), and then write an s, who can understand this?

Don't write a1, c1, v1, this kind of thing, the readability is "off the charts"

Writing uncontroversial variable names should be a quality that every programmer should have. Write according to the standards. Of course, some abbreviations can be written. For example, the "database" of the database can be abbreviated to "db", and the added "Addition" can be abbreviated. "Add", this abbreviation is not controversial, unlike sum abbreviated as s or fghjk. If someone quarrels with you about the abbreviation, you can definitely speak louder if you write "Add" than if you write "s", and This situation is indeed the other party's problem.

Please be sure to write the variable names in normal English. If you don’t know how to do it, you can use a translation software to translate it. Not only those with code fetishes can’t stand it, but anyone can’t stand it. Hey,

In short, if you write it in Pinyin or Chinese, it’s your problem. But if you write it in normal English, then it’s the other party’s problem if they come to you.

It is also best for everyone to abide by the following rules:

  • Only alphabetic characters, numbers, and underscores (_) can be used in names. The first character of the name cannot be a number
  • Distinguish between uppercase and lowercase characters
  • C++ keywords cannot be used as names. Names starting with two underscores or an underscore and a capital letter are reserved for implementation (the compiler and the resources it uses). Names starting with an underscore are reserved to implementations for use as global identifiers
  • C++ has no limit on the length of names, all characters in the name are meaningful

The penultimate point is a bit different from the previous ones, in that using a name like _time_stop or _Donut does not lead to a compiler error, but rather to undefined behavior. In other words, there is no telling what the outcome will be. The reason why no compiler error occurs is that such a name is not illegal, but is reserved for implementation/compiler use. The
global name refers to the location where the name is declared, which we will talk about later.

If you are combining two or more words to form a name, it is common practice to separate the words with an underscore character, as in sum _the_inputs, or to capitalize the first letter of each word starting with the second word, as in yourEyes( C programmers tend to use underscores the C way, while Pascal programmers prefer capitalization) Both forms make it easy to distinguish words like carDrip from cardRip or boat_sport from boats_port

Variable naming schemes, like function naming schemes, have many topics to discuss. Indeed, the subject elicits some of the sharpest objections. Again, as with function names, the C++ compiler won't mind as long as the variable names are legal, but consistent and precise personal naming conventions are helpful.

As with function naming, capitalization is also a key issue in variable naming, but many programmers may add other information to the variable name, that is, a prefix that describes the variable type or content. For example, you can name the integer variable "myWeight" "nMyWeight", where the prefix n is used to represent the integer value. The prefix is ​​useful when reading the code or when the variable definition is not very clear. Alternatively, this variable could also be called "intMyWeight", which would be more precise and easier to understand. Other prefixes often used in this way are: "str" ​​or "sz" (for a null-terminated string), b (for a Boolean value), p (for a pointer), and c (for a single character)

As you learn more about C++, you'll find many examples of prefixed naming styles (including the nifty m_lpctstr prefix—a class member value that contains long pointers to constants and null-terminated strings), and There are other, weirder, more counterintuitive styles, and it's entirely up to you whether to use them or not. Of all the subjective styles of C++, consistency and precision are the most important

integer

concept

Integers are numbers without decimal parts, such as 2, 98, -5468 and 0

There are many integers, and if infinite integers are considered to be very large, it is impossible to represent all integers with limited computer memory. Therefore, the language can only represent a subset of all integers. Some languages ​​provide only one integer type (one type meets all requirements), while C++ provides several, so that the most appropriate integer type can be selected based on the specific requirements of the program.

Different C++ integer types use different amounts of memory to store integers. The larger the amount of memory used, the larger the range of integer values ​​that can be represented. In addition, some types (signed types) can represent positive and negative values, while some types (unsigned types) cannot represent negative values. The term width is used to describe the amount of memory used when storing integers. The more memory used, the wider it becomes. The basic integer types of C++ (arranged in order of increasing width) are char, short, int, long and the new long long in C++11. Each type has a signed version and an unsigned version, so there are 10 in total. types to choose from. These integer types are described in more detail below. The char type has some special properties (it is most commonly used to represent characters, not numbers)

Students who learn Python may be wondering, isn't the Boolean type (bool) also a subtype of the integer type? Why doesn't it count? In C++, bool is an independent data type. It can have integer expressions (0 and 1) but it itself has nothing to do with integers or any data type.

integer type

Computer memory is made up of units called bits. C++'s short, int, long, and long long types can represent up to four different integer widths by using different numbers of bits to store values. It would be very convenient if the width of each type was the same in all systems. For example, if short is always 16 bits, int is always 32 bits, etc. But it's not that simple, and no one option can meet all computer design requirements. C++ provides a flexible standard that ensures a minimum length (borrowed from C)

  1. short is at least 16 bits
  2. int is at least as long as short
  3. long is at least 32 bits and at least as long as int
  4. long long is at least 64 bits and at least as long as long

bits and bytes

The basic unit of computer memory is a bit. Think of a bit as a switch that can be on or off. Off represents a value of 0, and on represents a value of 1. The 8-bit memory block can be set to 256 different combinations, because each bit can have two settings, so the total number of combinations of 8 bits is 2×2×2×2 ×2×2×2×2, which is 256. Therefore, an 8-bit unit can represent 0-255 or -128 to 127. Each additional digit doubles the number of combinations. This means that 16-bit units can be set to 65,536 different values, 32-bit units can be set to 4294672296 different values, and 64-bit units can be set to 18446744073709551616 different values. For comparison, unsigned long cannot store the current number of people on the earth or the number of stars in the Milky Way, but long long can.

Byte usually refers to an 8-bit memory unit. In this sense, bytes refer to the unit of measurement that describes the amount of computer memory. 1KB is equal to 1024 bytes, and 1MB is equal to 1024KB. However, C++ defines bytes differently. C++ bytes consist of contiguous bits that can accommodate at least the base character set of the implementation, that is, the number of possible values ​​must equal or exceed the number of characters. In the United States, the basic character sets are usually ASCII and EBCDIC character sets, both of which can be accommodated in 8 bits, so in systems using these two character sets, C++ bytes usually contain 8 bits. However, international programming may require the use of larger character sets such as Unicode, so some implementations may use 16-bit or even 32-bit bytes. Some people use the term octet to mean 8-bit bytes

Many systems currently use the minimum length, that is, 16 bits for short and 32 bits for long. This still leaves multiple options for an int, which can be 16, 24, or 32 bits wide while still being standard-compliant; or even 64 bits, since long and long long are at least 64 bits long. Typically, int is 16 bits wide (same as short) in older IBM PC implementations, and 32 bits (same as long) in Windows, macOS, and many other microcomputer implementations. Some implementations allow you to choose how to handle ints. The width of types varies from implementation to implementation, which can cause problems when moving a C++ program from one environment to another (including using different compilers on the same system). But with a little care, you can minimize this problem

Variables can be declared using these type names just like int:

#include <iostream>

int main(void)
{
    int n_hello;
    hello = 5;

    short s_hello;
    s_hello = 5;

    long l_hello;
    l_hello = 5;

    return 0;
}

 short is the abbreviation of short int, and long is the abbreviation of long int, but almost everyone does not use the longer form.

Next article preview

In the next article, we will talk about the size of variables in each environment in detail (listing tables is too troublesome, why not write a program to see it?)


Conclusion

The author will often help friends who have problems solve their problems. If you have any questions, you can directly point it out in the comment area. The author will reply within 24 hours after seeing it.

If you have any questions about this article, please point it out in the comments. If you like this article, I hope you can like, comment and follow.

If there is anyone around you who has mentioned this field to you, or hopes to make progress with them, share this article with them.

4363 words, 8 parents, 5 children, 1 grandchild


Quickly turn pages

Chapter 0                Chapter 2                 Chapter 4

Part 1                Part 3                 Part 5


This article refers to articles and books

Reference article

https://www.cnblogs.com/chuanzhang053/p/10582861.html

Programming variable naming rules and programming word abbreviation dictionary 

Does bool occupy 1 byte in C language? What to do if it only occupies one position? _Baidu knows 

Wenxinyiyan 

Reference books 

C++ Primer Plus

Guess you like

Origin blog.csdn.net/cat_bayi/article/details/132944632