C + + —————— various variables

Source: Internet
Author: User

For US programmers, "variables" and "objects" can be used interchangeably with each other. -------------the opening word.

Variable: Provides a storage space with a name that can be manipulated by the program. Consists of a type specifier and several lists immediately following it, where the variable names are separated by commas and end with a semicolon. like int sum = 0,value,unit_sold = 0; Sales item, where item is a variable of type sales.

Object: Refers to a piece of memory that is capable of storing data and having some kind of space.

Initialization and assignment are not the same concept; initialization is the creation of a variable with an initial value, and the meaning of the assignment is to erase the object's current value and replace it with a new value. Since C++11 started, it has been possible to use an int sum{0}, such as initialization, called List initialization!

Variables defined outside any function are initialized to 0, with the exception that the built-in type variables defined inside the function will not be initialized. The value of an uninitialized built-in type variable is undefined, and if you attempt to copy or otherwise access the type, it will throw an error!! An object of a built-in type defined inside a function body is not defined if it is not initialized. The object of the class is determined by the class if there is no initialization displayed. Therefore, it is recommended that we initialize each of the built-in types of variables, although not necessarily, but if we are not sure of the security of the program after initialization, then this is a simple and reliable way to--------------to develop a variable to initialize the good habit!!!

The declaration specifies the type and name of the variable, but it also applies a storage space and may assign an initial value to its variable.

If you want to declare a variable instead of defining it, add a keyword extern before the variable instead of the initialization variable: extern int i;//declares I rather than defines I;int j;//declares and defines J; inside the function body, If you attempt to initialize a variable that is marked by the extern keyword, an error is raised. Variable can and can only be defined once, but can be declared more than once!

Naming rules: 1. You cannot have two underscores in a user-defined identifier, or you can use underscores that are immediately preceded by uppercase letters, and identifiers outside of the function body cannot begin with an underscore!

2. Variable name should be written in lowercase, easy to read.

3. User-defined types typically start with a capital letter.

4. If the identifier consists of multiple words, there should be a distinct distinction between the words.

Recommendation: define it the first time you use a variable, meaning that it is a good choice to define it near the place where the object was first used, because it makes it easier to find the definition of the variable. More importantly, when the definition of a variable is close to where it was first used, we also give it a reasonable initial value!

Composite type: Refers to a

C + + —————— various variables

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.