Programmer's---C language details 20 (symbolic and signed conversion, two number addition overflow after the 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;                       This statement outputs 3.14:float j = 3.14 + (1E20-1E20) because it represents a finite output result of 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 unsigned int is the same as the complement of-1 (that is, unsigned umax and-1 of the complement of the same bit) printf ("\ n");//If a signed number is mapped to its corresponding unsigned number, negative numbers are converted to large positive numbers, non-negative numbers remain unchanged/* Test converted */short int  v = -12345;unsigned Short UV = (unsigned short) v; The cast changes the value. But do not change the bit representation (i.e. 12345 and unsigned 53,191 bits as) 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: A number in the expression is signed and there is an unsigned, C languageThe default conversion to unsigned. There will be problems 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:


Programmer's---C language details 20 (symbolic and signed conversion, two number addition overflow after the 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.