C ++ BASICS (1) data precision, forced type conversion, variable naming rules, and pig naming rules

Source: Internet
Author: User

C ++ BASICS (1) data precision, forced type conversion, variable naming rules, and pig naming rules
Pig C ++ BASICS (1) KEYWORDS: data precision, forced type conversion, variable naming rules


1. Most programming languages further supplement their features in two ways: first, give programmers the right to customize data types (classes in C ++ ); second, some useful functions are encapsulated into library functions for programmers to use (functions and standard libraries provided by C ++ ).

2. C ++ is a static data type language. Its type check occurs during compilation, while other languages such as Python check the data type at runtime.


The basics are divided into six parts. Through these six parts, we will understand the basic knowledge of C ++. With these basic syntax knowledge, it is enough to AC a small program on the OJ platform! PS: This article can only serve as a snack after your meal. If you do not have any basic knowledge, please listen to the teacher or read the book first ~


This chapter mainly describes the built-in types of C ++. Through these built-in types, We can initially understand how C ++ supports more complex data types.


I. Basic built-in types

The basic built-in types include the bool, char, short, int, long, float, and double types we are familiar with. What other wide bytes, long integer, and 64-bit long integer. Of course, if you have a little bit of programming language knowledge, you will think it is so easy. What else can you say. There are still many things to be elaborated on:


First, various types of digits.

Maybe you will be familiar with it. char is 8-bit, limit 16-bit, float is 32-bit, etc. But it is worth noting that the int type is floating on different machines, it may be 16 bits or 32 bits.

As for the accuracy of floating point numbers, it is easy to choose float and double. In this case, float has 7 valid digits, and double has 16 valid digits. When you perform a small amount of operations, you can ignore these precision issues. However, when you perform a large amount of data operations, for example, if we do a lot of image pixel computing and there are a lot of iteration operations in algorithms, there will be a big gap in the accumulation of precision differences. And it is different without a compiler. Generally, when you make an algorithm, there are different results from the theory. You can check whether the algorithm is accurate.

In this case, we recommend that you use the double type as much as possible. There are two reasons: First, double is of high precision, and second, double is not necessarily slow.


Second, force type conversion

In many operations, forced type conversion is performed, and the following problems are usually encountered:

1. Integer and floating point conversion:

It is easy to understand how to convert an integer to a floating point. If the integer is followed by a value of 0, for example, 1, it will be 1.0, But if you convert the integer to a floating point, this is not the case. After learning the composition principle, you should know how to store floating point numbers. It is the place where numbers are added with decimal points. As a result, floating point numbers all have a range. When your integer is too large, it will overflow. Many competition questions are so tough. Pay attention to programming.

When a floating point is converted to an integer, do not think it is an approximation. It is actually cut off. We can simply write a short piece of code:

Double a = 1.234;


Int B =;


Cout <"a =" <a <endl;

Cout <"B =" <B <endl;


A = 1.789;

B =;


Cout <"a =" <a <endl;

Cout <"B =" <B <endl;

The result is as follows:


2. Conversion of signed and unsigned types.

From the perspective of Digital Storage in principle of composition, there are two different storage methods for the symbolic type and the unsigned type. When there is a signed storage, there is a special symbol bit, no longer. Therefore, if char-3 is not converted to unsigned char, it will become 3. The actual result should be the remainder of the modulo value of 256. Therefore, when writing a program, symbols are signed. If there is no symbol, do not mix them. In addition, when unsigned numbers are subtracted, you must also note that you must not cut them into negative numbers. In this case, the value is wrong. Many programming problems may be caused by this. Remember, remember!


In short, selecting a data type is a very important thing. We usually need to consider the accuracy, storage size, and symbol issues. Especially when you have strict control over the memory.


Ii. Variable declaration and naming rules

In general, the instructor will tell you the global variables and the scope of the local variables. In C ++, a variable can be defined only once, but there is a difference in scope. Simply put, let's look at a simple program.




Basically, this is the case. The most effective scope is only used in it. After the brackets are complete, restore the original ones. We can see that the 15th rows define a for statement of I =, And the for statement of 18 rows should be 9 after running, but the parentheses are over, and the I equal to 9 is useless, so it became 100 again.


Iii. pointer and reference

When I was learning the C language, I usually couldn't understand the pointer I just learned, because it was too virtual. First, when someone asks me what a pointer is, I can only say it once according to the one in the book. If you still don't understand it, I will drop you again. Experience makes it easy to understand.

After a book about computer storage principles, such as composition principles or operating systems, we can probably find that pointers can also be seen as variables, similar to int. However, int is used for data and pointer is used for address. When the computer is doing a pointer, it first reads the data in the pointer as the address, and then finds the data in the address based on the address. So the common usage is to declare the pointer int * p, * q, with an asterisk in front. When we do p = q, in fact, q points to the address p. When we do * p = * q, we give the data value that q points to the data value that p points. In a simple sentence, a pointer is an address, not a value.

In addition, we recommend that you initialize the pointer when declaring the pointer, for example, int * p = 0. There won't be many bad results. For details, refer to the ones that the instructor boasted for you.

Reference refers to the concept mentioned in C ++. Simply put, it refers to taking a small name. Michael is you, and John is you. So int fun (& a); will modify the value of the parameter passed in the function, int fun (a) won't. You can try it several times.

Finally, the & Symbol also indicates the address. int a = 0; & a indicates the location of the address where a is located. It can be understood by referring to the pointer concept, & in fact, it is a type of * reverse operation. Int * p; int a = 0; p = & a; then we can find that * p is the value of.




To sum up, this chapter focuses on three issues: first, accuracy between variable types is not required; second, scope of declared variables (which is a little different from C); and third, pointer and reference meaning. The above is my personal understanding. please correct me if it is not correct.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.