C ++ type (1), Type

Source: Internet
Author: User

C ++ type (1), Type

Variable

Functions of Variables

  • Where is the information stored?
  • Value to store
  • Storage type information

Naming rules

  • Only letters, numbers, and underscores can be used in the name.
  • The first character of the name cannot be a number.
  • Case Sensitive characters
  • The C ++ keyword cannot be used as a name.
  • Names starting with two underscores (_) or underscores (_) and uppercase letters are reserved for the compiler and its resources. names starting with an underscore are used as global identifiers.
  • The length of the C ++ name is unlimited.
Integer

1 int _tmain(int argc, _TCHAR* argv[])2 {3     using namespace std;4     cout << sizeof(short) << endl;5     cout << sizeof(int) << endl;6     cout << sizeof(long) << endl;7     cout << sizeof(long long) << endl;8     return 0;9 }

Integer literal

Decimal, octal, hexadecimal, and other representation methods are only used to facilitate expression. They are all stored in binary format.

The following are the cout output methods,

1 int waist = 42; 2 cout <waist <endl; 3 cout 
Const qualifier

The common format for creating constants is as follows:

Const type name = value; const must be initialized in the Declaration.

Const comparison # advantages of define:

  • Can explicitly specify the type
  • You can use the scope rules of C ++ to restrict the definition to a specific function or file.
  • Const can be used for complex types.
Floating Point Number

Floating point numbers can be divided into three types: float, double, and long double. These types are described based on the valid digits they can represent and the minimum allowable exponential range. For example, 2.5, 3.32e + 022, and 1.23f are all floating point numbers.

Advantages and disadvantages of floating point over INTEGER:

  • It can be a value between integers.
  • The value range is greater than the integer value.
  • The operation speed is slower than the integer and the accuracy is low.
Type conversion
  • Convert the initialization value

C ++ allows the value of one type of variable to another type of variable. This type of conversion usually results in loss of data accuracy.

1     int num1 = 5;2     float num2 = num1;
  • Conversion during initialization in {} mode

This conversion method requires strict requirements. The list initialization does not allow narrowing. For example, you cannot convert a floating point to an integer.

  • Convert in Expression
  • Transfer Parameters

Similar to the first conversion method

  • Forced type conversion

The general format is as follows:

Method C: (typename) value

C ++ method: typename (value) static_cast <typename> (value)

1     char ch = 'A';2     cout << static_cast<int>(ch) << endl;

 

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.