Comma expression, comma

Source: Internet
Author: User

Comma expression, comma

What are the running results of this program? 0? See the following
 
Yes, it's not 0, it's 1!
You may wonder why 1 is not 0? We will not talk about this for the moment.
P = a [0] = & a [0] [0] Everyone knows this, so p [0] represents * (p + 0) that is, * (& a [0] [0]). From the inverse operation between * and &, p [0] is a [0] [0].
So there is a problem! What is a [0] [0?
Many may think that a [0] [0] is 0, but it is not. In this case
A [0] [0] is indeed 0. You can observe the slight differences between the codes at both ends. That is, the difference between () and.
When initializing a two-dimensional array, we use {} to aggregate the elements of the same row. {} is used to divide the region, and each {} is an element of a row, in addition, {} is not an operator. () Is a priority-level operator, but it cannot divide each line of elements when initializing a two-dimensional array. That is to say...int a [3] [2] = {(0), (1), (2)}; and. int a [3] [2] = {0, 1, 2}; is completely equivalent ......
But... what is the comma !?
Speaking of this, we have to mention a very easy to ignore operator. It is called the comma operator, also known as the order evaluation operator. Among all operators, the priority is the lowest (Level 15th ). An expression composed of commas (,) is also called a comma (,), in the form
Expression 1, expression 2 ,......, Expression n
When used, the values of Expressions 1, 2, 3,..., and n are obtained in sequence, and the value of expression n is taken as the value of the entire comma expression.

After reading this, you will surely understand why Exercise 4 has a result of 1 instead of 0. In fact
It can also be written as int a [3] [2] = {1, 3, 5 };
Only values 1, 3, 5 are assigned to a [0] [0], a [0] [1], and a [1] [0], and the values of other elements are 0. So the output value of a [0] [0] is of course 1 ~

An additional question is provided:

Main () {int x, y, z; x = y = 1; z = x ++, y ++, ++ y; printf ("% d, % d, % d \ n ", x, y, z );

}

The output result of the above program is:

The correct answer should be 2, 3, 1

Related Article

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.