Mixing operation between the various types of numeric types, cast, arithmetic operations and arithmetic expressions

Mixing operation between the types of numeric types:

Type of the variable can be converted, also written before the blog which, data type conversion, there are two: one is an implicit conversion (automatic transfer), another is the explicit conversion (cast), typical data are automatically calculating implicit conversion.

Implicit conversion following rules :

  1. When the data type is not the same, first converted into the same category, then operation.
  2. Data conversion carried out in the direction of increasing length, and a long type such as int type calculates, first type long int converter further operation.
  3. All floating-point arithmetic operations are performed in double precision, even if only a single precision float containing it must first be converted into double type further operation.
  4. char type with short type when participating in operation, it must be converted to an int. (Cpu usually 32 with 64, but sometimes the system is 32-bit, so when running 32-bit or according to.)
  5. When carrying out the assignment operator, the data type of the right of an assignment will be converted to the left and then calculates the data type. If higher precision data types to the right, then the data will be part of the wreck off, reduce the accuracy, missing parts by rounding rounding forward.

The figure is a diagram of the type of automatic conversion rule of:
Here Insert Picture Description
Next, the following procedures to feel text columns in this regard:
[5.1]

#include <stdio.h>

void main()
{
   float PI = 3.14159;
   int s,r = 5;
   s = PI*r*r;
   printf("s=%d",s);
}

Because the left side of the equal sign of the assignment operator used is integer s, even if the PI is a float type will be cast to integer, the fractional part is discarded later. The final output results are as follows:
Here Insert Picture Description
a cast: the type of conversion is achieved by operation.
Its general form:
(type specifier) (expression)
whose role is to convert the result of the expression to the operation type indicated by the type specifier.
For example:
(int) a: to convert into a type of int

Note: Type foregoing character must be bracketed (single variable can not); whether it is cast or automatic conversion requires time operations are performed present temporary variable length data conversion, without changing the data described the variable defined type.

Or give real examples of some of the more obvious:

#include <stdio.h>
void main()
{
float f = 5.75;
printf("(int)f=%d,f=%f",(int)f,f);
}

The result output:
Here Insert Picture Description
here is well understood, the first result (int) f is the integer output, a second output of the float. When we printf ( "(int) f = % d, f =% f", (int) f, f); in (int) f, f is not removed after being given, but the results would be output mess because there is no value to pass it.

Arithmetic operations and arithmetic expressions:

The addition operator "+": binary operator, that is involved in computing the amount of two, having a binding, such as: 1 + 2, x + y ;
subtraction operator "-": binary operator, can be used as a negative value operator, in this case unary operator. Such as: -x, -5, etc. having left binding.
The multiplication operator "*": binary operator, having a left binding.
Division operator "/": binary operator, and has a left binding. When the amount of computation involved are integer, the result is an integer, decimal rounding. If there is a real, the result is double. Here to put a program seen cases:
[Example 5.2]

#include <stdio.h>

void main()
{
   printf("%d\n", 3/2);
}

1 is an output predictable results since both are all integers, so the result is an integer, the decimal part is discarded directly behind.
Here Insert Picture Description
% Remainder operator: binary operator, for example, 4 3%, the result is more than 1. (Equivalent to elementary school division learned, not divide it more than a few, and this is what we find in the remainder) as shown in Example 5.3
[5.3]

#include <stdio.h>

void main()
{
   printf("7/2=%d......%d",7/2,7%2);
}

Output:
Here Insert Picture Description

Published 10 original articles · won praise 1 · views 1244

Guess you like

Origin blog.csdn.net/weixin_43671182/article/details/94497394