On syntactic syntax of C + + and forced data type conversion _c language

Source: Internet
Author: User

A program contains one or more program units (each program unit constitutes a program file). Each program unit consists of the following parts:
preprocessing commands. such as #include command and #define command.
Declarations section. For example, the declaration of data types and functions, and the definition of variables.
Function. Includes the first and function bodies of functions, which can contain several declaration statements and execution statements in the function body.

As the following is a complete C + + program:

#include <iostream>//preprocessing command
using namespace std;//The declaration part outside the function
int a=3;//The declaration part outside the function
int main ()//functions First
{
 The declaration part of the float b;//function
 b=4.5;//EXECUTE statement
 cout<<a<<b;//EXECUTE statement return
 0;//execute Statement
}

If a variable is declared outside of a function, this variable is a global variable, and its valid range is from the line to the end of this program unit. If a variable is declared within a function, the variable is a local variable, and its valid range begins at the beginning of the line to the end of this function. C + + program structure can be represented by graphs.

Programs should include data descriptions (implemented by declarative statements) and data operations (implemented by execution statements). The data description mainly includes the data type declaration, the function and the variable definition, the variable initialization and so on. The task of data manipulation is to process the data that has been provided.

The smallest self-contained unit in a C + + program is a statement (statement). It is equivalent to a sentence in an article. The sentence ends with a period. Statements typically end with semicolons (compound statements end with curly braces).

C + + statements can be divided into the following 4 kinds.

1. Declaration statements
such as int A, b; In C, only the actual operation is called a statement, the definition of the variable is not a statement, and the definition of the variable must appear before all the program statements in this block. So C programmers have developed a habit of defining all variables at the beginning of a function or block. In C + +, the definition of a variable (and other objects) is considered to be a statement and can appear in any row in a function, that is, where other program statements can appear, or outside the function. This is more flexible and makes it easy to localize variables (the scope of a variable begins with a declaration statement to the end of this function or block).

2. Execution statement
notifies the computer to complete a certain operation. The execution statement includes the following several.

1 Control statement, complete a certain control function. C + + has 9 kinds of control statements, namely:
if () ~else~ (conditional statement)
for () ~ (Circular statement)
while () ~ (Circular statement)
Do~while () (Circular statement)
Continue (end this circular statement)
Break (abort execution switch or loop statement)
Switch (multiple branch selection statement)
Goto (Turning statement)
Return (returns the statement from the function)

2 function and Flow object invocation statement. A function call statement consists of a function call with a semicolon to form a statement, for example:

  Sort (x, y,z); Suppose the sort function is defined, it has 3 parameters
  cout<<x<<endl;//Flow Object invocation Statements

3 An expression statement. A statement consists of an expression plus a semicolon. The most typical is that an assignment expression constitutes an assignment statement.

  I=i+1//is an assignment expression
  i=i+1;//is an assignment statement

The last semicolon of any expression can be a statement. A semicolon must appear at the end of a statement.

Expressions can constitute a statement is an important feature of C and C + + languages. Most statements in C + + programs are expression statements (including function call statements).

3. NULL statement
The following is an empty statement:

  ; The statement has only one semicolon

That is, a semicolon-only statement, it does nothing. Sometimes it is used to make the loop body in the turning point, or in the circular statement.

4. Compound statement
you can use {} to enclose some statements as compound statements. As follows is a compound statement.

{
  z=x+y;
  if (z>100) z=z-100;
    cout<<z;
}


Note: The last semicolon in the last statement in a compound statement cannot be omitted.

C + + coercion type conversion
different types of data in an expression are automatically converted to the type for operation. Sometimes the program creator can also use the coercion type conversion operator to convert an expression to the desired type. For example:

    • (double) A (convert a to double type)
    • (int)   (x+y) (Converts a X+y value to an integral type)
    • (float)  (5%3) (Converts the value of the 5%3 to float type)

The general form of coercion type conversion is:

  (type name) (expression)

Note: If the object you want to force type conversion is a variable, it can be enclosed in parentheses. If the object you want to force type conversion is an expression that contains multiple items, the expression should be enclosed in parentheses. If you write

  (int) X+y

The x is converted to an integral type and then added to Y.

The above mandatory type conversion form is the original C language use of the form, C + + has retained it to facilitate compatibility. C + + also adds the following form:

  Type name (expression)
such as
  Int (x)
or
  int (x+y)

Type names are not parentheses, and variables or expressions are enclosed in parentheses. This form is similar to a function call. But many people are still accustomed to using the first form to enclose the type name in parentheses, which is more clear.

What needs to be explained is that when you force a type conversion, you get an intermediate variable of the desired type, but the type of the original variable does not change. For example:

  (int) x

If x is specified as a float, and the value is 3.6, an intermediate variable of type int is obtained after the coercion type operation, and its value is equal to 3, while the original type and value of x are unchanged.

"Example" forces type conversions.

#include <iostream>
using namespace std;
int main ()
{
 float x;
 int i;
 x=3.6;
 i= (int) x;
 cout<< "x=" <<x<< ", i=" << i<<endl;
 return 0;
}

The results of the operation are as follows:

  X=3.6,i=3

The type of x is still float, and the value is still equal to 3.6.

From the above, there are two types of conversions, one is in the operation without the user specified, the system automatic type conversion, such as 3+6.5. The second is to force type conversions. You can use coercion type conversions when automatic type conversions do not implement the purpose. In addition, when a function is called, sometimes in order to make the actual participating parameter types consistent, you can use the coercion type conversion operator to get a parameter of the desired type.

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.