You are on page 1of 30

Subject : COMP6047

ALGORITHM AND PROGRAMMING


Year : 2019

Formatted Input / Output


Learning Outcomes
At the end of this session, student will be able to:
• Use a standard library function related to input and output
operation in C (LO1 & LO2)

Printf : print format


%[type] : format specifier
Putchar : print character
Putch : print character
Putchar dan putch tidak dapat new line

Puts sudah bisa baris baru dan bisa string


Puts : put string

“” : buat string
‘’ : buat karakter

COMP6047 - Algorithm and Programming 2


Sub Topics
Formatted Input / Output:
- Output
- Input

COMP6047 - Algorithm and Programming 3


Start With Functions
• Standard library function, are predefined functions that C
compiler provides. Using them by point out the header files of
where to find those functions (include directive)

• Programmer-defined function, or user defined function are


functions that are created by the programmer/developer to be
used inside a program

COMP6047 - Algorithm and Programming 4


Output Operation
• To show data on the display screen/monitor. Some of
standard library function in C :
printf();
Printf : print format
putchar(); %[type] : format specifier
Putchar : print character
putch(); Putch : print character
Putchar dan putch tidak dapat new line

puts(); Puts sudah bisa baris baru dan bisa string


Puts : put string
etc. “” : buat string
‘’ : buat karakter

COMP6047 - Algorithm and Programming 5


Output Operation: printf function
• To display some data on the standard output, using
certain format

• Standard output is the monitor.

• Syntax:
printf(const char *format[,argument, …]);

• Header file : stdio.h

COMP6047 - Algorithm and Programming 6


Output Operation: printf() function
/* A first program in C */
#include <stdio.h>
void main()
{
printf (“Welcome to C!\n”);
}
/*Printing on one line with two printf statements*/
#include <stdio.h>
int main(void){
printf (“Welcome”);
printf (“to C!\n”);
return 0;
}
COMP6047 - Algorithm and Programming 7
Output Formatting
• Output also has formatted specification:
%[flags][width][.precision] type
width : number of columns provided type :
precision : digit number d –or- i : signed decimal
flags : o : unsigned octal
can be changed into: u : unsigned decimal
none : right justify x : unsigned hexadecimal
- : left justify f : floating point
- + : for positive & negative value
e : floating point (exponent)
c : single character
s : string
% : % character
p : pointer

COMP6047 - Algorithm and Programming 8


Output Formatting
• For long data type, add l at the front of data type:
– long double  ( “ %lf “)
– unsigned long int  ( “ %lu “)
– long int  ( “ %ld “)

COMP6047 - Algorithm and Programming 9


| : untuk batas spasi

Output Example
spasi

• Example 1:
printf (“%6d”, 34); ….34
printf (”%-6d”, 34); 34….

• Example 2
printf (“%10s”, “BINUS”); …..BINUS
printf (“%-10s”, “BINUS”); BINUS…..
printf (“%8.2f”, 3.14159 ); ….3.14
printf (“%-8.3f”, 3.14159 ); 3.141…

COMP6047 - Algorithm and Programming 10


Output Operation: putchar() function
• Syntax:
int putchar(int c)

• Functionality:
– Displaying character on the monitor at cursor position. After
display, cursor will move to the next position
– Return EOF if error, and return the displayed character after
successfully done
– putchar is a macro similar to : putc(c, stdout )
– Header File : stdio.h

• Example :
char ch=’A’;
putchar(ch);

COMP6047 - Algorithm and Programming 11


Output Operation: putch() function
• Syntax:
int putch(int ch)

• Functionality:
– Display ASCII character to the monitor without moving cursor to its
next position
– Return EOF if error, and return the displayed character after
successfully done
– Header file : conio.h

• Example :
char ch=’b’;
putch(ch); COMP6047 - Algorithm and Programming 12
Output Operation: puts() function
• Syntax:
int puts(const char *str);

• Functionality :
– Display string to the monitor and move the cursor to new line
– Return non-negative value when successful and EOF if error
– Header file: stdio.h

• Example :
puts(”Welcome”);
puts(”to Binus”);
Output on monitor:
Welcome
to Binus

COMP6047 - Algorithm and Programming 13


Input Operation
• Standard library function that is related to input operations are:
scanf();
getchar();
getch();
getche();
gets();
etc.

• Input operation: function/operation of getting the data into the


memory using standard I/O devices (keyboard, disk, etc.)

COMP6047 - Algorithm and Programming 14


Input Operation: scanf() function
• Header file: stdio.h
• Format:
int scanf( const char *format [,argument]... );
• All the argument type are pointers (address of a variable)
• To get the address of a variable use “&” sign
• Example :
int aValue;
scanf(”%d”,&aValue);

• Input format: ”%type”


where type can be substituted with one of the following list:
(next page)

COMP6047 - Algorithm and Programming 15


Input Operation: scanf() function
• Format Type:
Type Used to scan
d integer
u unsigned integer
x hexadecimal
e, f, g floating point
c single character
s string ended with white space
o data unsigned octal
[…] string ended with non of the value inside [...]
[^..] string ended with the value inside [...]

%[^ \n] Scan sampe enter


%[ a] scan hanya a saja
COMP6047 - Algorithm and Programming 16
Input Operation: scanf() function
• If exist an x integer variable, state the difference of x and &x?
Variable Name Address Value
X 45678 234

Answer:
x : 234
&x : 45678

COMP6047 - Algorithm and Programming 17


Input Operation: scanf() function
• scanf() function returns an integer that stated how many fields
are successfully assigned

• Example :
int x,y,z,w;
x = scanf("%d %d %d",&y,&z,&w);

– Input three values of integer 6 7 8, then x = 3;


– Input four values 6 7 8 9 then x = 3 (successfully assign
3 variables y z w)

COMP6047 - Algorithm and Programming 18


Input Operation: scanf() function

scanf()
function
can use
/* Program Calculating rectangle area v2*/ more than
#include <stdio.h> one
int main(){ argument
int width, height, area;
scanf(“%d %d”,&width, &height);
area = width * height;
return(0);
}
COMP6047 - Algorithm and Programming 19
Input Operation: scanf() function
/* Program different argument*/

#include <stdio.h>
int main()
{
int number; char initial; float money;
scanf(“%d %c %f” ,&number, &initial, &money);
Data type
//other statements
for each
return(0); variable in
} the function
argument
can be
different

COMP6047 - Algorithm and Programming 20


Input Operation: scanf() function
• Getting string data from keyboard using scanf() using format:
%s
• Example :
char ss[40];
scanf(”%s”, ss);
• Note for the example above, as the ss variable is a pointer then
we need not putting extra & sign (&ss) in the function
argument
(pointer will be discussed later separately)
• String takes only till the first whitespace found

COMP6047 - Algorithm and Programming 21


Input Formatting
• Space char, tab, linefeed, carriage-return, form-feed, vertical-
tab, and new-line entitle ”white-space characters”

• Example :
– Using previous example, if a string “good
morning every one” entered then ss value will
only contain “good”

COMP6047 - Algorithm and Programming 22


Input Formatting & Example
• To get string that ended with certain character for example
Enter, use scanf() with format: [^\n]

• Example:
char ss[40];
scanf(”%[^\n]”,ss);

• Using the previous example, if a string “good morning every


one” then ENTER, the ss variable will contain “good morning
every one”

COMP6047 - Algorithm and Programming 23


Input Operation: getchar() function
• Syntax:
int getchar(void);

• Functionality:
– Return the next ASCII character from keyboard buffer
– Shown on the monitor screen
– Awaiting for ENTER pressed
– Header file: stdio.h

• Example :
char ch;
ch = getchar();

COMP6047 - Algorithm and Programming 24


Input Operation: getch() function
• Syntax:
int getch(void);

• Functionality:
– Return the next ASCII character from keyboard buffer
– Does not show on the monitor screen
– Does not wait for ENTER pressed
– Header file: conio.h

• Example :
char ch;
ch = getch();

COMP6047 - Algorithm and Programming 25


Input Operation: getche() function
• Syntax:
int getche(void);

• Functionality:
– Return the next ASCII character from keyboard buffer
– Shown on the monitor screen
– Does not wait for ENTER pressed
– Header file: conio.h

Akan langsung mengprint seperti search engine google


• Example :
char ch;
ch = getche();

COMP6047 - Algorithm and Programming 26


Input Operation: gets() function
• Syntax:
char *gets(char *buffer)

• Functionality :
– read a string from keyboard till find new-line and save in buffer
– new-line will later on replaced with null character
– will return NULL if error and return its argument (buffer) if success

• Example :
char buffer[40];
char *ptr;
ptr = gets(buffer);

COMP6047 - Algorithm and Programming 27


Summary
• Syntax for Output:
printf, putchar, putch, puts
• Syntax for Input:
scanf, getchar, getch, getche, gets

COMP6047 - Algorithm and Programming 28


References
• Paul Deitel & Harvey Deitel. (2016). C how to program : with an
introduction to C++. 08. Pearson Education. Hoboken. ISBN:
9780133976892. Chapter 9
• Reading from and Writing to Standard I/O:
http://aelinik.free.fr/c/ch05.htm
• Intro to File Input/Output in C:
http://www.cs.bu.edu/teaching/c/file-io/intro/

COMP6047 - Algorithm and Programming 29


END

COMP6047 - Algorithm and Programming 30

You might also like