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

ELC 111 : Computer Programming I

Reading Data

Lecturer: Dr. Reham Samir


References
 Paul Deitel, Harvey Deitel, “C How to Program with
an introduction to C++ ”, Pearson Education, eighth
edition, 2016.

 Noel Kalicharan, “Learn to Program with C ”, Apress,


2015.
The scanf function

 scanf is used to obtain a value from the user.


 The function reads from the standard input, which is usually the
keyboard.
 When the computer executes the preceding scanf, it waits for the
user to enter a value for variable integer1.
 The user responds by typing an integer, then pressing the Enter key
to send the number to the computer.
 The computer then assigns this number, or value, to the variable
integer1.
The scanf function

 When this statement is executed, the value entered by the user is


placed into a memory location to which the name integer1 has been
assigned.
 Suppose the user enters the number 45 as the value for integer1.
The computer will place 45 into location integer1,

 Whenever a value is placed in a memory location, the value


replaces the previous value in that location and the previous value
is lost.
The scanf function

 This scanf has two arguments, "%d" and &integer1.


 The first, the format control string, indicates the type of data that
should be entered by the user.
 The %d conversion specifier indicates that the data should be an integer
(the letter d stands for “decimal integer”).
 The second argument of scanf begins with an ampersand (&) is
called the address operator and is followed by the variable name.
 The &, when combined with the variable name, tells scanf the location
(or address) in memory at which the variable integer1 is stored.
 The computer then stores the value that the user enters for integer1 at
that location.
Another Simple C Program: Adding Two Integers

 Our program uses the Standard Library function scanf to obtain


two integers typed by a user at the keyboard, computes the sum of
these values and prints the result using printf.
Another Simple C Program: Adding Two Integers

 The output from this program will be like this:

 This printf has two arguments, "Sum is %d\n" and sum.


 The first is the format control string. It contains some literal characters
to be displayed and the conversion specifier %d indicating that an
integer will be printed.
 The second argument specifies the value to be printed.
Another Simple C Program: Adding Two Integers

 Calculations can also be performed inside printf statements. For


example:

 Then

 Can be replaced with the statement:


Reading Data Into an int Variable
 We can use scanf to read more than one value at a time.
 For example, suppose we want to read three integer values for
variables a, b, and c, thus:
scanf("%d %d %d", &a, &b, &c);
 Note that: if any invalid character is encountered while reading the
data, the program will crash.
 For instance, if the user types

42 -7 v8
or
42= 18 24
Reading Data Into a float Variable
 If we wish to read a floating-point number into a float variable x,
we can use:
scanf("%f", &x);
 When executed, scanf expects to find a valid floating-point constant
in the data.
 For example, any of the following will be acceptable:
4.265
-707.96
2.345E+1
 In the last case, there must be no spaces, for instance, between the 5 and
the E or between the E and the + or between the + and the 1.
 The following will all be invalid for reading the number 23.45:
2.345 E+1
2.345E +1
2.345E+ 1
Reading Data Into a double Variable
 If we wish to read a floating-point number into a double variable,
y, we can use:
scanf("%lf", &y);
 Note that the specification %lf (percent ell f) is used to read a value into a

double variable.
 Be careful: you cannot use %f for reading data into a double variable. If you

do, the value read will be stored in 32 bits rather than 64 bits.
 You can read values into more than one variable using one scanf
statement.
 Example: If x and y are double variables, you can use:
scanf("%lf %lf", &x, &y);
 Note that:
 When entering data for a float/double variable, an integer is
acceptable.
 If you enter 42, say, it will be interpreted as 42.0.
Reading Data Into a double Variable
 Note that:
 If you enter a floating-point constant e.g., 2.35 for an int variable, it
will be truncated to 2.
 %lf is used to read a value into a double variable.
 %f is used to read a value into a float variable.
 %f is used to print the value of a float or double variable.
 You can read values for int, double, or float variables in the same
scanf statement.
 Example: Suppose item and quantity are int, and price is double.

We can write the statement:


scanf("%d %lf %d", &item, &price, &quantity);
Reading Data Into a double Variable
 Example: Identify whether the following data are valid for the
scanf statement:
scanf("%d %lf %d", &item, &price, &quantity);
1. 4000 7.99 8.7
2. 4000 7.99 x.8
3. 25cm 10 44
4. 3575 10 44
5. 5600 25.0 1
6. 560 25 amt = 7
Reading strings
 Assume that we declare a variable called item that hold a string
value as:
char item[50];
 We can assign a string value to item using the standard string
function, strcpy as:
strcpy(item, "Right front headlamp");
 To read a value from the input into item. We will use the gets
function (which will wait for you to type item) as:
gets(item);
Reading strings
 Example: what is the output from the following program if the
user type Ali:
#include <stdio.h>
int main()
{
char name[50];
printf("Hi, what's your name? ");
gets(name);
printf("Delighted to meet you, %s\n", name);
}
 Sol:
Hi, what's your name? Ali
Delighted to meet you, Ali
Examples (Average)
 Write a program to request three integers and print their average.
The program should work as follows:

 Sol:

f
Examples (Average)
 Notes:
 We use 3.0 instead of 3 in calculating the average. This forces a
floating point division to be performed.
 If we had used 3, an integer division would be performed,

giving 13.0 rather than 13.3 as the answer for the sample
data, above.
 We could calculate and print the average in the printf statement
with
 printf("\nTheir average is %f\n", (a + b + c) / 3.0);
Examples (Banking)
 Write a program to read the data for a customer in a bank: name, account
number, average balance, and number of transactions made during the
month, then calculate the interest earned and service charge.
 The interest is calculated as follows:
interest = 6% of average balance
 The service charge is calculated by this:
service charge = 50 cents per transaction
 Finally the program print the customer’s name, average balance, interest
earned, and service charge.
 The following is a sample run of the program:
Examples (Banking)
 Sol:

You might also like