C-language numerical representation and calculation

Source: Internet
Author: User
Tags see definition

---restore content starts---

First, the data type

View <limit.h> See definition of data type length on my windows

#define SHRT_MIN ( -32768)//Minimum (signed) short value
#define SHRT_MAX 32767//Maximum (signed) short value
#define USHRT_MAX 0xffff//maximum unsigned short value
#define INT_MIN ( -2147483647-1)//Minimum (signed) INT value
#define INT_MAX 2147483647//Maximum (signed) INT value
#define UINT_MAX 0xFFFFFFFF//maximum unsigned int value
#define LONG_MIN ( -2147483647L-1)//Minimum (signed) LONG value
#define LONG_MAX 2147483647L//Maximum (signed) LONG value

Also recommended <stdint.h> based on the number of bits selected data type such as uint16_t, unsigned 16-bit integer

typedef signed Char int8_t;
typedef short INT16_T;
typedef int int32_t;
typedef long Long int64_t;
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
typedef unsigned long long uint64_t;

Large end: High byte placed at storage address low (address 0 lowest), low byte high end (different end, each byte internal order is the same, different bytes in memory, disk in the order of different)

Network byte sequence to native byte order: Ntohl,ntohs

Native byte-order-to-network byte order: htonl,htons

Floating-point numbers have sign bits, and float represents a limited number of bits, with a 23-bit mantissa representing 7-bit decimal digits, a more accurate intermediate value such as long double, and a double (exact 16-bit), with floating-point numbers that focus on the accumulation of end errors.

Ii. implicit conversion rules for data types

Implicit conversion is the assignment to the right of the calculation process temporary changes to the second binary code interpretation rules, do not change the binary code will not fundamentally change the interpretation rules, C-language default rules for the numerical representation of the small range of data conversion to large. The last assignment result data length and interpretation is determined by the = left data type.

1, signed number unsigned number at the same time in an expression, unsigned conversion to signed, that is, the complement of negative numbers interpreted as unsigned numbers, often a large number of positive, which in the logical expression and subtraction operations prone to errors, unless it is not related to the numerical calculation, the other less use unsigned number

2, are signed number of direct expansion, no sign of the same

Truncation and expansion of shaping number

1, the value of the left small less right operand data length, the left can only intercept the right side of the low, explain the same way, so negative numbers may become positive

2, the positive number multiplied after the overflow truncation retains the low, the length is the left type

2, the value is unchanged after expansion

Iii. type Conversion

Basic conversion: Char,short operation is converted Int,float to double, done basic conversion if the operator ends up with data types that are still inconsistent, the compiler does an automatic type conversion

C language does not stipulate that Char must be signed, so use must write the name signed or unsigned,ascii characters highest bit 0

Automatic type conversion: Narrow variable width (similar unsigned symbol); Large turn small missing decimal between floating-point numbers, floating point to integer analysis, inverse ok

Coercion of type conversions is also temporary

---restore content ends---

C-language numerical representation and calculation

Related Article

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.