Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

Computer Programming and Problem Solving

Lecture - 3

P.THENDRAL Assist.Prof.Senior SCSE SJT 511 26A thendral.p@vit.ac.in


P.Thendral-CPPS-Lecture 3 1

Using scanf() function


Library : <stdio.h> Syntax : int scanf(const char *format, ...); eg. scanf(%d%f,&intNum, &floatNum); it will return 2, since two expressions are got as input from the user
P.Thendral-CPPS-Lecture 3 2

Input Using Scanf


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

Using scanf() function


#include <stdio.h> int main(void) { int a; printf("Please input an integer value: "); scanf("%d", &a); } "Read in an integer from the user and store it at the address of variable a ".
P.Thendral-CPPS-Lecture 3 4

scanf() uses & operator


scanf() uses

& - 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

Expressions AND Operators

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

Precedence Rule in Arithmetic Operator


Multiplication, Division and Remainder operators have higher precedence than the addition and subtraction operators 5 + 3 * 2 will be giving the result 11 not 10

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

Arithmetic Assignment Operators

P.Thendral-CPPS-Lecture 3

14

You might also like