A simple numerical comparison Problem

Source: Internet
Author: User

# Include <stdio. h>

Int main (void)
{

Int I =   0 xaabbccdd ;

Char * P = (Char * ) & I;

If (P [ 0 ] =   0xdd )
Printf ( " Equal " );
Else
Printf ( " Not equal " );
Return   0 ;
}

 
ProgramThe intention is to output equal when running on a little-endian machine (Intel), but output "not equal" and the expected idea after running.

Where is the problem? Type escalation rules in incorrect C.

In the IF (P [0] = 0xdd) Statement, the right operand of the operator "=" is an integer constant, and the default type is int. The left operand is a char type, therefore, the type from Char to int needs to be upgraded. Since the value of P [0] Is 0xdd (the symbol bit is 1), according to the extension rules of the symbol bit when the data width is extended, P [0] is extended to (INT) 0 xffffffdd. It is not equal to (INT) 0xdd, so "not equal" is output ".

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.