C language learning - operators and data types

1. Operator

arithmetic operators

  1. Basic arithmetic operators:+, -, *, %, ++, –
  2. The type of arithmetic operation result in C language is determined by the type of the two operands involved in the operation. When two ints are used for arithmetic operation, the result must also be of type int (when two integers are divided, the actual operation is an integer division operation) )
  3. % NumberRemainder operator: Remainder operation requires that the left and right operands must be integers. The sign of the remainder operation result in C language is determined by the left operand. )
  4. % sign and / signThe denominator of can not be zero

Relational operators

  • The result of a relational operator is 1 or 0 (1 represents true, 0 represents false

Conditional operator (ternary operator)

  1. General form:条件1 ? 条件2 :条件3
  2. Meaning: Determine whether the result of condition 1 is true. If the result of condition 1 is true, the result of the entire conditional expression is condition 2, otherwise the result of the entire conditional expression is condition 3.
  3. The ternary operator is generally interchangeable with if-else

a++ give++a

  1. a++: The value of a is incremented by one after this statement ends.
  2. ++a: When this statement is executed, the value of a has been increased by one.

2. Variables

definition

  • Variable definition: <数据类型> <变量名> = <初值>When a variable is defined, it should be assigned an initial value.
  • Variable name: also called identifier, can only consist of letters, numbers, and underscores. Numbers cannot be in the first place, and C language keywords cannot be used as variable names. The identifier can be up to 32 characters long.
  • You cannot use continuous equality to assign values ​​to variables, for exampleint a = b =0;
  • Multiple variables cannot be assigned directly after being defined, separated by commas, for examplea, b, c = 0;

const keyword

  • Keywords used to define read-only variables

Global variables and local variables

  1. Scope: the scope in which the variable takes effect
  2. Local variables: Variables defined in a function can only be used in the function. They are called local variables and their scope is within the curly brackets of the function.
  3. Global variables: variables defined during preprocessing, which can be used globally and have a global scope.

3. Basic data types

floating point number

  • float type: single-precision floating point number, 4 bytes (B), 32 bits (b). Use %f
  • Double type: double-precision floating point number, 8 bytes, 64 bits. Use %lf

Operation

  1. When two integers are operated on, the result is still an integer.
  2. When integers and floating-point numbers are operated together, the integers are automatically converted into floating-point numbers, and the result is also a floating-point number.

floating point output

  1. Rounding: When using %.digit f and %.digit lf, the data will be automatically rounded.
  2. Manual remainder (no rounding)
int main()
{
    
    
   double pi = 3.1415926; //题目测试样例数字
   int temp = (int)(pi * pow(10,n)); //希望保留n位小数
   pi = temp / (pow(10,n) * 1.0);	//将结果处理为有小数的浮点数
   printf("%.nf",pi);		//保留小数点后n位并输出结果
   return 0;
} 

Guess you like

Origin blog.csdn.net/weixin_73402838/article/details/131029203