Programmer --- C language details 20 (Conversion between symbols and symbols, and calculation after two numbers overflow)

Source: Internet
Author: User

Programmer --- C language details 20 (Conversion between symbols and symbols, and calculation after two numbers overflow)
Main Content: unsigned and converted between symbols, numeric calculation after two numbers Overflow

# Include
 
  
/* This function has a potential vulnerability */float sum_elements (float a [], unsigned length) {int I; float result = 0; for (I = 0; I <= length-1; I ++) {result + = a [I]; printf ("a [% d] = % f \ n", I, a [I]);} return result;} int main () {int I = 200*300*400*500; // int indicates about 2 billion, unsigned, about 4 billion float j = (3.14 + 1e20)-1e20; // The output result is 0 due to limited precision, and the output of this statement is 3.14: float j = 3.14 + (1e20-1e20); // e indicates that 10 is the low index long int a = 1; long int B = 10; printf ("I = % d \ n", I); printf ("j = % f \ n", j); printf ("a = % ld \ n ", a); printf ("B = % ld \ n", B); printf ("\ n");/* test the completion Code */unsigned int u = 4294967295u; int tu = (int) u; printf ("u = % u, tu = % d \ n", u, tu ); // The maximum value of the unsigned int is the same as that of-1 (that is, the same bit of the unsigned Umax and-1) printf ("\ n "); // when a signed number is mapped to its unsigned number, the negative number is converted to a large positive number, and the non-negative number remains unchanged/* test conversion */short int v =-12345; unsigned short uv = (unsigned short) v; // force conversion changes the value without changing the bit representation (that is,-12345 is the same as the unsigned 53191 bit) printf ("v = % d, uv = % u \ n", v, uv); printf ("\ n"); float c [3]; sum_elements (c, 3); // an error occurs when 0 is passed. There is a problem of unsigned and Signed Conversion./* conclusion: one number of expressions is signed, the other is unsigned, And the C language is converted to unsigned by default, when comparing-1 <0U, there will be a problem * // * for the overflow calculation formula, see */short a1 =-65536; short b1 =-1; printf ("\ n "); printf ("% d \ n", a1 + b1); return 0 ;}
 

Overflow formula:

Output:


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.