• C Operators


    COMMON OPERATORS IN C 

    Unary Operators:    take only one argument

        e.g.    unary -, unary +, ++, --, !
                 (-2), (+2), a++, a--, !done

    Binary Operators:   take two arguments

        e.g.    binary -, binary +, *, /, %, =, +=, -=, *=, /=, %=
                  (a - 2), (a + 2), (a * 2), (a / 2), (a % 2), (a = 2), (a += 2) ...

    Assignment Statements:    Used to set (or "assign") a value to a variable

        Examples:

                x = y;           //  The value of the variable x now has the same value as the variable y

                x = x + 1;     //  The value of the variable x has been increased by one

                x = x * x;     //    The value of the variable x has been set to the value of x squared

                x = x - 1;     //    The value of the variable x has been decreased by one

                x = x / 2;     //    The value of the variable x has been divided by 2
                                    //    **  please note:  the resulting value of this operation will depend
                                    //                              on the type of the variable x!
                                    //          If x is an integer, the operator  /  will truncate the result.

                x = x % 2;   //    The  % operator is not valid if either number is of type double/float.
                                     //    Assuming x is of type int (or char) the value of the variable x
                                     //              has been set to the remainder of x / 2

           Special Assignment Operators:      Combine Arithmetic with Assignment

                x += 2;        //    this statement is equivalent to:   x = x + 2;

                x -= 2;         //    x = x - 2;

                x *= 2;        //    x = x * 2;

                x /= 2;         //    x = x / 2;

                x %= 2;      //    x = x % 2;        (not valid for type float or double)
     

            Increment & Decrement Operators:    Increase or Decrease a value by one
                    programming example:
                            void myfunction()
                            {
                                int a = 2, b = 1;   // declare and initialize variables a and b
                                a++;                  //  a now equals 3
                                b--;                  //  b now equals 0
                             }

            Boolean Operators:    Evaluate expressions as true or false
                    A boolean expression may be simple or complex, but it always
                    evaluates to one of two values:  true, or false.

                    Examples:        y < z        // if y < z, the value will be true
                                                             // else, the value will be false

                                              y<=z        // if y is less than OR equal to z
                                                              // the value will be true, else false

                                              !(y>z)        // if y if NOT greater than z, this
                                                                // value will be true, else false.
                                                                // equivalent to y <= z above.

                                              !done     // if the variable "done" = zero,
                                                            // then this expression will be true

                                              a == b    // if a and b are equal, the value will be true
                                                            //  else the value will be false

                   ** Please Note!  The comparison operator for "equal to" is
                                                  '==' (two equal signs), NOT '=' alone

                    Combining boolean expressions using && and | |

                            Expressions can be made more complex by use of && and | |

                            &&     =>    logical and operator (true only if both expressions are true)
                            | |        =>    logical or operator (true if either one of the expressions is true)

                            Examples:    (a < 5) && (b > 2)    // this boolean expression will evaluate
                                                           // to true only if BOTH a < 5 is true, AND b > 2 is true

                                                  (a < 5) | | (b > 2)    // this boolean expression will evaluate
                                                                                // to true if EITHER a < 5 OR if b > 2

    Bitwise Operators:  Binary operators used for bit manipulation

        e.g.    &        bitwise AND    (e.g.   n = n & 0177;  // sets lower 7 bits to 1, all others to 0)
                   |         bitwise OR        (e.g.   x = x | SET_ON;  // sets x to same bits as SET_ON)
                   ^        bitwise XOR
                  <<        left shift (multiply)
                  >>        right shift (divide)
                   ~        one's complement (unary)

    Operator Precedence:    When an expression has more than one operator,
                                             C has to determine the order in which to execute them.
                                             This is called the "precedence."

            Precedence of some basic operators:

                           ++, --,  !, (unary -), (unary +)    //  increment, decremement, not, unary ops
                           *, /, %                            //  multiplication, division, modulo
                           +, -                                 //  addition and subtraction 
                            <<  >>                           //  shift left, shift right
                        <, <=, >, >=                      //  greater than, greater than or equal to, etc.
                        ==, !=                               //  equal, not equal 

                        &                                      // bitwise and
                         ^                                      // bitwise xor
                         |                                        // bitwise or
                        &&                                  //  and
                        | |                                      //  or
                        =, +=, -=, *=, /=, %=      //   combined arithmetic/assignment operators

      
            Parenthetical expressions "trump" other precedence

               e.g.    (a + b) * (c - d)    will cause the expressions inside the parentheses
                           to be evaluated before multiplying the two values together

                           a + b * c - d    would create an entirely different result, because
                                                   by default, b * c would be executed first.

    control statements - switch

    Switch case statements are a substitute for long if statements that compare a variable to several integral values

    Syntax:

    switch (n)
    {
        case 1: // code to be executed if n = 1;
            break;
        case 2: // code to be executed if n = 2;
            break;
        default: // code to be executed if n doesn't match any cases
    }

    Example:

    // Following is a simple C program 
    // to demonstrate syntax of switch. 
    #include <stdio.h> 
    int main() 
    { 
    int x = 2; 
    switch (x) 
    { 
        case 1: printf("Choice is 1"); 
                break; 
        case 2: printf("Choice is 2"); 
                    break; 
        case 3: printf("Choice is 3"); 
                break; 
        default: printf("Choice other than 1, 2 and 3"); 
                    break; 
    } 
    return 0; 
    } 
  • 相关阅读:
    维护gcd的线段树 补发一波。。。
    BZOJ 4720: [Noip2016]换教室
    P2184 贪婪大陆 树状数组
    BZOJ 1047: [HAOI2007]理想的正方形 单调队列瞎搞
    POJ3280 Cheapest Palindrome 区间DP
    BZOJ 2288: 【POJ Challenge】生日礼物 堆&&链表
    BZOJ 4236: JOIOJI map瞎搞
    浅谈最近公共祖先(LCA)
    题解 BZOJ 1912 && luogu P3629 [APIO2010]巡逻 (树的直径)
    [笔记] 求树的直径
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/12898119.html
Copyright © 2020-2023  润新知