Value retention principle

Source: Internet
Author: User

The ASNI C standard rule takes the value preserving principle, that is, when several integer operands are mixed, the result type may be either a conforming number or an unsigned number, depending on the relative size of the operand's type.

#include <stdio.h>

#define Size_of_array (arr) (sizeof (ARR)/sizeof (arr[0))

int main () {

int d =-1;

int a[] = {1, 2, 3, 4, 5, 6, 7};

if (D<size_of_array (a)) {

printf ("1\n");

} else {

printf (" -1\n");

}

return 0;

}

The execution results are as follows: 1

The type of the value defined by Size_of_array is the unsigned int type (the type that sizeof returns is an unsigned type), and the IF statement tests equality between signed int and unsigned int, so d is promoted to unsigned int Type. 1 is a large value when converted to unsigned int type, so the expression is false.

If you cast the return result of Size_of_array to int, the result will be as follows because of the same type:

if (d< (int) (Size_of_array (a)))

The execution results are as follows: 1

Value retention principle

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.