Upload
yong-nierah
View
241
Download
1
Embed Size (px)
Citation preview
8/10/2019 3 - Input Output
1/45
INPUT AND OUTPUT PROCESS
8/10/2019 3 - Input Output
2/45
Aim
To introduce student to input and output
concept in C programming
Objective
Student will be able to : Understand and identify the usage
of input functions and operations
Understand and identify the usage
of output functions and operations Write C program using input and
output operations.
http://tafmaster.com/taf/496/250924/8/10/2019 3 - Input Output
3/45
Chap 4
Introduction
printf() function
scanf() function
Other I/O functions
Formatting Output
Topics in Chapter 4
8/10/2019 3 - Input Output
4/45
Input and output are two importantoperations in computer programming.
Data will be read as input to computerand will be processed as output.
Input and output (I/O) can be doneeither through interactive or file batch
processing.
Introduction
8/10/2019 3 - Input Output
5/45
operation
input output
scanf() function printf()function
Read data (input) Write out data (output)
use use
includeInclude
Input/Output
8/10/2019 3 - Input Output
6/45
printf ) function
printf()
Write out data and display as output
Send data to output device regarding to a
given format
printf(output_format,print_list);
printf(First example);
printf(The bread price is RM %lf,price);
function
format
exampleOutput
Output
8/10/2019 3 - Input Output
7/45
printf ) function
continued..
output_format
text
placeholder
combination
print_listvariable
constant
expression
function_name
printf(output_format,print_list);format
may
Consist of
mayConsist of
8/10/2019 3 - Input Output
8/45
Example 1:
printf(Universiti Tun Hussein Onn Malaysia);
Output:
Universiti Tun Hussein Onn Malaysia
printf ) function
continued..
Format output text/string
8/10/2019 3 - Input Output
9/45
Example 2:
printf(%lf, salary);
Format output- placeholdervariable
Output:
printf ) function
continued..
8/10/2019 3 - Input Output
10/45
Example 3:
printf(Monthly salary is RM %lf, salary);
Output format -Text
placeholder
variable
Output:
Monthly salary is RM
printf ) function
continued..
8/10/2019 3 - Input Output
11/45
Example 4:
const float porosity 16.78;
printf(The porosity of sand = %lf , porosity);
Output format -Text
The porosity of sand = 16.78000
Output:
printf ) function
continued..
constant
placeholder
8/10/2019 3 - Input Output
12/45
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(DIT 1064\n);
printf(Programming Principle);
pr in t f () funct ion
(continued..)
8/10/2019 3 - Input Output
13/45
escape Character
functionTo tell the compileradditional formattingto be performed with theprintf()function
Character Exaplaination
\n New line
\t Tab
\a Beep sound
pr in t f () funct ion (continued..)
8/10/2019 3 - Input Output
14/45
Placeholder
functionA symbol beginning with % in a format string that
indicates where to display the output value
format Using percent symbol (%) followed by characterwhich represent particular data type
Placeholder Output to be print%d Integer number
%lf Floating number
%c Character
%s String
pr in t f () funct ion (continued..)
8/10/2019 3 - Input Output
15/45
char huruf1, huruf2;
huruf1 = B;
huruf2 = P;
printf(%c %c %c, A, B, U);
printf(\nI live in %c%c,huruf1,huruf2);
Example 5 (Printing character value ) :
Output:
ABU
I live in BP
pr in t f () funct ion (continued..)
8/10/2019 3 - Input Output
16/45
int number1, number2;
printf(My house number is %d, 26);
number2=10;
printf(\n\tNumber %d and %d,number1,number2);
number2=2;
number1= 15;
printf(Number %d and %d,number2,number1);
Example 6 (Printing integer value) :
Output:
My house number is 26
Number and 10Number 2 and 15
pr in t f () funct ion (continued..)
8/10/2019 3 - Input Output
17/45
double sahih1, sahih2;
printf(The value of pi is %lf, 3.142);sahih1= 23.46;
printf(\nThe value of sahih1 is %lf,sahih1);
sahih2= 15.78;
printf(\tThe value of sahih2 is %lf,sahih2);
Example 6 (Printing floating point number) :
Output:
The value of pi ialah 3.142
The value of sahih1 is 23.46 The value of sahih2 is 15.78
pr in t f () funct ion (continued..)
8/10/2019 3 - Input Output
18/45
8/10/2019 3 - Input Output
19/45
scanf ) function
scanf()Read data with specified
format
Read input from keyboard
scanf(input_format,&variable_list);
scanf(%d,&nom);
scanf(%s %d %lf,&name,&age,&weight);
function
format
example
8/10/2019 3 - Input Output
20/45
scanf ) function continued..)
placeholder
variable_list variableMust consist
input_formatConsists of example
%d @ %c @%lf
constantexpression function
Not allowed!!!
& symbol
Called as ampersand
Variable address/location reference
8/10/2019 3 - Input Output
21/45
Example1:
scanf(%lf %d %c,&float_num, &num,&letter);
Placeholder
Every variables to be used in the program MUSTbe declaredfirst!!!
Variable
The above statement is valid!
Important Note!!!!!
scanf() func t ion (cont in ued..)
Faculty of Information Technology & Multimedia
8/10/2019 3 - Input Output
22/45
Example 2:
scanf( %d , 15);
Placeholder Constant value
The above statement is INVALID!
scanf( %lf , &price + 0.05);
Conversion character Expression
The above statement is INVALID!
scanf() funct ion (cont inued..)
8/10/2019 3 - Input Output
23/45
Example 3(program code):
#include
void main()
{ int semester;
float CPA;
printf(Enter the semester : );
scanf(%d, &semester);
printf(\nEnter your CPA : );
scanf(%lf,&CPA);printf(\nYour CPA for semester %d is %f,semester,CPA);
}
?
?
?
scanf() fun ct ion (cont inued..)
8/10/2019 3 - Input Output
24/45
Output:
Enter the semester : 3
Enter your CPA : 3.45
Your CPA for semester 3 is 3.45
scanf() func t ion (cont in ued..)
8/10/2019 3 - Input Output
25/45
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);
Exercise
scanf() func t ion (cont in ued..)
8/10/2019 3 - Input Output
26/45
Other I/O functions
There are some functions can be used in the input andouput operations in C programming such as:
getchar() and putchar() function
getch() dan putch() function
gets() dan puts() function
8/10/2019 3 - Input Output
27/45
getchar()
To read any input from input device
as character data type
variable= getchar();
char letter;
printf(Enter one letter>>);letter= getchar();
function
format
example usage
Other I/O functions
8/10/2019 3 - Input Output
28/45
putchar()
To display output in a single character
value
putchar(variable);
printf(The input letter is );
putchar(letter);
function
format
example usage
Other I/O functions
8/10/2019 3 - Input Output
29/45
Other I/O functions
#include
void main()
{ char letter;
printf(Enter one letter : );
letter = getchar();
printf(The input letter is\t);
putchar(letter);
}
Example 1: getchar() and putchar()
Enter one letter :eThe input letter is e
8/10/2019 3 - Input Output
30/45
Other I/O functions
#include
void main(){ int number;
printf(Enter one number : );
number = getchar();
printf(The input number is %d\n,number);putchar(number);
}
Example 2: getchar() and putchar()
Enter one number :123The input number is 49
1
However, we can also program like this :
8/10/2019 3 - Input Output
31/45
getch()
To read an input and terminate the
program automatically
variable= getch();
char letter;printf(Enter one letter >>);
letter= getch();
function
format
example
Other I/O functions
getch() have same function like getchar(), but different action
8/10/2019 3 - Input Output
32/45
Other I/O functions
#include
void main(){ int number;
printf(Enter one number : );
number = getchar();
printf(The input number is %d\n,number);getch();
}
Example 3: getch()We can use getch() to hold user screen, and terminate once
any input is read
8/10/2019 3 - Input Output
33/45
putch()
To display output , have similar function as
putchar
putch (variable);
printf(The input letter is );putch(letter);
function
format
example
5.5 Other I/O functions
8/10/2019 3 - Input Output
34/45
gets()To read string input from input device
gets(variable);
char name[20];printf(Enter your name >>);gets(name);
function
format
example
Other I/O functions
8/10/2019 3 - Input Output
35/45
Other I/O functions
#include
void main(){ char name[20];
printf(Enter your name : );
scanf(%s,&name);
printf(Your name is %s ,name);getch();
}
Example 4: gets()If we use scanf() function to read string input, it will only takes
the first data between two words (if any)
Enter your name: Siti AminahYour name is Siti
8/10/2019 3 - Input Output
36/45
Other I/O functions
#include
void main(){ char name[20];
printf(Enter your name : );
gets(name);
printf(Your name is %s\n,name);getch();
}
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)
Enter your name: Siti AminahYour name is Siti Aminah
8/10/2019 3 - Input Output
37/45
puts()To display string output to output device
puts (variable);
printf(Your name is );puts(name);
functions
format
example
Other I/O functions
8/10/2019 3 - Input Output
38/45
formattingInteger
To format the display of an integer
value
%spaced
Use the integer formatting in theoutput statement
functions
format
Where to use?
Formatting Output
How it works? Provide spaces according to formattingPut the value to display starting from right to leftsideIf there issign in the formatting, values willbe printed starting from left to right side
8/10/2019 3 - Input Output
39/45
FormattingOutput
Formatting Integer
exampleprintf(%3d,123);
output123
printf(%5d,123);output
_ _123
printf(%10d,123);output
_ _ _ _ _ _ _123
printf(%-6d,123);output
123_ _ _
printf(%-4d%4d,123,456);output
123_ _456
Assume _ isan empty
space
example
example
example
example
8/10/2019 3 - Input Output
40/45
Formatting Output
#include
void main()
{printf(%d ,123);printf(\n%3d ,123);
printf(\n%5d ,123);
print(\n%10d,123);
print(\n%-6d,123);
print(\n%-4d%d4d,123,456);}
Example 1: Formatting Integer
Output?
8/10/2019 3 - Input Output
41/45
FormattingFloatingnumber
To format the display of the floating
number value
%precisionf
Use in the output statement
functions
format
Where to use?
Formatting Output
How it works? Provide spaces according to formattingPut the value to display starting from right to leftsideIf there issign in the formatting, values willbe printed starting from left to right side
8/10/2019 3 - Input Output
42/45
Formatting Floating numberformat
%precisionf
exampleprintf(%10f,57.78);
output_57.780000
example
printf(%15f,57.78);output
_ _ _ _ _ _57.780000
example
printf(%-10f,57.78);output
57.780000_
exampleprintf(%15f,57.78);
output57.780000_ _ _ _ _ _
Spaces is including floating point
Formatting Output
8/10/2019 3 - Input Output
43/45
Formatting Output (cont..)
Formatting characterformat
%precisionc
example char letter = A;printf(%c,letter);printf(%2c,letter);printf(%-5c,letter);
A_AA_ _ _ _
output
8/10/2019 3 - Input Output
44/45
Formatting Stringformat
%precisions
char ayat[] =KUITTHO PARIT RAJAprintf(%s,ayat);
printf(%4s,ayat);printf(%22s,ayat);printf(%.7s,ayat);printf(%-20.13s,ayat);
KUITTHO PARIT RAJAKUITTHO PARIT RAJA_ _ _ _KUITTHO PARIT RAJAKUITTHOKUITTHO PARIT_ _ _ _ _ _ _
output
example
Formatting Output (cont..)
8/10/2019 3 - Input Output
45/45
1. What is the output of this statement?
printf(%8.5s,BIT 1033)
2.State the output of these statements:
float h = 253.7654;
int g=6789;
printf(%5d %5.1f ,g,h);
printf(%10d %10.3f ,g,h);
printf(%-7d %-7.5f ,g,h);
Exercise
Formatting Output (cont..)