Program Ape's---C language details 20 (symbolic and signed conversion, two number addition overflow after numerical calculation)

Source: Internet
Author: User

Main content: Unsigned and signed conversions, two number addition overflow after the numerical calculation

#include <stdio.h>/* 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];p rintf ("a[%d] =%f \ n", I,a[i]);} return result;}  int main () {int i = 200 * 300 * 400 * 500; int represents about 2 billion, unsigned on 4 billion float J = (3.14 + 1e20) -1e20;                       The output of this statement is 3.14:float j = 3.14 + (1E20-1E20) because the representation of the precision is limited to 0;    e represents a low exponent of 10 long int a = 1;       A long long int b = 10;  printf ("i =%d\n", i);p rintf ("j =%f\n", j);p rintf ("a =%ld\n", a);p rintf ("b =%ld\n", b);p rintf ("\ n"); */test complement */  unsigned int u = 4294967295u;int tu = (int) u;printf ("U =%u, tu =%d\n", u, tu); The maximum value of the unsigned int and the complement of-1 are the same (i.e. unsigned umax and-1 of the complement of the same bit representation) printf ("\ n");//When a signed number is mapped to its corresponding unsigned number, negative numbers are converted to large positive numbers, non-negative numbers remain unchanged/* Test conversion */short int  v = -12345;unsigned Short UV = (unsigned short) v; The cast changes the numeric value, but does not change the bit representation (that is, 12345 and unsigned 53,191 bits are the same) printf ("V =%d, UV =%u\n", V, UV);  printf ("\ n"); float c[3];sum_elements (c,3); Error passing 0 o'clock with unsigned and signed conversion problems/* Summary: One number in an expression is signed another is unsigned, C languageThe default conversion is unsigned, there is a problem when comparing -1<0u *//* overflow calculation, the formula see */short a1 = -65536;short B1 = -1;printf ("\ n");p rintf ("%d\n", A1+B1); return 0;}

Overflow formula:

Output:


Program Ape's---C language details 20 (symbolic and signed conversion, two number addition overflow after numerical calculation)

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.