C + + Tutorial Chapter II-Variable basics

Source: Internet
Author: User

Reprint Please specify source: Http://blog.csdn.net/miaoyunzexiaobao


1. Basic built-in type

C + + contains the arithmetic type and the empty type. Where the arithmetic type contains characters, integers, Boolean values, and floating-point numbers. namely char,int,long,bool,float,double and so on. Note that there is a type unsignedcalled unsigned number, with an unsigned number constant greater than 0. It is important to note that if you are executing in while:

<span style= "FONT-SIZE:14PX;" >unsignedint a = 10;while (a> 0) {...} </span>

Because a is unsigned integer, so a is more than 0, logic judgment is true, the program will fall into a dead loop.


Case:

Character Char:char a = ' a '; char b = ' 1 '; char c = '! '; char a = 0x0000

Integer int:int a = ten; int b = -10;

Unsigned integral type unsigned int:unsigned int a = ten;

Boolean bool:bool a = true; bool B = false;

Floating point float/double:float a = 1.12321; float B = 3.54234

Overflow: The built-in type occupies a limited length in memory, and overflows if the value represented exceeds a certain length. If char is 1 bytes long, i.e. 8 bit, execute:char a = 0x123456789, assign 9 bit to a , an overflow occurs.


2. type Conversion

Basic built-in types can be converted between each other

int to bool: Trueif the value of int is not 0, or falseif it is converted to bool .

Floating point to int: The number after the decimal point is truncated

int to floating point: 0 decimal place for floating-point number after conversion


3. literal constants

The literal constant is the amount that cannot be changed. For example , "Hello" (string constant), etc.

In C + + , the string constant is the const char* type. Where * represents the pointer. The concept of pointers will be explained later in the content.


4. Escape Sequences

Some characters have special meanings in C + + , in which case the transpose sequence is used.

Escape sequences can be referenced by:

http://blog.csdn.net/qustdjx/article/details/7728684


5. Initialization of variables

It is recommended to assign an initial value to all variables when they are defined, that is, to perform initialization, which avoids most unexpected hassles.


After a variable is defined, if it is not assigned an initial value, the variable is initialized by the system by default, giving a default initializer. The initial value is determined by the variable type.


Note the difference between initialization and assignment, which is important in C + + . The meaning of initialization is to give it an initial value when creating a variable, meaning to erase the object's current value and replace it with a new value.

Initialize:int a = 1;// here a is initialized to 1

Assignment:int A; a = 1;// here a is first initialized to 0 by default , then an assignment operation is performed, and A is assigned a value of 1.


6. Scope of variables

Variables can be divided into global variables and local variables. It is only necessary to know that the variables defined inside the function body are local variables and are valid only within the function body. ( you will then learn the static variable, which is the exception to the static variable, which is globally valid after the variable is defined )

Cases:

<span style= "FONT-SIZE:14PX;" >int a = 0;void fun () {int fa = 0;int fb = 1;} int main () {int b = 4;return}</span>

Where a is a global variable,FA,fb,b are local variables.


7. the difference between a variable definition and a declaration

Note the distinction between definition and declaration, which is defined as a space in system memory for storing variables. The declaration is telling the program that there is this variable present. A variable can be defined only once, but may be declared more than once.


Summary: Some of the basics of variables are introduced today, and the next chapter will be a detailed analysis of the concept of arrays in compound variables


PS: Here is an explanation of the main function parameter list in the previous chapter.

Int_tmain (int argc, _tchar* argv[])

Where main has two parameters in the argument list, one is int, and the other is the tchar* array, which is actually the char** type, represented as an array of strings.

When you have compiled a program, you may want to attach some parameters to it when you execute the program from the command line by command. As shown



As you can see, after compiling code showpar.cc to test through g++ , when you call test ,argc is the total number of parameters,argv holds each additional parameter.

such as:./test Hello and

Execute the test program with additional parameters Hello and AND,ARGC values of 3,ARGV, respectively./test,hello and


C + + Tutorial Chapter II-Variable basics

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.