How to use the comma and conditional operators in C + + programming to explain the _c language

Source: Internet
Author: User
Tags arithmetic

Comma operator:,
allows grouping of two statements, one of which is expected.

expression, expression

Note
The comma operator has left-to-right affinity. A comma-delimited two expressions are evaluated from left to right. The left operand is always computed, and all side effects are completed before the right-hand operand is computed.
In some contexts, such as the list of function arguments, commas can be used as delimiters. Do not confuse the comma as a delimiter with the use of it as an operator: The two usages are completely different.
Consider an expression

E1, E2

The type and value of the expression are the type and value of the E2, and the calculated results of the E1 are discarded. If the right-hand operand is a left value, the result is a left value.
In scenarios where commas are usually used as delimiters (for example, in arguments in a function or aggregation initializer), the comma operator and its operands must be enclosed in parentheses. For example:

Func_one (x, y + 2, z);
Func_two (x--, y + 2), z);

In the above function call to Func_one, three parameters separated by commas are passed: x, y + 2, and Z. In a function call to Func_two, parentheses force the compiler to interpret the first comma as a sequential computed operator. This function call passes two parameters to the Func_two. The first parameter is the result of a sequential calculation operation (x--, y + 2) with the value and type of the expression Y + 2, and the second parameter is Z.
Example

 Cpp_comma_operator.cpp
#include <stdio.h>
int main () {
 int i = ten, B = c=;
 i = b, C;
 printf ("%i\n", I);

 i = (b, c);
 printf ("%i\n", I);
}


30

Conditional operator:? :
Grammar

Expression? Expression:expression

Note
Conditional operator (?:) is a ternary operator (with three operands). The conditional operators run in the following ways:
The first operand is implicitly converted to bool. Calculate the operand and complete all side effects before continuing.

    • If the first operand evaluates to True (1), the second operand is computed.
    • If the first operand evaluates to False (0), the third operand is computed.
    • The result of the conditional operator is the computed result of the operand, whether the second or third. Only one of the last two operands is evaluated in a conditional expression.
    • Conditional expressions have right-to-left affinity. The first operand must be an integer or a pointer type. The following rules apply to the second and third operands:
    • If two operands are of the same type, the result is the same type.
    • If the two operands are either arithmetic or enumeration types, the common arithmetic transformations (described in the arithmetic transformation) are performed to convert them to a generic type.
    • If all two operands are pointer types, or one is a pointer type, and the other is a constant expression that evaluates to 0, then the pointer conversion is performed to convert them to a generic type.
    • If all two operands are reference types, then a reference conversion is performed to convert them to a common type.
    • If all two operands are of type void, the common type is a void type.
    • If the two operands are the same user-defined type, then the generic type is also that type.
    • If the operands are of different types, and at least one of the operands is a user-defined type, the language rule is used to determine the common type. (See the warning below.) )

Any combination of the second and third operands that are not in the previous list is illegal. The type of the result is a generic type, and if the second and third operands are of the same type and are both left, the result is a left value.

System_caps_warning Warning
If the second and third operand types are different, the complex type conversion rules are invoked as specified in the C + + standard. These transformations can cause unexpected behavior, including the construction and destructor of temporary objects. To this end, we strongly recommend that: (1) Avoid using user-defined types as operands with conditional operators, (2) If you do want to use user-defined types, be sure to explicitly convert each operand to a generic type.

Expre_expressions_with_the_conditional_operator.cpp
//compile with:/EHSC
//Demonstrate Conditional operator
#include <iostream>
using namespace std;
int main () {
 int i = 1, j = 2;
 cout << (i > J i:j) << "is greater." << Endl;

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.