CP0372_29-Jul-2011_RM02

download CP0372_29-Jul-2011_RM02

of 14

Transcript of CP0372_29-Jul-2011_RM02

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    1/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 11

    Computer Programming andComputer Programming and

    Problem SolvingProblem Solving

    LectureLecture -- 33

    P.THENDRALP.THENDRALAssist.Prof.SeniorAssist.Prof.Senior

    SCSESCSE

    SJT 511SJT 511 [email protected]@vit.ac.in

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    2/14

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    3/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 33

    Input UsingInput Using ScanfScanf The scanf() function is the output method

    The scanf format stringholds placeholder orformat specifierrepresenting the type of valuethat will be entered by the user.

    These placeholders are exactly the same as theprintf() function like %d for integerss, %f forfloats, and %lf for long doubles.

    The scanf() function requires the memoryaddress of the variable to which we want to savethe input value

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    4/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 44

    UsingUsing scanfscanf() function() function#include

    int main(void){

    int a;

    printf("Please input an integer value: ");scanf("%d", &a);}

    "Read in an integer from the user and store it atthe address of variable a".

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    5/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 55

    scanfscanf() uses & operator() uses & operator scanfscanf() uses() uses

    && -- Address of OperatorAddress of Operator

    scanf(scanf(%d%d%d%d%d%d,&a,&b,&c,&a,&b,&c););

    &a&a -- address of variable aaddress of variable a

    &b&b -- address of variable baddress of variable b

    &c&c -- address of variable caddress of variable c

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    6/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 66

    ExpressionsExpressions

    ANDANDOperatorsOperators

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    7/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 77

    ExpressionsExpressions

    An expressionis a combination of constants,variables, and operators that are used to denotecomputations.

    For instance, the following:

    (2 + 3) * 10 is an expression that adds 2 and

    3 first, and then multiplies the result of theaddition by 10. (The final result of the expressionis 50.)

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    8/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 88

    ExpressionsExpressions

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    9/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 99

    Assignment OperatorAssignment Operator The = operator is called an assignment operator

    The general statement form to use an assignment operatoris left-hand-operand = right-hand-operand;

    The value of the right-hand-operand to be assigned (orwritten) to the memory location of the left-hand-operand.Thus, after the assignment, left-hand-operand will be equalto the value of right-hand-operand.

    For example, the statement a = 5; writes the value of theright-hand operand (5) into the memory location of the

    integer variable a (which is the left-hand operand in thiscase).

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    10/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 1010

    Assignment OperatorAssignment Operator Similarly, the statement b = a = 5; assigns 5 to

    the integer variable a first, and then to theinteger variable b. After the execution of thestatement, both a and b contain the value of 5.

    An expression such as 6 = a, is actually

    backwards and will not work.

    The = operator always works from right toleft; therefore the value on the left must be

    some form of a variable that can receive the datafrom the expression on the right.

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    11/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 1111

    Arithmetic OperatorsArithmetic Operators

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    12/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 1212

    Precedence Rule in ArithmeticPrecedence Rule in Arithmetic

    OperatorOperator

    Multiplication, Division and RemainderMultiplication, Division and Remainder

    operators have higher precedence thanoperators have higher precedence thanthe addition and subtraction operatorsthe addition and subtraction operators

    5 + 3 * 2 will be giving the result 11 not5 + 3 * 2 will be giving the result 11 not

    1010

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    13/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 1313

    Arithmetic ExpressionsArithmetic Expressions

    Uses Arithmetic operatorsUses Arithmetic operators

    Order of Evaluation from Left to RightOrder of Evaluation from Left to Right

    Follows Precedence Rules of theFollows Precedence Rules of the

    arithmetic operatorsarithmetic operators

  • 8/3/2019 CP0372_29-Jul-2011_RM02

    14/14

    P.ThendralP.Thendral--CPPSCPPS--Lecture 3Lecture 3 1414

    Arithmetic Assignment OperatorsArithmetic Assignment Operators