Conversion of C + + middle number

Source: Internet
Author: User

Question: d=1,-d==?

Let's see what the answer will look like:

-----------------------------

What is the output of the following code?

int main () {

Char dt = ' \1 ';

Long TDT;

TDT =-dt;

printf ("%ld\n", TDT);

}

My first reaction was that this output should be "-1". I think you think so, too. But what if the output is on a 64-bit system? I hope it is also "-1". Let's see if this is the case.

Let's test it.

#xlc-q64 a.c-qlanglvl=extended

#./a.out

4294967295

!! The output here is "4294967295" instead of "1".

Don't worry, it's possible we missed something.

Before answering the previous question, let's look at the following code snippet:

"Char dt = ' \1 ';" -dt; "

For this code snippet, can we confirm that the DT variable is signed or unsigned? C

Let's look at what the C language standard says:

The implementation shall define char to have the same range, representation, and behavior as either signed char or Unsigne D Char.

This means that the standard does not require symbols for char type variables. We're looking at the XL compiler's documentation Http://publib.boulder.ibm.com/infocenter/comphelp/v111v131/topic/com.ibm.xlc111.aix.doc/language _ Ref/ch.html

Inside said:

By default, char behaves like a unsigned char. To change this default, can use The-qchars option or the #pragma chars directive. See-qchars for more information.

It appears that in the XL compiler, char defaults to unsigned. We can use-qchars to change this default behavior. Let's test this out:

#xlc-q64 a.c-qlanglvl=extended-qchar=signed

#./a.out

-1

That's great. We seem to have understood.

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.