Document

advertisement
[email protected]
Advanced Input, Process
and Output Commands
Session 3
Input – Output Command
[email protected]
Input
 scanf(“%d”,&a);
 gets(nm_mhs);
 sex = getch();
Output
 printf(“%d”,a)
 puts(“Nama Anda : “+nm_mhs);
 putchar(sex);
Format Specifier
[email protected]
%d is called Format Specifier
Use of determining the format is associated
with the type of data to be printed, meaning
each has a data type determines the format of
each. The following table is a table determines
the format for each type of data.
Table of scanf Format Specifier
[email protected]
No
Tipe Data
1
Integer
2
Floating Point
Format Specifier for
scanf()
%d

Bentuk Desimal
%e atau %f

Bentuk Berpangkat
%e atau %f
3
Double Precision
%lf
4
Character
%c
5
String
%s
6
Unsigned Integer
%u
7
Long Integer
%ld
8
Long unsigned integer
%lu
9
Unsigned Hexadecimal Integer
%x
10
Unsigned Octal Integer
%o
Table of printf Format Specifier
[email protected]
No
Data Types
1
Integer
2
Floating Point
Format Specifier for
printf()
%d

Bentuk Desimal
%f

Bentuk Berpangkat
%e

Yang lebih pendek antara Desimal dan Berpangkat
%g
3
Double Precision
%lf
4
Character
%c
5
String
%s
6
Unsigned Integer
%u
7
Long Integer
%ld
8
Long unsigned integer
%lu
9
Unsigned Hexadecimal Integer
%x
10
Unsigned Octal Integer
%o
Escape Sequences
[email protected]
Character combinations consisting of a
backslash (\) followed by a letter or by a
combination of digits are called "escape
sequences." To represent a newline character,
single quotation mark, or certain other
characters in a character constant, you must
use escape sequences. An escape sequence is
regarded as a single character and is therefore
valid as a character constant.
Escape Sequences
[email protected]
Escape sequences are typically used to specify
actions such as carriage returns and tab
movements on terminals and printers. They
are also used to provide literal
representations of nonprinting characters and
characters that usually have special meanings,
such as the double quotation mark ("). The
following table lists the ANSI escape
sequences and what they represent.
Table of Escape Sequences
[email protected]
Escape Sequence
Represents
\a
\b
\f
\n
\r
\t
\v
\'
\"
\\
\?
Bell (alert)
Backspace
Formfeed
New line
Carriage return
Horizontal tab
Vertical tab
Single quotation mark
Double quotation mark
Backslash
Literal question mark
\ooo
ASCII character in octal notation
\xhh
ASCII character in hexadecimal notation
\xhhhh
Unicode character in hexadecimal notation if this escape sequence is
used in a wide-character constant or a Unicode string literal.
For example, WCHAR f = L'\x4e00' or WCHAR b[] = L"The Chinese
character for one is \x4e00".
Exercise 1 scanf()
[email protected]
1.
2.
3.
4.
5.
6.
7.
8.
9.
/*
Syntax :
scanf(“format specifier",&variabelname);
*/
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{ //deklarasi variabel
10.
11.
12.
13.
14.
15.
16.
clrscr();
//input
printf("Nama Mahasiswa
printf(“Jenis Kelamin [L/P]
printf(“Tinggi Badan
printf(“Tahun Lahir
//proses cari umur
17.
//cetak output
18.
19.
getch();
}
: ");
: ");
: ");
: “);
Exercise 2 printf()
[email protected]
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
/* Contoh Penggunaan Escape Sequence & Penentu Format
\t = tabulasi, \n = pindah baris
%6.2f = 6 digit 2 desimal untuk format float */
#include <stdio.h>
#include <conio.h>
void main()
{
float bil1=3.14,bil2=8.50,bil3=88.0;
float bil4=13.7,bil5=70.80,bil6=100.45;
clrscr();
printf("\'NKR\' Singkatan dari \"Naya Kartika Ramadhani\" \n\n");
printf("\n Tekan Enter...!\n\n");getch();
printf("Escape Sequence tab(\\t) \n");
printf("------------------------\n");
printf("%6.2f \t%6.2f \t%6.2f \n",bil1,bil2,bil3);
printf("%6.2f \t%6.2f \t%6.2f \n",bil4,bil5,bil6);
printf("------------------------\n");
getch();
}
Task 3
[email protected]
Create a program to calculate the sum and difference of
two integers, then show the sum and difference was to
use wide-format field = 8.
Input data : number 1, number 2
Data output : total, difference
The desired view as follows:
Enter Number 1
: ... ...
Enter Number 2
: ... ...
The number between ... and ... are
The difference between ... and ... are
: ... ...
: ... ...
Download