Attention problems of C-language operators

Source: Internet
Author: User

//For the operation of the self-increment and decrement operator is worth studying, be careful to be pits.
1#include <stdio.h>2 intMainvoid){3 intI=5, j=5, P,q;4P= (i++) + (i++) + (i++);5Q= (++J) + (++J) + (+ +)j);6printf"p=%d,q=%d,i=%d,j=%d", p,q,i,j);7 return 0;8}

The results of his operation:

Q= (++J) + (++J) + (++J) is supposed to be 21 Ah!

Original: For q= (++J) + (++J) + (++J), first calculate (++j) + (++J), because is "pre-plus", to calculate two times ++j, at this time j=7, then add, equivalent to 7+7, the result is 14; then the 14+ (++J), the equivalent of 14+8, the result is 22.

This is the Fedora Platform feature, other platforms can self-test.

Type conversions

If the data types are different on either side of the assignment operator, the system will automatically convert the type to the right of the assignment number to the left type. Specific provisions are as follows:

    • The real type gives the integer type, rounding off the fractional part.
    • The integer is given a true type, the value is constant, but it is stored as a floating point, which increases the fractional part (the value of the decimal portion is 0).
    • The character type is given an integer, because the character type is one byte, and the integer type is two bytes, so the ASCII value of the character is placed in the low eight bits of the integer, and the height eight bits is 0. The integer type is given a character, and only the lower eight bits are given the character amount.
Compound assignment operators

The addition of the two-mesh operator before the assignment "=" can form a compound assignment. such as + =,-=, *=,/=,%=, <<=, >>=, &=, ^=, |=.

Attention problems of C-language operators

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.