C++ variables and primitive types

The standard library defines four IO objects, namely cin for standard input, cout for standard output, cerr for error output, and clog for general information output.

Each expression in c++ produces a result. For the operator, it is an output-input operator, which accepts the right operand and returns the value of the left operand. For example cin>>i>>j;  

If you understand correctly, comments are removed by regular expression-related definitions when precompiling, then if there are block comments /**/ The criteria for the state machine to look for is /* until the next */ stop, so the comment is Can not be nested, such as /* kk/**/dd*/ then dd will be considered a code segment.

In the exercise, std::cout<</* "*/" */; although we have commented until the output value, so the right operand is empty, which will report an error.

 

basic type


  • If the assignment of integer type overflows, the remainder operation is generally performed . For example, the result of assigning -1 to unsigned char is 255, and 255 is the modulo of -1 to 256.
  • Literal constants, such as 9, etc., are the right half of the smallest expression in our regular expression from the perspective of compilation principles. If we define a variable as num -> 0 |1|2|3...9|10 In fact, 0 to 10 here are literal constants, and the symbols they represent can no longer be divided into subcomponents and compiled into a syntax tree.
  • So literals are because they can only be called by their values, so constants are because their values ​​cannot be modified, and each literal has a corresponding type. Such as 0 is int, 3.1415926 is double and so on.
    • Integer literal value rules: decimal 24; octal (0 zero) 024; hexadecimal 0x14, if you want to represent a long integer, add L after the value, and add U for unsigned, such as 128U, 1024UL, etc.
    • Floating-point literal value rules: Decimal or scientific notation can be used, E means exponent, such as 3.14159F=3.14159E0F, .001F=1E-3F. 3.14 is double by default, 3.14f is float, 3.14L is long double, 1024f It is wrong that the integer 1024 cannot be connected to f, and 3.14UL is also wrong. L has no U
    • Boolean literals and character literals: true,false,'a', or L'a' if wchar_t
    • Escape character sequences: /n /t etc.
    • String literals: Zero or more characters enclosed in double quotes. For C compatibility, all string literals are automatically appended by the compiler with a null character at the end. 'A' //single quote:character literal "A" //double quote:character string literal.
    • String literal concatenation, similar to java but without the + sign, such as cout<<"a""b""c"; the result is "abc"
  • Object: An area in memory that has a type.
    • define object
      • There are two ways to initialize: 1.int val(1024); 2.int val=1024; The former is called direct initialization, and the latter is called copy initialization
      • When the variable is initialized, the value matches, such as int month=09, which is assigned in the form of octal, and 9 is already exceeded at this time. should be int month=011
      • Variable initialization rules, if it is the initialization of built-in type variables, the outside of the function is initialized to 0 or an empty string, and if it is inside the function, it is not initialized. If it is a class type variable initialization, no matter where the initializer is not specified, the default constructor is used by default. 
  • Declaration and definition, variables must be defined only once. extern declares a variable, it is not a definition, nor does it allocate storage space, it just shows that the variable is elsewhere in the program. Variables in a program can be declared multiple times, but only once. extern double pi=3.14; define declaration extern double pi; declare double pi; repeat definition. Note that extern can be declared and defined, but only outside the function body, that is, the variable is a global variable.
  • const qualifier, the object must be initialized, the default is a local variable of the file, if you want to use it in other files, as follows //file1.cpp extern const int size=19; //file2.cpp extern const int size; int a=size ; ..
  • Reference reference. It is essentially an alias for a variable, and the variable points to the same address content. A reference must be initialized. As long as the reference exists, it remains bound to the object specified at the time of initialization, and the reference cannot be bound to another object. A const reference is a reference to a const variable. In fact, a non-const reference can only bind variables of the same type as the reference. A const reference can be bound to an object of a different but related type or to an rvalue.
  • 1  int ival= 1.01 ;         // legal, implicitly converted to 1 
    2  int &rval1= 1.01 ;         // illegal, non-const references cannot be rvalues 
    ​​3  int &rval2=ival;            // legal 
    4  const  int &rval3= 1 ;           // legal 
    5  
    6 rval2= 3.14 . 159 ;            // legal 
    7 rval2=rval3;               // legal 
    8 ival=rval3;                 // legal 
    9rval3=ival;                 // Illegal, const value cannot be modified
    View Code
  • typedef name, used to define a synonym for the type, typedef double wages;
  • Enumeration, enum open_modes {input , output, append} represents the three values ​​​​0, 1, 2 respectively. Of course, you can also customize constants, enum Points { pd=2,pw,px=3,pl}; In this example, if there is no specified value, it will increase by default, so pw=3,pl=4;

 

Class types and their definitions


 C++ programs are often composed of multiple files, and a variable generally has the stages of declaration, definition, and initialization. The declaration is used to indicate to the program the type and name of the variable, and the definition has the corresponding storage space allocated to it in addition to the declaration. Initializers must have storage space to initialize.

In the C++ language, variables must be defined only once, and variables must be defined or declared before using them.

See this blog post for restrictions on specific header files .

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325629556&siteId=291194637