Professional Documents
Culture Documents
CHAPTER 4 Part 1
Input and Output
Objectives
• After this lesson, you should be able to:
Understand and identify the usage of input
functions and operations using keyboard and
file
Understand and identify the usage of output
functions and operations (screen and into a
file)
Write C program using input and output
operations.
1
Sub topics in Chapter 4
File I/O
Introduction
Formatting Output
Chap 4 printf() function
Part 1
Introduction
• Input and output are two important
operations in computer programming.
• Data will be read as input to computer
and will be processed as output.
• Input and output (I/O) can be done either
through interactive or file batch
processing.
2
Input/Output
operation
input output
use use
scanf() function printf() function
printf() function
printf() purpose
Send data to output device regarding to a
given format
format printf(“output_format”,print_list) ;
example
Output
printf(“First example“) ;
3
printf() function (continued..)
format printf(“output_format”,print_list) ;
text
may
output_format
consist of placeholder ““
combination
may variable
print_list consist of constant
expression
function_name
Example 1:
4
printf() function (continued..)
Example 2:
printf(“%lf”, salary);
variable
Format output- placeholder
Output:
Example 3:
placeholder
Output:
Monthly salary is RM
10
10
5
printf() function (continued..)
Example 4:
11
Exercise
What are the output produced from the following statements?
1.printf(“Welcome to C programming world!>”);
2.printf(“Now “);
printf(“See “);
printf(“Heart “);
3.printf(“BIC 10204\n”);
printf(“Algorithm and Programming”);
12
6
printf() function (continued..)
escape Character
purpose
To tell the compiler additional formatting
to be performed with the printf() function
Character Explaination
\n New line
\t Tab
\a Beep sound
Placeholder
purpose
A symbol beginning with % in a format string that
indicates where to display the output value
7
printf() function (continued..)
Output:
ABU
I live in BP
15
Output:
My house number is 26
Number -858993460 and 10Number 2 and 15
16
8
printf() function (continued..)
Example 6 (Printing floating point number) :
Output:
The value of pi is 3.142
The value of sahih1 is 23.46 The value of sahih2 is 15.78
17
Exercise
1. Given a = 2, b = 4. What are the output produced by the following
statements?
a) printf(“The additionresult of two number >> %d”,a + b);
b) printf(“\nThe addition of %d and %d “, a,b);
2. Write a valid C programming statement to display the following output
(use variables):
(Notes: Given the value of variable x = 2.5 and y = 3.1
The value of x is 2.5cm and y is 3.1cm
The mutiplication result of x and y is 7.75cm
18
9
scanf() function
format scanf(“input_format”,&variable_list) ;
example scanf(“%d“,&nom) ;
scanf(“%s %d %lf”,&name,&age,&weight) ;
19
Consists of example
input_format placeholder %d @ %c @%lf
Called as ampersand
‘&’ symbol
Variable address/location reference
Must consist
variable_list variable
Not allowed!!!
20
10
scanf() function (continued)
Example1:
Placeholder Variable
Important Note!!!!!
Every variables to be used in the program MUST be declared
first!!!
21
Example 2:
scanf(“ %d ”, 15);
11
scanf() function (continued)
?
void main() mistakes?
{ int semester;
float CPA;
?
scanf(“%d”, &semester);
printf(“\nEnter your CPA : “);
scanf(“%lf”,&CPA);
printf(“\nYour CPA for semester %d is %f”,semester,CPA);
}
? 23
Output:
24
12
scanf() function (continued)
Exercise
1).Write the scanf() statement to accept the following type of variables:
a. char jantina;
b. float berat;
c. int tempoh;
2).State whether the following scanf() statements is VALID or INVALID.
Given the following declaration:
int a, b;
char c;
a) scanf(“%d”, &a + b);
b) scanf(“%c”, &c);
c) scanf(“%d”,&a,&b);
25
26
13
Other I/O functions (continued)
example usage
char letter;
printf(“Enter one letter>>”);
letter= getchar();
27
format putchar(variable);
example usage
printf(“The input letter is ”);
putchar(letter);
28
14
Other I/O functions (continued)
29
30
15
Other I/O functions (continued)
Example 3: getch()
We can use getch() to hold user screen, and terminate once
any input is read
#include <stdio.h>
void main()
{ int number;
32
16
Other I/O functions (continued)
33
format gets(variable);
example
char name[20];
printf(“Enter your name >>”);
gets(name);
34
17
Other I/O functions (continued)
Example 4: gets()
If we use scanf() function to read string input, it will only takes
the first data between two words (if any)
#include <stdio.h>
void main() Enter your name: Siti Aminah
{ char name[20]; Your name is Siti
35
Example 5: gets()
If we use gets() function to read string input, it will takes all the
data including space between two words (if any)
#include <stdio.h>
void main() Enter your name: Siti Aminah
{ char name[20]; Your name is Siti Aminah
18
Other I/O functions (continued)
example
printf(“Your name is ”);
puts(name);
37
Formatting Output
format %spaced
Where to use? Use the integer formatting in the
output statement
38
19
Formatting Output (continued)
Assume _ is
an empty
Formatting Integer space
example output
printf(“%3d”,123); 123
example output
printf(“%5d”,123); _ _123
example output
printf(“%10d”,123); _ _ _ _ _ _ _123
example output
printf(“%-6d”,123); 123_ _ _
example output
printf(“%-4d%4d”,123,456); 123_ _456
39
Output?
#include <conio.h>
void main()
{
printf("%d\n",123);
printf("%3d\n",123);
printf("%-3d\n",123);
printf("%5d\n",123);
printf("%-5d\n",123);
printf("%-10d\n",123);
printf("%10d\n",123);
printf("%4d\%-4d",123,456);
getch();
} 40
20
Formatting Output (continued)
format
Formatting Floating number %precisionf
example output
printf(“%10f”,57.78); _57.780000
example
output
printf(“%15f”,57.78); _ _ _ _ _ _57.780000
example
output
printf(“%-10f”,57.78); 57.780000_
example output
printf(“%-15f”,57.78); 57.780000_ _ _ _ _ _
42
21
Formatting Output (continued)
format
Formatting character %precisionc
43
format
Formatting String %precisions
22
Formatting Output (continued)
Exercise
1. What is the output of this statement?
printf(“%8.5s”,”BIC 10204”)
45
46
23
47
Calling fscanf() to
read data from a file
48
24
Extra steps for reading data from a
file (continued)
• Declaring a pointer variable
– To use with fopen() and fscanf() functions
• Calling fopen() to allow program to access the
file
• Calling fscanf() to read data from a file
– fscanf(file_pointer,format_string,argument_list);
– File pointer is a variable whose memory cell contains an address
instead of an int, float or double value.
• The address gives the key to access the file stores on disk.
• It must begin the declaration with the word FILE and have an asterisk (*)
before the variable name
50
25
51
26
Reading data from a file and
Writing data into a file
53
54
27
Faculty of Computer Science and Information Technology
Chapter 4 Part 2
Expression 1
0011 0010 1010 1101 0001 0100 1011
&
Operator 1
O bjectives
1
0011 0010 1010 1101 0001 0100 1011
1
Introduction to Expression
1
0011 0010 1010 1101 0001 0100 1011
C
Arithmetic Expression
hapter 4 (Part 2)
Logical Expression
Relational Expression
33
Introduction to Expression
1
0011 0010 1010 1101 0001 0100 1011
Given the following statement :
2x + 3 – z = y
expression
2
Introduction to Expression
1
0011 0010 1010 1101 0001 0100 1011
withdrawal scenario
balance – drew out money = current balance
Expression in C :
Introduction to Expression
What is Expression?
1
Combination of more than one variable
0011 0010 1010 1101 0001 0100 1011
example Operator
x+3 - z
Operand
Consists of arithmetic
relational
logical
3
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011Known as
Arithmetic Expression Mathematic Expression
using
Arithmetic Operator
Represents by
Represents by
Unary operator Binary operator
-- decrement + add
++ increment - subtraction
% modulus
7
Arithmetic Expression
Unary Operator
1
0011 0010 1010 1101 0001 0100 1011
Increment Decrement
prefix ++c; --c;
postfix c++; c--;
8
4
Arithmetic Expression
Unary Operator
1
Example 1:
0011 0010 1010 1101 0001 0100 1011
int A = 5;
++A;
printf(“%d”,A); output ?
A++;
printf(“%d”,A); output ?
9
Arithmetic Expression
Unary Operator
1
0011 0010 1010 1101 0001 0100 1011
Example 2:
int A ;
A=6;
printf(“%d”,3 + --A); output ?
printf(“%d”, A); output ?
A=6;
printf(“%d”, 3 + A--); output ?
printf(“%d”, A); output ?
10
5
Arithmetic Expression
Unary Operator
1
0011 0010 1010 1101 0001 0100 1011
Example 3:
Arithmetic Expression
Binary Operator
1
0011 0010 1010 1101 0001 0100 1011
example
A + z
operator
operand
12
6
Arithmetic Expression
Binary Operator
1
0011 0010 1010 1101 0001 0100 1011
example
A * z
operator
operand
Mathematic Arithmetic Expression
2x + y 2*x+y
13
Arithmetic Expression
Binary Operator
1
0011 0010 1010 1101 0001 0100 1011
example
A / z
operator
operand
Mathematic Arithmetic Expression
2÷y 2/y
14
7
Arithmetic Expression
Binary Operator
1
0011 0010 1010 1101 0001 0100 1011
example
A % z
operator
operand
Return a balance when 2 numbers is divided
Arithmetic Expression
Binary Operator
1
0011 0010 1010 1101 0001 0100 1011
Example:
int A, B;
float C;
A= 2;
B=5;
C= 2.4;
B% A;
Valid! Answer is 1
C % A;
Invalid! C is float
16
8
Arithmetic Expression
Assignment Statement
1
0011 0010 1010 1101 0001 0100 1011
Used to store value/result of process to a variable
Use operator symbol =
Assignment statement
17
Arithmetic Expression
Assignment Statement
1
0011 0010 1010 1101 0001 0100 1011
Format / syntax :
variable = value;
variable = constant; or variable = variable;
variable = expression;
Example : average 44
1.average= ( 6 + 5) * 4;
1500
grossSalary
2.grossSalary = 1500;
nettSalary 1700
nettSalary = grossSalary + 200;
price 50.00
3.price= 50.00;
pay = price; pay 50.00
` 18
9
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
int a = b= c = d = e = 250;
int b =2, number =0, total = 0,average =3;
number = b++ = 10;
int age = workHour = 0;
19
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
Function
operator:
+= , -= , *= , /= , %=
20
10
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
Example :
21
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
Operator Arrangement/Priority
() Left to right
++, -- Right to left
*, /, % Left to right
+, - Left to right
22
11
Arithmetic Operator
1
0011 0010 1010 1101 0001 0100 1011
Example:
1. 5 + 2 * 6 – 4 / 2 2. 3 * 4 / 2 + ( 3 –1)
5 + 12 - 4 / 2 3*4/2+ 2
5 + 12 - 2 12 / 2 + 2
17 - 2 6 + 2
15 8 23
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
Example:
3. Prefix unary arithmetic expression
Output:
4 30
24
12
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
Example:
3. Prefix unary arithmetic expression
Output:
4 35
25
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
26
13
Arithmetic Expression
1
0011 0010 1010 1101 0001 0100 1011
Example:
#include<stdio.h>
#include <math.h>
Output :
void main() 4
{
int x = 16, y ;
y = sqrt(x);
printf(“%d”,y);
}
27
Arithmetic Expression
Exercise:
1
0011 0010 1010 1101 0001 0100 1011
a) b = 3 + b b) x = (a – b)(a – c2)
a+4
c. ++a d. c + a * c / (3 * c)
28
14
Arithmetic Expression
Exercise:
1
0011 0010 1010 1101 0001 0100 1011
Assume i,j and k are integer variables with i = 5 and j=3. Determine
the value for each of the following statement:
a) k = j++; d) k = ++j;
b) k = i * j--; e) k = i * --j;
c) k = j + i * j++; f) k = 27 / j++ - 16 % i;
29
Relational Expression
1
Relational
0011 0010 1010 1101 0001 0100 1011
use
Relational operator
expression
constant vs constant
30
15
Relational Expression
Relational Operator
1
0011 0010 1010 1101 0001 0100 1011
Operator Description
== Equal to
Relational Expression
1
P/s:
Example 1:
0011 0010 1010 1101 0001 0100 1011
a, b and c are variables,
Replaced with the given values
1) a+ b == c 2) a != b
6 + 1== -2 6 != 1
7 == -2
32
16
Relational Expression
1
1010 1101
3) b<c 4) b + c <= a
1 < -2 1 + -2 <= 6
-1 <= 6
33
Relational Expression
P/s:
Relational operator has less
1
0011 0010 1010 1101 0001 0100 1011
Example 3:
priority than other operators.
Start evaluating from left to
int a=10, b = 3, c = 7; right.
(a+b >= 3*c)==( a != 2*c+b)
34
17
Relational Expression
1
0011 0010 1010 1101 0001 0100 1011
#include <stdio.h>
void main()
{ int age;
}
35
Logical Expression
Logical expression
1
0011 0010 1010 1101 0001 0100 1011 Logical Operator
use
Can consists of
Relational expr. vs logical expr.
36
18
Logical Expression
Logical Operator
1
0011 0010 1010 1101 0001 0100 1011
Operator Description
&& AND
|| OR
! NOT
37
Logical Expression
1
Logical
0011 0010 1010 1101 0001 operator
0100 1011 truth table for AND
38
19
Logical Expression
1
Logical
0011 0010 1010 1101 0001 0100operator
1011 truth table for OR
39
Logical Expression
1
Logical
0011 0010 1010 1101 0001 0100operator
1011 truth table for NOT
NOT(!)
40
20
Logical Expression
1
Example
0011 0010 1010 1101 0001 0100 1011 1:
1 && 1 (1 < 2) || (6 == 6)
1 1 || 1
41
Logical Expression
Example 2:
1
0011 0010 1010 1101 0001 0100 1011
Given a = 3, b = 4;
!(1) ! ( 0)
0 1
42
21
Logical Expression
1
0011 0010 1010 1101 0001 0100 1011
#include <stdio.h>
void main()
{ int mark;
43
Logical Expression
Exercise:
1
0011 0010 1010 1101 0001 0100 1011
1. Given with i=2, j = 5 and k = 15. Evaluate each of the
following expression:
a) i > j – k g) k == j + i * j
b) i != k h) k <=k /j
c) (i >= 1) && (j == 5) i) (j < i) || (k > j)
d) !( i > j) j) (i >0) && (j <k) || (k <i)
e) i < j < k k) i * k < k / j
f) (i < j) && (j < k) l) i – j > k
int angka1,angka2;
22