Write high-quality C code one: data

Source: Internet
Author: User

(1) Note the data type and its scope

The representations and scopes of different data types are different, and the integer type is as follows:

Note that there is a difference between the number of symbols and the number of unsigned, the highest bit of the symbol is sacrificed as a sign bit, the sign bit is 1 for negative numbers, and the sign bit is 0 for positive numbers. In essence, we interpret the data in memory according to his data type. For example, 3 in-memory complement (assuming 8-bit) is represented as 1000 0011, and if definition-3 is unsigned, then the interpreted value is 131. So sometimes something that seems "strange" can happen. Give two examples:


The representation range of Char is -127~127, where char a=150 is defined, and the compiler is processed by the number of symbols, so the final result is negative.

(2) Note the conversion of different data types

Let's look at an example of an exception:

Supposedly, the final result should be I < sizeof (array) ah?
So it is, sizeof. The return value is a size_t type and is an unsigned form. When a signed number and an unsigned number are operational, the signed number is converted to an unsigned number. In this way, 1 is a very large positive number if it is represented by unsigned numbers. So it will come out "seemingly strange" results.

Thus, it is dangerous to mix signed and unsigned numbers, so you should use as little of the unsigned type as possible in your code.

(3) Prevent unsigned integer wrapping

Calculations involving unsigned numbers are never overrun, that is, if the value exceeds the maximum limit of the unsigned number, a wrap occurs. If the unsigned number exceeds the maximum, it returns 0, then increases from 0, and if it falls below the lower limit of the unsigned number, the upper bound of the unsigned number is reached and then decreases from the upper bound. As an example:

Therefore, in the operation of the unsigned number, the appropriate method should be taken to prevent wrapping. For example, make the following changes:

#include<stdio.h>int main(){    unsignedint4294967295;    unsignedint2;    unsignedint4;    if(UINT_MAX - a > b)        printf("%u\n",a+b);    if(UINT_MIN + c < b)        printf("%u\n",b-c);    return0;}
(4) Prevent sign integer overflow

When the two symbol count operation overflows, it causes "indeterminate behavior," and most compilers ignore the overflow, which causes indeterminate or incorrect values to be saved in the variable. Overflow is similar to wrapping, which makes the value of the variable "unexpected" changes, can escape the boundary check, causing many problems, such as buffer overflow.

(5) Use less floating-point numbers

Floating-point numbers follow the IEEE754 standard for most platforms. The implementation of floating-point types in different platforms varies greatly, some platforms have floating-point units (FPU), some processors can only do integer operations, you need to use integer operations to achieve analog floating point arithmetic (soft floating point).
5.1 avoids the accurate calculation of floating-point numbers, accurately represents floating-point numbers with fractions, and avoids imprecise problems caused by rounding errors of floating-point numbers.
5.2 Avoid floating point number directly using = = to judge, general use | V0-v1 < min| form.
5.3 Avoid using floating-point numbers as loop counters.
5.4 try to convert integers in floating-point operations to floating-point numbers. Look at an example:

Why is the first result different from the one in the back four? And the first result is obviously not very precise. Because X/9 will execute first, and then convert the result 35 to a floating-point number assigned to a. It is also important to note that integers and floating-point operators convert integers to floating-point numbers first. Therefore, to avoid this lack of information, when we use integer arithmetic and assign the result to a floating-point number, we first convert an integer to a floating-point number!

(6) Scope check when data type conversion

Conversions of different types of data are first converted to the highest-level data type before the operation is performed. For example, int type data and long type data operations, int is converted to long first. It is also important to note that all floating-point operations are double-precision, even if only the float type in the expression is converted to a double type first.
If an unsigned type is converted to a signed type, it is possible to have high truncation and data loss, mainly to prevent overflow.

(7) Use a strictly defined data type

First look at two examples:


Why is that? Since CH is compared with 0xFF, CH is first converted to int, if the int is 32 bits, then if CH is signed, then the complement is 0xFFFF FFFF, if ch is unsigned, then 0x0000 00ff is required.
Use strictly-defined, portable data types as much as possible, without using variables that are closely related to specific hardware or software.

Write high-quality C code one: data

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.