Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - The difference between expression and sentence
The difference between expression and sentence
An expression is a combination of constants, variables and operators, and returns a result value after calculation. The closing symbol of the expression is a semicolon (; ), all statements and declarations in C language end with semicolons, and the statements before semicolons are incomplete. What is the difference between a statement and an expression? Come and have a look with me.

I. Expression

The expression itself does nothing but return the result value. If the program does nothing to the returned result value, the returned result value is invalid. Expressions have two functions, one is placed on the right side of the assignment statement, and the other is used as a parameter of the function (which will be introduced later).

The result value returned by the expression is typed. The data type implied by an expression depends on the types of variables and constants that make up the expression. So the return value of the expression may be an integer with a certain size, a floating-point type with a certain precision, or a pointer type.

There is also the problem of type conversion, which I mentioned earlier when I talked about integer operation. The principle of type conversion is to automatically switch from low level to high level (unless manually controlled). The conversion order of calculation is basically as follows:

Character type->; Integer-> Long integer-> Floating point type->; Single precision type->; Double precision real number type

That is, when a character type and an integer type are operated together, the result is an integer type; If integer type and floating-point type are operated together, the result is floating-point type; If a double-precision type participates in the operation, the answer is a double-precision type.

The cast is like this. Enclose the type specifier in parentheses, and then convert the following variables to the required type. For example:

(int)a;

(float)b;

The first formula is to convert a into an integer, and if there is a decimal part, it will be discarded.

The second formula is to convert b into a floating-point type, and if it is an integer, it is followed by 0.

The return value of each expression has logical characteristics. If the return value is non-0, the expression returns true, otherwise it returns false. This logical feature can be used in program flow control statements.

Sometimes expressions do not participate in operations, such as:

If (a||b)

5 & gt3? a++:b++;

When A is true, B does not participate in the operation, because no matter what B does, the condition is always true.

Second, the sentence

(1) Transfer statement

In fact, this problem has already been discussed when talking about assignment operators.

Amount =1+2;

Total = Counter/3+5;

Area = height * width;

Perhaps you will find that these assignment statements are very similar to algebraic equations. In some cases, we can really understand them in this way, but sometimes they are different. Please read the following:

Num = Num+ 1;

This is obviously not an equation.

(2) Statements separated by commas

Like most languages, C allows the list of identifiers to be separated by commas in declaration statements, indicating that these operators are of the same variable type. For example:

Floating area, height and width;

But some programmers like to write identifiers on different lines. For example:

Floating area,

Height,

Width;

At least one advantage of this way of writing is that you can add comments after each identifier.

When you declare a variable, you can also directly assign a value to it, which is called variable initialization.

Such as: int a;;

a = 3;

Equivalent to:

int a = 3;

We also initialized some variables, some did not, such as:

int a=3,b,c = 5;

When initializing, the initialization expression can be arbitrary (there is a difference between global variables and static variables). Since comma operators operate from left to right, how about this?

int a=3,b=a,c = 5;

(3), standard input and output reports

Turbo C 2.0 standard library provides two console format input and output functions scanf (); And printf (); These two functions can read and write data in various formats on standard input and output devices. The scanf () function is used to read data from a standard input device (keyboard), and the printf () function is used to write data to a standard output device (screen). The usage of these two functions will be described in detail below.

1. Standard input statement

The scanf () function is a formatted input function that reads input information from a standard input device (keyboard). Its calling format is:

scanf(& lt; Format string >; , < address table >);

Formatted strings include the following three different types of characters;

(1). White space characters: White space characters will cause the scanf () function to ignore one or more white space characters in the input during the reading operation.

(2) Non-blank character: Non-blank character will make the scanf () function reject the same character as the non-blank character when reading.

(3). Format specifier: starts with% followed by one or several specified characters, which is used to determine the format of the output content.

Turbo C 2.0 provides the following input format specifiers:

━━━━━━━━━━━━━━━━━━━━━━━━━━

Symbolic action

──────────────────────────

%d decimal signed integers

%u Decimal unsigned integer

%f floating-point number

%s string

%c single character

The value of the %p pointer

%x, %X hexadecimal unsigned integer.

%o octal unsigned integer.

━━━━━━━━━━━━━━━━━━━━━━━━━━

The address table is the address of all variables that need to be read in, not the variables themselves, and the address symbol is'&'. The address of each variable is the same and independent.

For example:

Scanf(%d, %d,&me&);

In the above example, the scanf () function first reads an integer, then deletes the comma entered next, and finally reads another integer. If this character is not found, the scanf () function will terminate. If the separator between parameters is a space, you must enter one or more spaces between parameters.

Description:

(a) For each variable, what is a type descriptor? Enter the appropriate type of format descriptor. Otherwise, there will be a program error or the input data is different from the ideal value.

(b) For string array or string pointer variable, because the array name scanf pointer variable name itself is an address, there is no need to add&; Operator.

char *p,str[20];

scanf(%s,p);

scanf(%s,str);

The knowledge of specific strings and pointers will be introduced later.

(3). You can insert an integer between the% format specifiers in the format string, indicating the maximum number of digits in any read operation.

If it is stipulated in the above example that only 10 characters can be input to the string pointer p, then the first scanf () function statement becomes:

scanf(% 10s,p);

When the program is running, once the number of characters entered is greater than 10, P will not continue reading.

When actually using the scanf () function, there will be a problem, as shown in the following example:

When using multiple scanf () functions to input multiple character variables continuously, for example:

char c 1,C2;

scanf(%c,& ampc 1);

scanf(%c,& ampC2);

Run the program, enter a character A and press Enter (press Enter to complete the input), and execute scanf (%c,&; C 1), assign a to the variable c 1, but keep the carriage return in the buffer, and execute the input statement scanf (%c,&; C2), the variable C2 outputs a blank line. If you enter AB and press Enter, the result actually stored in the variable is c 1 for A and c2 for B. ..

To solve the above problems, you can add the clear function fflush () before the input function; (The usage of this function will be described at the end of this section).

(4). Adding "*" between format descriptors means skipping input, for example:

scanf(%3*d,& ampa);

When 12345 is entered, the first three characters are skipped and ignored, and the final value of variable A is 45.

2. Standard output statement

The printf () function is a print format function, which is generally used to output information to a standard output device in a specified format. This function is often used when writing programs. The calling format of printf () function is:

printf(& lt; Format string >; , < parameter table >);

Wherein the formatted character string comprises two parts: one part is normal characters,

These characters will be output as they are; The other part is to format the specified characters, starting with%, followed by one or more specified characters, to determine the format of the output content.

Parameter table is a series of parameters to be output, and its number must be as much as the number of output parameters described in the formatted string, and the parameters are used separately, which corresponds to each other one by one, otherwise unexpected errors will occur.

There are also two format descriptors for outputting statements.

Symbolic action

Floating point number in exponential form of %e

%g automatically selects the appropriate representation.

Description:

(1). You can insert a number between% and letters to indicate the maximum field width.

For example, %3d means to output a 3-bit integer, which is not enough for 3-bit right alignment.

%9.2f represents a floating-point number with an output field width of 9, where the decimal place is 2, the integer place is 6, and the decimal point occupies one place, which is not enough to align 9 places to the right.

%8s means to output an 8-character string, which is not enough for right alignment.

If the length or integer digits of the string exceed the specified field width, it will be output according to its actual length. But for floating-point numbers, if the number of bits in the integer part exceeds the specified integer bit width, it is output according to the actual integer bit; If the number of decimal places exceeds the width of decimal places in the description, the output will be rounded according to the width of the description.

In addition, if you want to add some zeros before the output value, you should add a zero before the field width item.

For example, %04d means that when a number less than 4 digits is output, it will be preceded by 0, making its total width 4 digits.

If floating-point numbers are used to represent the output format of characters or integers, the number after the decimal point indicates the maximum width, and the number before the decimal point indicates the minimum width.

For example, %6.9s means to display a string with a length of not less than 6 and not more than 9. If it is greater than 9, the content after the ninth character will be deleted.

(2). You can add a lowercase letter L between% and letters, indicating that the output is a long number.

For example, %ld means to output a long integer.

%lf means to output double floating-point numbers.

(3) The output can be controlled to be left-aligned or right-aligned, that is, adding a-sign between% and letters means that the output is left-aligned, otherwise it is right-aligned.

For example, %-7d means that the output 7-bit integer is left-aligned.

%- 10s means that the output 10 characters are left aligned.

Some special designated characters (please refer to the escape characters mentioned earlier)

━━━━━━━━━━━━━━━━━━━━━━━━━━

character function

──────────────────────────

\n newline character

\f Clear the screen and change pages.

\r input.

\t tab

\xhh indicates that the ASCII code is represented by 16.

I guess you like it.

1. More poetic emotional sentences

2. More meaningful sentences to express sadness

3. Want to express the success of cooperation.

4. The use of logical OR in C language

5. Express contempt for everything

6. Sentences expressing struggle

7. Five commonly used words to express the relationship between sentences