CMPE113_Lecture 5_Input_Output_Operations
CMPE113_Lecture 5_Input_Output_Operations
CMPE113
Computer Programming - I
Atilim University
3
Recall: Function main
• Every C program has a main function.
int main (void)
• declarations
• the part of a program that tells the compiler the names of
memory cells in a program
• executable statements
• program lines that are converted to machine language
instructions and executed by the computer
5
Recall: Statements
• The body of a program contains statements which are actually the
commands that we are going to give to our computer in order to
perform the tasks that we want.
• Examples:
printf("Enter weight in pounds: ");
scanf("%lf", £s);
8
int type
• Integer numbers are whole numbers, such as –3, 0, 15.
• They do not contain a decimal point, and they may be positive or negative.
• A positive integer can be preceded or not with a plus sign. E.g., 15 and +15 are the same
numbers. On the other hand, minus sign must be used before a negative number. E.g., -
15.
• Leading zeros are ignored. E.g., 015, +00015, 0000015 are all the same number: 15.
• An integer can not contain a comma, even if it contains too many digits. E.g., 1,000,000 is
wrong, it should be written as 1000000.
• Decimal points can not be used when writing integers. E.g., 15 and 15.0 have the same
numeric value, but 15.0 is not of type integer (but double).
• If we want to declare a variable in which we will store an integer, e.g. year, we declare it
as int year;
• If we want to declare more than one integer variables, we can list them separating with
commas: int year, age, number;
9
double type
• double type: This type is used to store real numbers. A real number has an
integral part and a fractional part which are separated by a decimal point, such as
3.0, 0.75, -2.47.
• A real number can be positive or negative, and the rules for + and – signs are the
same as for the integers. E.g., 0.75 = +0.75 -0.75.
• Both leading and trailing zeros are ignored. E.g., -02.47, -2.4700, -002.470 are all
equal to -2.47.
• The zero before or after the decimal point can be omitted. Thus, 3.0 can also be
written as 3.. But do not forget the decimal point, otherwise it becomes an
integer.
• Notice that the arithmetic values of 3.0 and 3 are the same, but the type of these
two numbers are different: First one is double, second one is int.
• Similarly, 0.75 can be written as .75 in C language.
10
double type
• We can use exponential notation, especially for very small or very
large real numbers. This notation is very similar to scientific notation
in arithmetics.
• You know from arithmetics that 6.5x102 = 650 and 0.00012 = 1.2x10-4.
These values can be expressed as 6.5E+2 (or 65E+1) and 1.2E-4 (or
12E-5) respectively. This notation is referred as exponential notation.
• Read the letter E as “times 10 to the power”. E.g., read 1.2E-4 as “1.2
times 10 to the power minus 4”.
• If we want to declare a variable to contain a real number, e.g. weight,
we declare it as double weight;
11
char type
• char type: This data type is used to store a single character value, such as a
letter, a digit, or a special symbol.
• A character value must be enclosed in single quotes (‘) in a program,. E.g.,
‘A’, ‘z’, ‘2’, ‘5’, ‘*’, ‘:’, ‘ ‘ (blank character).
• It is very important to understand the difference between 5 and ‘5’
appearing in a program. The first one is an integer, thus, it has an
arithmetic value, and it can be used in arithmetic operations. On the other
hand, the second one is a character, and has no arithmetic value. It can not
be used in arithmetic operations.
• If we want to declare a variable to contain a character, e.g. option, we will
declare it as char option;
12
Constants
• A constant is a value that does not change during the execution of a
program.
• You can use constant values in many different statements in your
programs. As you have already learned, you can also give names to
some constant values using #define preprocessor directive, and use
those names in the statements.
• Eg.:
• #define PI 3.14159
• #define MAX 1000
• #define BLANK ' ’
13
Assignment Statements
• Assume we declare an integer variable:
int age;
• Once we have declared our variables, we can put values in them by means
of assignment statements:
int age=20;
• In an assignment, the movement of data is from the right of the
assignment symbol to the left.
• If the variable already had a value, that value is overwritten after the
assignment statement.
age = 25;
14
Assignment Statements
• It is also possible to assign the value of a variable to another variable.
salary1 = 15.780;
salary2 = salary1;
• The variable on the left hand side and the value on the right must
match in data type. Real numbers must be assigned to double
variables, integers to integer variables, and characters to character
variables.
15
Assignment Statements
16
Input/Output Statements
17
Input/Output statements
• The input statement in C language is scanf,
• The output statement in C language is printf.
• Both are functions supplied as part of the C standard input/output
library to which we gain access through the preprocessor directive
18
Output Operation: printf ()
• printf () displays the format string on the screen.
• printf () is used to write formatted output to console
19
Output Operation: printf ()
• Syntax:
printf(format string);
printf(format string, variable list);
20
Example: The printf Function
• print list
• in a call to printf, the variables or expressions whose values
are displayed
• placeholder
• a symbol beginning with % in a format string that indicates
where to display the output value
function name
© 2016 Pearson Education, Ltd. All rights reserved. 21
Output Operation: printf ()
• If you want to display it on a new line, you should put \n either at the
beginning of the format string in the second printf statement, or at
the end of the format string in the first printf statement.
Output screen
22
\n -- new line
Formatting Characters:
“\n”: Newline escape sequence
Moves to the first column of next line
“\b”: Moves to left one column
“\f”: Moves to the first column of next page
“\t”: Skips to next tab position
“\\”: Prints one backslash
int a=5
double b=8.13; a=5 b=8.130000
printf("a=%d b=%f",a,b);
25
Placeholders
• It is possible to display the result of an arithmetic expression using
printf.
•
Output screen
26
Input Operation: scanf()
• Copies data from the standard input device (usually
the keyboard) into a variable (stored in memory).
double miles;
scanf(“%lf”, &miles);
28
Memory
Address of operator & 4001
4002
4003 2000 birth_year
• The name of the input variable is preceded by & 4004
symbol, which is called as address of operator. It 4005
tells the computer the address of the variable 4006
into which the input will be placed. 4007
4008
Output screen
int birth_year;
printf("Enter your birthyear: "); Enter your birthyear: 2000
scanf("%d", &birth_year); You are 21 years old
printf("You are %d years old", 2021-birth_year);
• Here, the user should enter three numbers separated by blanks: first
an integer, then a double, lastly an integer, e.g.: 15 3.5 765.
• If the placeholder and the data type of the corresponding variable do
not match, it will cause an error.
30
Input/ Output Operations: Examples-1
Output screen
31
Input/ Output Operations: Examples-2
Memory
4001
4002 C letter1
4003
4004
4005
4006 A Letter2
4007
4008
Output screen
4009 N letter3
4010
4011
4012
32
Thank you
Any Questions