C ++ tutorial Chapter 2-variable Basics

Source: Internet
Author: User
Tags integer numbers

C ++ tutorial Chapter 2-variable Basics

 

 

1. Basic built-in types

C ++ contains the arithmetic and Null Types. The arithmetic type includes characters, integer numbers, Boolean values, and floating point numbers. Char, int, long, bool, float, double, etc. Note that there is an unsigned type called the unsigned number, and the unsigned number constant is greater than 0. Note that if you execute in while:

 

unsignedint a = 10;while(a> 0){…}

 

 

Because a is an unsigned integer, a Evergrande is at 0, and the logic judgment is true, the program will be in an endless loop.

 

Use Case:

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

Integer int: int a = 10; int B =-10;

Unsigned int: unsigned int a = 10;

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

Float/double: float a = 1.12321; float B = 3.54234

Overflow: The length occupied by the built-in type in the memory is limited. If the indicated value exceeds a certain length, overflow occurs. For example, if the char length is 1 byte, that is, 8 bits, if the execution is: char a = 0x123456789, 9 bits are assigned to a, overflow occurs.

 

2. type conversion

Conversion between basic built-in types

Int to bool: If the int value is not 0, it is true after being converted to bool; otherwise, it is false.

Floating Point to int: truncates the number of decimal points

Int to floating point: After conversion, the decimal point of the floating point is 0.

 

3. Nominal value constant

The literal value constant is an unchangeable amount. For example, 43, "hello" (String constant), etc.

In C ++, The String constant is of the const char * type. * Indicates the pointer. The pointer concept will be explained later.

 

4. Escape Sequence

Some characters have special meanings in C ++. In these cases, the conversion sequence is used.

For escape sequences, refer:

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

 

5. Variable Initialization

We recommend that you assign an initial value to all variables during definition, that is, execute initialization. This line can avoid unexpected troubles.

 

If an initial value is not assigned to a variable after a variable is defined, the variable is initialized by default and assigned a default initial value. The initial value is determined by the variable type.

 

Note the differences between initialization and assignment, which is important in C ++. Initialization is to assign an initial value to a variable when it is created, and assign a value to erase the current value of the object and replace it with a new value.

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

Value assignment: int a; a = 1; // here, a is initialized to 0 by default, and then assigned a value to 1.

 

6. Scope of Variables

Variables can be divided into global variables and local variables. Currently, you only need to know that the variable defined in the function body is a local variable and only valid in the function body. (The static variable will be learned later, that is, the static variable is an exception and is globally valid after the variable is defined)

Example:

 

int a = 0;void fun(){int fa = 0;int fb = 1;}int main(){int b = 4;return}

 

Where a is a global variable, fa, fb, and B are all local variables.

 

7. Differences between variable definition and Declaration

Note the differences between definitions and declarations. The definition is to open up a space in the system memory to store variables. The Declaration tells the program that this variable exists. Variables can be defined only once, but can be declared multiple times.

 

Summary: Today we have introduced some basic knowledge about variables. The next chapter will analyze the array concept in composite variables in detail.

 

PS: here we will explain the main function parameter list in the previous chapter.

Int_tmain (int argc, _ TCHAR * argv [])

The main parameter list contains two parameters, one being an integer int and the other being a TCHAR * array, which is actually a char ** type and is represented as a string array.

After compiling a program, you may want to attach some parameters to it when executing this program through the command line. As shown in

 

 

As you can see, after code ShowPar. cc is compiled to test through g ++, argc is the total number of parameters when test is called, and argv stores each additional parameter.

For example:./test hello and

Run the test program. The values of the parameters hello and argc are 3, and the contents of argv are./test, hello, and.


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.