C language learning record -- types, operators and expressions

Keywords: C C++

Variable name

Naming restrictions

- A name is a sequence of letters and numbers. The first character must be a letter.
- Do not underline variable names“_"start.
- Distinguish between uppercase and lowercase letters (i.e x And X Are two different names).

In traditional C language usage, variable names use lowercase letters, and symbolic constants all use uppercase letters.
The selected variable name should be able to express the purpose of the variable literally as much as possible, which is not easy to cause confusion.
Local variables generally use shorter variable names (especially loop control variables), and external variables use longer names.

Data type and length

Basic data type

	char    Character type, which occupies one byte and can store one character in the local character set.
	int     Integer, which usually reflects the most natural length of an integer in the machine used.
	float   Single precision floating point.
	double  Double precision floating point.

Some qualifiers can be applied to basic types,
as

1  short int = short
2  long int = long
3  short Generally 16 bits.
4  int Can be 16 or 32 bits.
5  long At least 32 bits.
6  short Type cannot be longer than int Type, int Type cannot be longer than long Type.
7
8
9   unsigned/signed modification char And any integer.
10  char Whether it is signed or unsigned depends on the machine implementation, but printable characters are always positive.
11  float/double/long double Depending on the specific implementation, it can represent the same length or two or three different lengths.

constant

- int Type constant, example 1234
- long Type constant, example 1234 l Or 1234 L
- An integer constant is too large to be exceeded int When, it will be regarded as long Treatment, e.g. 12345678
- Unsigned int Constant, example 1234 u Or 1234 U
- Unsigned long Constant, example 1234 ul Or 1234 UL
- double Constant (floating point constant without suffix), example 123.4 Or 1 e-2   ((contains decimal points or exponents, expressed as floating point constants)
- float Constant (suffix) f or F),Example 123.4f Or 1 e-2f Or 123.4F Or 1 e-2F
- long double Constant (suffix) l or L),Example 123.4l Or 1 e-2l Or 123.4L Or 1 e-2L



- The expressed value of an integer can be hexadecimal, octal, or hexadecimal.
-8 Binary form: 0 yyy  (yyy (represents an integer constant)
-16 Binary form: 0 xyyy Or 0 Xyyy
-Example:
-10 Binary: 31
-8 Hex: 037
-16 Binary: 0 x1f Or 0 x1F
-8 Hexadecimal and hexadecimal constants can also be suffixed with L To represent long,and U To represent unsigned
-Example:
-0XFUL express unsigned long The constant value is 15 in base 10	




-Character constants are integers with characters in single quotes.
-Character constant value is the value of the character in the character set of the machine.
-Example:
-stay ASCII Character set'0'The value is 48.
-Character constants participate in numeric operations in the form of integers.
-In string and character constants, specific characters can be expressed by escape sequences,
-as\n,
-Escape characters look like two characters, but represent only one.
-In addition, bit patterns of any byte size can be specified,
-adopt'\ooo',ooo It can be 1 to 3 octal numbers
-Or through'\xhh',hh You can say one or more hexadecimal numbers.

Escape sequence set:

Escape characterdescribe
\aRinger
\bBS-Backspace
\fPage feed
\nNewline character
\rCarriage return
\thorizontal tab
\vVertical tab
\\Backslash
\?question mark
\'Single quotation mark
\"Double quotation mark
\oooOctal number
\xhhHexadecimal number
- character constants '\0'Represents a character with a value of 0, i.e null Character.
- A constant expression is an expression that contains only constants.
- Such expressions are evaluated at compile time, not at run time. They can appear anywhere constants can appear.


- A string constant or string literal is a sequence of 0 or more characters surrounded by double quotes.
- For example:
- "I am a string"
- or
- "" /* Empty string */
- Double quotation marks do not belong to character content and are only used to define.
- The same escape sequence applies to the in character constants and also to strings.
- \"Represents a double quote character. Multiple string constants can be concatenated.
- "hello, " "world"
- Equivalent to
- "hello, world"


- A string constant is an array of characters consisting of empty characters'\0'To terminate.
- Standard library functions strlen(s) You can return string parameters s Length, length does not include the end'\0'. 
- Pay attention to distinguish between a character constant and a string containing only one character.
- Example:
- 'x'and"x"
- The former is an integer used to generate characters x The value in the machine character set.
- The latter is a character array containing one character,'x',And one'\0'


- An enumeration constant is another type of constant.
- An enumeration is a list of constant integer values.

statement

- All variables need to be declared before use.
- Specific statements, which may be implied.
- A declaration that specifies a variable type and contains a list of one or more variables of that type.
- Multiple variables in a declaration statement can be declared in multiple declaration statements.

- A variable may also be initialized when declared.
- If the declaration is followed by an equal sign and an expression, the expression is equivalent to an initialization expression,

- If the variable is not an automatic variable, initialization can only be performed once.
- Conceptually, the initializer must be a constant expression before the program starts executing.
- An automatic variable that displays initialization. It will be initialized every time its function or program block enters.
- And the initialization expression may be any expression.
- External variables and static variables are initialized to 0 by default.
- Automatic variable. When not explicitly initialized, the value is undefined (i.e. invalid value).
- Any variable declaration can be used const Qualifier. The value of the variable specified by the qualifier cannot be modified.
- For arrays, const It can also be used together, which indicates that the function cannot modify the value of array elements.
- const double e = 2.71828182845905;

Arithmetic operator

- Binary arithmetic operator: +  -  *  /  %(Modulo operator)
- %Cannot be applied to float or double
- For integer division, truncation direction and pair%The result symbol depends on the machine.

Relational and logical operators

- Relational operator  >   >=   <   <=
- Equality Operator  ==   !=
- Logical operator  &&   ||
- from && and || The connected expression is evaluated from left to right. Once the result is known, the evaluation stops.
- Logical non operator !,Convert a non-zero value to 0 and a 0 to 1

Type conversion

-  There is a hermit type conversion in an expression,
-  When an expression contains multiple types, multiple independent items will be converted to a unified type for operation,
-  It is common to include different integers or integers and floating points,
-  The short integer is converted to a long integer that can contain it, and the entire line is converted to a floating-point type.
-  char May be implemented as unsigned or signed types.

-  The highest bit of a visible character is generally 0 in the character set, so when it is implemented without symbol or with symbol, when char Turn into int Results when int It's all positive.
-  but char When it contains other non visible characters, when the maximum is 1, it is converted to int Whether you get a negative number or an integer depends on char Is implemented as signed or unsigned.
-  To avoid uncertainty in such situations, you can explicitly use signed char,unsigned char

If there is no unsigned operand, just use the following informal rules:

- If one of the operands is of type long  double ,The other operand is converted to long double Type;
- If the type of one of the operands double,The other operand is converted to double Type;
- If the type of one of the operands float,The other operand is converted to float Type;
- take char And short Operands of type are converted to int Type;
- If one of the operands is of type long ,The other operand page is converted to long Type.

·

-  For type conversion, signed and unsigned are not considered first.
-  First, for integers, each integer has a storage size, which is different on different machines.
-  Suppose an operational designnInteger operands,
-  Among the integer numbers, the type with the largest size is assumedt. 
-  Then,
-  The first step is to determine the unified type of all operands.
-  The principle is:
-  yesnSort the types and get the largest type.
-  All types are converted to the maximum type before they participate in the operation.
-  Collation:
-  If typeASize less than typeBSize, thenAless thanB
-  typeASigned<typeAUnsigned

-  The type size thus obtained is less thanint,Convert all types directly tointParticipate in the operation.

-  For assignment or cast type conversion, the conversion is performed according to the specified target type.
-  When a large type is transferred to a small type, there is a problem of information loss.

Self increasing operator and self decreasing operator

- increment operator ++Increment its operands by 1, self subtraction operator--Decrements its operand by 1.
- It can be used as a prefix operator or a suffix operator
- Note that,++/-- A or A ++/-- As a whole, it is an expression, and there is an expression result.
- after++/--Version, the value of the overall expression is the unchanged value. The variable value increases automatically after the operation/Minus 1
- front++/--Version, the value of the overall expression is the variable value after the change. The value of the variable increased automatically after the operation/Minus 1
- Self increasing and self decreasing operators can only be used for variables

Bitwise Operators

- Can only act on integer operands[Signed or unsigned char/short/init/long]
operatordescribe
&Press AND
|Bitwise OR
^Bitwise exclusive OR (OR)
<<Shift left
>>Shift right
~Bitwise negation (unary operator)
/* getbits: get n bits from position p */
// Returns an unsigned. The lower N bits are the N bits in X that we are interested in. The remaining bits are 0.
unsigned getbits(unsigned x, int p, int n)
{
	// X > > (P + 1-N) remove the low bits that are not interested in X, and ensure that the processed unsigned low N bits are the ones we are interested in
	// ~(~ 0 < < n) construct an unsigned type number, the lower N bits are all 1, and the rest are all 0
	return (x >> (p+1-n)) & ~(~0 << n);
}

Assignment operators and expressions

- Assignment Operators  op=
- op Can be:+  -  *  /  %  <<  >>  &  ^  |
- expr1 op= expr2
- Equivalent to
- expr1 = (expr1) op (expr2)

- example:
x * = y + 1
 Meaning
x = x * (y+1)
instead of
x = x*y + 1

- The type of the assignment expression as a whole is the type of the left operand, and the result is the copied result.

Conditional expression

if ( a > b )
	z = a;
else
	z = b;
/* Used to find the larger of a and b and save the results to z  */

Conditional expressions (using the ternary operator "?:") provide another way to write this program and similar code snippets.

expr1 ? expr2 : expr3

In the above expression, expr1 is calculated first. If its value is not equal to 0 (true), the value of expr2 is calculated and used as the value of the conditional expression. Otherwise, the value of expr3 is calculated and used as the value of the conditional expression.
Only one expression in expr2 and expr3 can be evaluated.
The above statement can be rewritten as:

z = (a > b) ? a : b;    /* z = max(a, b) */

If the types of expr1 and expr3 are different, the type of the result will be determined by the conversion rule.
For example, if f is of float type and n is of int type, there is an expression

(n > 0)? f : n

Is a float type, regardless of whether n is a positive value.

Operator priority and evaluation order

operatorAssociativity
( ) [ ] -> .From left to right
! ~ ++ – + - * & (type) sizeofright to left
* / %From left to right
+ -From left to right
<< >>From left to right
< <= > >=From left to right
== !=From left to right
&From left to right
^From left to right
|From left to right
&&From left to right
||From left to right
?:right to left
= += -= *= /= %= &=right to left
^= |= <<= >>=From left to right
'right to left
yes&& / || / ?: / ,
The order in which operands are evaluated is specified.
/This is a language trap, so we should avoid uncertain code.
In the case of other operators, there is no explicit requirement for the evaluation order of each operand involved in the operation, which depends on the implementation.
Similarly, when calling a function with multiple formal parameters, the evaluation order of multiple arguments is not explicitly required.
// In this call, if the evaluation order of multiple arguments is different, inconsistent results will be generated, which should be avoided.
	printf("%d %d\n", ++n, power(2, n));

	// When the evaluation order of the left and right sides is different, different results will be produced.
	a[i] = i++;

reference resources:

https://blog.csdn.net/x13262608581/article/details/108061295

Posted by cyberplasma on Mon, 18 Oct 2021 00:31:21 -0700