Computer Programming and Problem Solving
Computer Programming and Problem Solving
Computer Programming and Problem Solving
Lecture - 3
The scanf() function is the output method The scanf format string holds placeholder or format specifier representing the type of value that will be entered by the user. These placeholders are exactly the same as the printf() function like %d for integerss, %f for floats, and %lf for long doubles. The scanf() function requires the memory address of the variable to which we want to save the input value
P.Thendral-CPPS-Lecture 3 3
& - Address of Operator scanf(%d%d%d,&a,&b,&c); &a - address of variable a &b - address of variable b &c - address of variable c
P.Thendral-CPPS-Lecture 3 5
P.Thendral-CPPS-Lecture 3
Expressions
An expression is a combination of constants, variables, and operators that are used to denote computations. For instance, the following: (2 + 3) * 10 is an expression that adds 2 and 3 first, and then multiplies the result of the addition by 10. (The final result of the expression is 50.)
P.Thendral-CPPS-Lecture 3 7
Expressions
P.Thendral-CPPS-Lecture 3
Assignment Operator
The = operator is called an assignment operator The general statement form to use an assignment operator is left-hand-operand = right-hand-operand; The value of the right-hand-operand to be assigned (or written) to the memory location of the left-hand-operand. Thus, after the assignment, left-hand-operand will be equal to the value of right-hand-operand. For example, the statement a = 5; writes the value of the right-hand operand (5) into the memory location of the integer variable a (which is the left-hand operand in this case).
P.Thendral-CPPS-Lecture 3
Assignment Operator
Similarly, the statement b = a = 5; assigns 5 to the integer variable a first, and then to the integer variable b. After the execution of the statement, 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 to left; therefore the value on the left must be some form of a variable that can receive the data from the expression on the right.
P.Thendral-CPPS-Lecture 3
10
Arithmetic Operators
P.Thendral-CPPS-Lecture 3
11
P.Thendral-CPPS-Lecture 3
12
Arithmetic Expressions
Uses Arithmetic operators Order of Evaluation from Left to Right Follows Precedence Rules of the arithmetic operators
P.Thendral-CPPS-Lecture 3
13
P.Thendral-CPPS-Lecture 3
14