• 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; 
    } 
  • 相关阅读:
    使用pycharm调用模块后字体变灰
    spring注解驱动开发
    spring注解驱动开发
    spring注解驱动开发
    Centos6.5 防火墙开放端口
    fastJson一些补充
    springboot使用hibernate validator校验
    swagger的使用
    rabbitmq的vhost与用户管理
    解决mysql中只能通过localhost访问不能通过ip访问的问题
  • 原文地址:https://www.cnblogs.com/JasperZhao/p/12898119.html
Copyright © 2020-2023  润新知