Upload
alish-al-shahab
View
696
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Ada pembetulan di slide sila lihat http://dti2143.blogspot.com/2011/01/week-2-slide.html
Citation preview
Chapter 2 :INPUT AND OUTPUT
PROCESSDTI 2143: Computer Programming
OBJECTIVES-PART 1
To understand input and output streams. To be able to use all print formatting capabilities. To be able to use all input formatting capabilities. To be able to print with field widths and precisions. To be able to use formatting flags in the printf format
control string. To be able to output literals and escape sequences.
PRINTF() FUNCTION
3
printf()
To display output/data
To send data to output device
printf(“output_format”,print_list) ;
printf(“First example“) ;
printf(“The bread price is RM %lf“,price) ;
function
format
example
PRINTF() FUNCTION (CONTINUED..)
4
Output_format
mayconsists
text
placeholder
combination
Inside“ ”
Print_list
variable
constant
expression
function_name
mayconsist
printf(“output_format”,print_list) ;format
PRINTF() FUNCTION (CONT..)
5
Example 1:
printf(“Universiti Tun Hussein Onn Malaysia”);
Output format -Text
Output:
Universiti Tun Hussein Onn Malaysia
PRINTF() FUNCTION (CONT..)
6
Example 2:
printf(“%lf”, salary);
Format output-placeholdervariable
Output:
PRINTF() FUNCTION (CONTINUED..)
7
Example 3:
printf(“Monthly salary is RM %lf”, salary);
Output format -Text
placeholder
variable
Output:
Monthly salary is RM
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
PRINTF() (FUNCTION CONTINUED..)
8
Example 4:
const float porosity = 16.78;printf(“The porosity of sand = %lf ”, porosity);
Output format -Text
The porosity of sand = 16.780001
Output:
constant
placeholder
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
PRINTF() FUNCTION (CONT..)
9
escape Character
function Additional formatting to be performed with the printf() function
Character Explanation\n New line
\t Tab
\a Beep sound
PRINTF() FUNCTION (CONT..)
10
Placeholder
function
A 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
Data type
Placeholder Output to be print
int %d Integer number
double %lf Floating number
float %f Floating number
char %c Character
char %s String
PRINTF() FUNCTION (CONT..)
11
char huruf1, huruf2;huruf1 = ‘B’;huruf2 = ‘P’;
printf(“%c%c%c”, ‘A’, ‘L’, ‘I’);printf(“\nI live in %c%c”,huruf1,huruf2);
Example 5 (Printing character value ) :
Output:
ALII live in BP
PRINTF() FUNCTION (CONT..)
12
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(“\nNumber %d and %d”,number2,number1);
Example 6 (Printing integer value) :
Output:My house number is 26
Number 906 and 10Number 2 and 15
PRINTF() FUNCTION (CONTINUED..)
13
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.142The value of sahih1 is 23.46 The value of sahih2 is 15.78
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
PRINTF() FUNCTION (CONT..)
14
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 multiplication result of x and y is 7.75cm
SCANF() FUNCTION
15
scanf() To accept input
To read input from keyboard
scanf(“input_format”,&variable_list) ;
scanf(“%d”,&nom) ;
scanf(“%s %d %lf”,&name,&age,&weight) ;
function
format
example
SCANF() FUNCTION (CONT..)
16
placeholder
output_list variableconsist
input_formatconsists example
%d @ %c @%lf
constantexpression function
Not allowed!!!
‘&’ symbolKnown as ampersand
Variable address/location reference
SCANF() FUNCTION (CONT..)
17
Example1:
scanf(“%lf %d %c”,&float_num, &num,&letter);
Placeholder
Every variables to be used in the program MUST be declared!!!
Variable
The above statement is valid!
Warning!!!!!
SCANF() FUNCTION (CONT..)
18
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() FUNCTION (CONT..)
19
Example 3(program code):
#include <stdio.h>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);}
?
?
?Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
SCANF() FUNCTION (CONT..)
20
Output:
Enter the semester : 3Enter your CPA : 3.45Your CPA for semester 3 is 3.45
SCANF() FUNCTION (CONTINUE..)
21
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);
OTHER I/O FUNCTIONS
22
Similar functions used in the input and output operations in C programming such as:
getchar() and putchar() functiongetch() dan putch() functiongets() dan puts() function
OTHER I/O FUNCTIONS
23
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
24
putchar()To display output in a single
character value
putchar(variable);
printf(“The input letter is ”);putchar(letter);
function
format
example usage
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OTHER I/O FUNCTIONS
25
#include <stdio.h>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
OTHER I/O FUNCTIONS
26
#include <stdio.h>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 491
However, it can also be program like this :
OTHER I/O FUNCTIONS
27
getch()
To read an input and terminate the program automatically
variable= getch();
char letter;printf(“Enter one letter >>”);letter= getch();
function
format
example
getch() have same function like getchar(), but different action
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OTHER I/O FUNCTIONS
28
#include <stdio.h>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 onceany input is read
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OTHER I/O FUNCTIONS
29
putch()
To display output , have similar function like
putchar
putch (variable);
printf(“The input letter is ”);putch(letter);
function
format
example
OTHER I/O FUNCTIONS
30
gets()To read string input from input device
gets(variable);
char name[20];printf(“Enter your name >>”);gets(name);
function
format
example
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OTHER I/O FUNCTIONS
31
#include <stdio.h>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
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OTHER I/O FUNCTIONS
32
#include <stdio.h>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 thedata including space between two words (if any)
Enter your name: Siti AminahYour name is Siti Aminah
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OTHER I/O FUNCTIONS
33
puts() To display string output to output device
puts (variable);
printf(“Your name is ”);puts(name);
functions
format
example
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OUTPUT FORMATTING
34
Integer formatting
To format the display of an integervalue
%spaced
Use the integer formatting in theoutput statement
functions
format
Where to use?
How it works?• Provide spaces according to formatting• Put the value to display starting from right to left side• If there is – sign in the formatting, values will be printed starting from left to right side
35
Output Formatting
Integer formatting
exampleprintf(“%3d”,123);
output 123
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
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
OUTPUT FORMATTING
36
#include <stdio.h>
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: Integer formatting
Output?
OUTPUT FORMATTING
37
Floatingnumber
formatting
To format the display of the floatingnumber value
%spacef
Use in the output statement
functions
format
Where to use?
• Provide spaces according to formatting• Put the value to display starting from right to left side• If there is – sign in the formatting, values will be printed starting from left to right side
How it works?
OUTPUT FORMATTING
38
Floating number formattingformat %spacef
example printf(“%10f”,57.78);
output _57.780000
example printf(“%15f”,57.78);
output _ _ _ _ _ _57.780000
exampleprintf(“%-10f”,57.78);
output 57.780000_
example printf(“%15f”,57.78);
output 57.780000_ _ _ _ _ _
39
Output formatting
Floating point exampleformat %f
example printf(“%.3f”,57.78);
output 57.780
printf(“%.2f”,57.78);output 57.78
printf(“%.1f”,57.78);output 57.8
printf(“%6.2f”,57.78);output 57.78_
printf(“%-6.2f”,57.78); _57.78output
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
example
example
example
example
40
Output formatting
String formatformat %<space>c
char huruf = ‘A’;printf(“%c”,huruf);printf(“%2c”,huruf);printf(“%-5c”,huruf);
A_AA_ _ _ _
output
Hazalila Kamaludin, Faculty of Information Technology and Multimedia, 2008/2009
example
41
Format Output (cont..)
String formattingformat %<spaces> s
char ayat[] =“KUITTHO PARIT RAJA”printf(“%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
QUICK RECAPConversion specifier Description
%d Read an optionally signed decimal integer
%i Read an optionally signed decimal
%o Read an octal integer
%u Read an unsigned decimal integer
%x Read a hexadecimal integer
%e,%f,%g Read a floating-point value
%l Place before any floating-point specifiers to indicate that a double or long double value is to be input
%c Read a character
%s Read a string
Table 1- Conversion specifiers for scanf
PRINTING INTEGERS
An integer is a whole number, such as 776, 0, or -52 that contains no decimal point
Table 1- Integer conversion specifiers
Conversion specifier
Description
%d Display as a signed decimal integer
%o Display as an unsigned octal integer
%u Display as an unsigned decimal integer
%x Display as an unsigned hexadecimal integer
PRINTING FLOATING-POINT NUMBERS
Floating-point value contains a decimal point as in 33.5, 0.0 or -657.983
Conversion specifier
Description
%e Display a floating-point value in exponential notation
%f Display floating-point values in fixed-point notation
%g Display a floating-point value in either the floating-point form f or the exponential form e
%l Place before any floating-point conversion specifier to indicate that a long double floating-point value is displayed
Table 3- floating-point conversion specifies
symbolic ASCII symbolic ASCII
\0 null \a alert (bell)
\b backspace \v Vertical tab
\n New line \t Horizontal tab
\r Carriage return \\ or \* *\ comment
45
CHARACTER CONSTANTS