c language, the usefulness and use of commas

Updated on vogue 2024-05-02
18 answers
  1. Anonymous users2024-02-08

    There are several ways to use a comma:

    The first is a separator, which is used to separate the parts of a statement, such as inti, j, k, etc., and the comma indicates that the statement is not finished.

    The second is used for expressions, such as if(a==1,b<2,c>3) where the value of the if statement is the result of c>3, although the first two are also involved in the operation, but its result has no effect in the if expression, only the result of c>3 is 0, the expression is 0, the result of c>3 is 1, and the expression bracket of if is 1

  2. Anonymous users2024-02-07

    The C language provides a special put in operator --- comma operator.

    Expression 1, Expression 2

    The process of solving comma expressions is to find expression 1 first, and then expression 2The value of the entire comma expression is the value of expression 2, for example, the value of the expression "3+5,6+8" is 14.

    For example, in the comma expression: "a=3*5,a*4", the assignment operator has a higher priority than the comma operator, so a=3*5After calculation and assignment, the value of a is 15, and then a*4 is solved, and 60

    The value of the entire comma expression is 60

    The general form of a comma expression can be extended to:

    Expression 1, Expression 2, Expression 3,。。 Expression n. Its value is the value of the expression n.

    The comma operator is the lowest level of all operators.

  3. Anonymous users2024-02-06

    1. When the sequential point is used, the combination order is from left to right, which is used for sequential evaluation, and the value of the whole expression is the value of the last expression after completion.

    main()

    Calculate the values in parentheses: s+2=4, d+4=7;The parentheses should be (4,7), and the value in the parentheses should only be the last one, and if there are no parentheses, the first one should be taken; a=12+7=19。

    x=(y=3,(z = y+2) +5);

    It is to first assign y to 3, increment y to 4, then add 4 to 2, assign result 6 to z, then add z to 5 and finally assign x to the result value of 11.

    2. Note: The comma operator ( is the operator with the lowest priority among the C language operators.

  4. Anonymous users2024-02-05

    The C language provides a special put in operator --- comma operator.

    Expression 1, Expression 2

    The process of solving a comma expression is:

    Find Expression 1 first, then Expression 2

    The value of the entire comma expression is the value of expression 2, for example, the value of the expression "3+5,6+8" is 14. For example, in the comma expression: "a=3*5,a*4", the assignment operator has a higher priority than the comma operator, so a=3*5After calculation and assignment, the value of a is 15, and then a*4 is solved, and 60 is obtained, and the value of the entire comma expression is 60.

  5. Anonymous users2024-02-04

    In the C language, a comma (,) can also be an operator, called a comma operator. A comma operator can concatenate two or more expressions into a single expression, called a comma expression. Its general form is:

    Sub-expression 1, Sub-expression 2, .,The sub-expressions n are e.g. a + b, c = b, c++

    The comma operator has the lowest priority of all operators and is usually used in conjunction with the for cycle. The value of the rightmost sub-expression of the comma expression is the value of the comma expression. In the example above, the value of C++ (the value before C self-incrementing) is the value of the expression.

    The comma operator ensures that the left subexpression is evaluated only after the right subexpression is completed. That is, the comma operator is a sequence of points whose subexpressions to the right are only evaluated after all *** to the left of it has ended. Therefore, in the above example, c gets the value of b before performing the auto-increment operation.

  6. Anonymous users2024-02-03

    A comma operator is usually associated with a set of expressions.

    The result of its operation is 6

    In other words, no matter how many expressions the comma algorithm operates on, the final result it returns must be the result of the last expression.

  7. Anonymous users2024-02-02

    First of all, it is important to understand that the comma operation is a multivariate operation, and its entire value is the value of the last expression, and the value in this case is calculated from left to right.

    In the above program, the value of x=a+b+z is first found, and the value of z is not assigned at all, so the result will not appear x, y is equal to 4! That is, there is something wrong with your program.

    If it is changed to y=(z=a+b), (x=a+b+z) according to the above operation order, the result is x=12, y=12;

    You can just take a look at it!

  8. Anonymous users2024-02-01

    Comma expressions serve two purposes.

    Statements before and after the comma will run to, for example, (u=5, k=0), after execution, the value of u is 5, and the value of k is 0

    The value of the whole expression = the value of the last sentence = (k=0) = 0, that is, the value of f is 0

  9. Anonymous users2024-01-31

    0 The value of the expression of the comma operator is the value of the last expression, and the last one is k=0;

    Actually, it's 0

  10. Anonymous users2024-01-30

    Wait.. I got it wrong. It is also necessary to understand that Shenma is a comma expression.

    I said the same thing as uuyyhhjj and delta charlie, but we're all getting it wrong. You can run all of our examples yourself and see if that's the case. I feel that the following should be my correct understanding.

    The comma expression has the lowest priority of all the operators, and is lower than the assignment operator. What it does is count each expression and use the value returned by the last expression as the value of the comma expression. (Leave nothing else behind).

    Like the one you said, if yes.

    x=200,500;

    without parentheses), x would be equal to 200, and then the value of the whole equation above would be 500.

    But with parentheses, (200,500) is a separate formula, and its value is 500, so x=(200,500) is 500 at the end of the assignment to x

    To give a few examples, x=(21,22,23,24,25);

    In this one, count the parentheses first, and the first 21 to 24 are discarded after calculation, and the value of the expression in the parentheses is 25. After that, the assignment operation is performed, and the value of x becomes the value of the expression in parentheses, 25.

    If compiled. int x;

    int a;

    x=(21,22,23,24,25);

    cout<<"x="<

    If you have any questions, please feel free to ask.

  11. Anonymous users2024-01-29

    It must be 500, and the comma operator only takes the value of the last expression as the value of the whole statement.

  12. Anonymous users2024-01-28

    The comma operator takes the value of the last item as the value of the entire comma expression.

  13. Anonymous users2024-01-27

    inta=3,b=4,c=5;

    c=(a+=a-=a), here two separate sentences (b=a,c+2);

    a+=a-=aThis one runs from right to left.

    a-=aa=0a+=a

    a=0c=a

    c=0, so c=0

    b=ab=0

    C+2c is unchanged.

    No, it is necessary to enclose the last one in parentheses.

    For example, c=((a+=a-=a),(b=a,c+2));

    This is the one that takes the latter.

  14. Anonymous users2024-01-26

    Your question seems to be written in the wrong place, it should be a=(

    b,c--,d+3), this is the section of "Comma Operators and Expressions", "The comma operator is calculated from left to right, and the value of the whole comma expression is the value of the rightmost expression".

    Inside the parentheses is a comma expression, and a takes the value of the comma expression. So the answer is a=8

    It's very professional, oh, give it to adopt.

  15. Anonymous users2024-01-25

    A formula consisting of a comma operator is also an expression, and its value is equal to the value of the rightmost expression.

  16. Anonymous users2024-01-24

    In type C, the comma can be used as an operation expression.

    For example, x=(x=1,x=x+1,x+2) is calculated from left to right, which is equivalent to assigning x to 1 first, then calculating x+1=1+1=2, and finally 2+2=4, which returns 4

    And the book you read probably doesn't go into much detail at all.

    When x=(200,500,600), x is 200 first, then 500, and finally 600, because there is no assignment operation, so 200 and 500 are overwritten.

  17. Anonymous users2024-01-23

    For reference, a comma can be understood in such a way that a comma indicates a kind of order.

    It will be clearer to compare the following two expressions:

    x = x + 10, x * 3;

    x = (x + 10, x * 3);

    Suppose the initial value of x is 10

    For the first expression, it is equivalent to:

    x = x +10;

    x = x * 3;

    In this case, x is (10 + 10) *3 = 60

    For the second expression, x + 10, x * 3) is equivalent to:

    x + 10

    x * 3 so its return value is x * 3, that is, 10 * 3 = 30, so x is 30 at this time

  18. Anonymous users2024-01-22

    The comma operator ( is the lowest priority of the C operators, and is used for sequential evaluation from left to right (the value of the expression after the last comma is used as the value of the entire expression).

    Here's an example of how to use the comma operator:

    int a=3, b=5, c;c = a>b, a+b;After running, the value of c is 0, because the comma operator has a lower priority than the assignment operator, so the result of a>b (which is 0) is assigned to c first, and then the result is calculated a+b (the result is not saved), so c=0c = (a>b, a+b); After running, the value of c is 8, because the priority of the parentheses is higher than the assignment operator, so the expression in the parentheses is calculated first, and the result is the value of the last expression, that is, the value of a+b, so c=8

Related questions
8 answers2024-05-02

Installation:1The installation of the anti-rat plate is very simple, first insert the expansion nail into the installation eye on the card slot, and then fix the expansion nail parallel to the wall on both sides of the door frame along the door frame, and the card slot is successfully installed! When using, you can put the anti-rat plate smooth. >>>More

6 answers2024-05-02

Essential oils can purify the air, remove dust and sterilize. At the same time, the scent can make people feel good. Aromatherapy essential oil - add 2-3 drops of sesame oil to the top groove of the incense burner and the hanging product. >>>More

5 answers2024-05-02

impsystem/manager

file=bible_db >>>More

2 answers2024-05-02

How to use] First of all, do a good job of cleaning and moisturizing, and apply a makeup primer or base cream with a better moisturizing effect before applying foundation. Then gently invert the bottle to make the powder uniform and closer to the bottle mouth, then take out the brush and gently rotate the brush head to evenly absorb the mineral powder. After gently shaking the brush head to remove excess powder, first dot the mineral powder on the forehead, cheeks, and jaw, and then swipe it in a circular motion. >>>More

9 answers2024-05-02

The question you ask is very inexhaustible...