C + + Primer learning notes and thinking _1----variables and basic types

Source: Internet
Author: User

C + + Primer learning notes and thinking _1----variables and basic types


What does a type do?
Types tell us what the data represents and what actions can be performed on the data.

One: Basic built-in type

The C + + standard specifies the minimum storage space for each arithmetic type, but it does not prevent the compiler from using larger storage space. for int, almost all compilers use more storage space than required.

There are two types of char and wchar_t, where wchar_t is used to extend character sets, such as kanji and Japanese, and some characters in these character sets cannot be represented by a single char.

C + + Primer 32nd page

Unlike other types, char has three different types: normal char, unsigned char, signed char. Although there are three different types of char, there are only two ways to represent it. You can use unsigned char or signed char to represent a char type. Which type of char is used to identify the method is determined by the compiler.

The understanding of this passage: why there are three different types, and why there are two ways of expression?

The original is this: in the program directly write an int, the default is signed int, but write a char in the program, there is no default, specifically signed char or unsigned char, depending on the compiler settings.

For VC, x86 on the Gcc,char is processed into signed char. If you want to test your compiler, you can use this code

Char a =-1;

printf ("a=%d", a);

If the result is-1, the current compiler processes the char into signed char; If the result is 255, the current compiler processes the char into unsigned char.

The assignment problem of integral type

What is the result when we try to assign a value beyond its value range to an object of the specified type?

(1) for the unsigned type, the compiler will model it and get the result. An attempt to store 336 in a 8-bit unsigned char is actually assigned a value of 80, because 80 is the value of 336 to 256 modulo. In addition, negative numbers always exceed the range of unsigned, and it is perfectly legal to assign negative numbers to unsigned in C + +. If you assign 1 to a 8-bit unsigned char, the result is 255, because 255 is the value after modulo 1 for 256.

(2) When assigning values that exceed the range of values to the signed type, the compiler determines the actual assigned value. In practice, many compilers handle signed types in a manner similar to the unsigned type. That is, the value is evaluated when the value is given the number of values for that type. However, we cannot guarantee that the compiler will handle the signed type this way.

Two: Literal constants

One way to use an escaped character:

<span style= "color: #330033;" ><span style= "Font-family:simsun;" > #include <iostream>using namespace std;int main () {//output Welcome to the ' ACM ' World, you must use the escape character cout<< " Welcome to the '  acm\ World ' <<endl;return 0; </span></span>
string Literals:

<span style= "color: #330033;" ><span style= "Font-family:simsun;" > #include <iostream>using namespace std;int main () {cout<< "a multi-line"//correct, type consistent       "string literal"  <<endl;    cout<< "multi-line" L "literal" <<endl; Error, string literal and wide string literal type different    cout<< "a mul-line using   a Namespace" <<endl;//multi-faceted string use \, thereafter no comment and no space}< /span></span>

Three: Variables

1. Keyword cannot be a variable name

2. Generally lowercase letters and to help with memory

3. The identifiers of multiple words are underlined between: Student_loan

The initialization and assignment of C + + is two different operations, which may be of no difference for built-in types, but the difference between class types is obvious. Initialization refers to creating a variable and assigning it an initial value, while the assignment is to erase the object's current value and replace it with a new value.

1) Variable initialization rules

1. Whether a variable of a built-in type is initialized depends on where the variable is defined: variables defined in the function body are initialized to 0, and built-in type variables defined inside the function are not automatically initialized.

"However, uninitialized variables actually have a value"

Recommendation: Objects of each built-in type are initialized.

2. Classes control the initialization of class objects by defining one or more constructors. If a class has a default constructor, the default constructor is called regardless of where the variable is defined.

2) Declaration and definition

C + + uses the extern keyword declaration to indicate to the program the type and name of the variable. Because extern is a declaration, no storage space is allocated.

A variable can be declared multiple times, but can only be defined once

If the declaration has an initialization, then it can be treated as a definition, even if there is a declaration tag extern, such as:

<span style= "color: #330033;" ><span style= "Font-family:simsun;" >extern int i;//only declares that int i is not defined; Define the variable extern double PI = 3.14;  An extern declaration can contain an initializer only if it is outside the function, but some compilers cannot compile. </span></span>
3) Scope of the name

C + + scopes can be nested, curly brackets are bounded

<span style= "color: #330033;" ><span style= "Font-family:simsun;" >//It is always bad to define a local variable with the same name as the global variable that the function might use, it is best to use a different name for the local variable!  #include <iostream>   #include <string>   using namespace std; string s1 = "Hello";  S1 have global scope     int main ()   {       string s2 = "world";//s2 have local scope         //uses global S1; Prints ' ' Hello World '       cout << s1 << ' << s2 << std::endl;           An int type of S1 will mask the std::string type of s1      int s1 = n;//S1 is local and hides global S1         //uses local S1; prints "wor LD '       cout << s1 << ' <<  s2 << Std::endl;        return 0;   }  </span></span>
English moment: Destiny is not a matter of chance. It ' s a matter of choice. (Destiny lies not in opportunity, but in choice.) )

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Primer learning notes and thinking _1----variables and basic types

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.