码迷,mamicode.com
首页 > 其他好文 > 详细

C Operators

时间:2020-05-16 09:23:53      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:dep   tor   truncate   exp   fir   div   variable   with as   exec   

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; 
} 

 

C Operators

标签:dep   tor   truncate   exp   fir   div   variable   with as   exec   

原文地址:https://www.cnblogs.com/JasperZhao/p/12898119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!