Tải bản đầy đủ (.pdf) (14 trang)

Bài giảng Tin học đại cương A (dành cho khối tự nhiên):Input and Output

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (658.79 KB, 14 trang )

Input & Output
Nguyen Dung
Faculty of IT - College of Science


Functions I/O
stdio.h

Input:
scanf
gets
getch
getchar
Output
printf
puts
putch

scanf
printf
gets
puts


conio.h
getch
getchar
putch




Output - printf
List arguments

int printf (const char *format [,argument,…])

string (plain text)

Format string
format specifier

%[flags][width][.prec]type_char


%[flags][width][.prec]

type_char

Code

Out put

d

Integer number

u

Unsigned integer

ld


Long integer

lu

Unsigned long integer

o

Octal

x, X

Hexa

f

Real number: float, double

E, e

Real number: float, double

G, g

Real number: floar, double

c

Character: char


s

String: array char

p

Address

Different???


%[flags][width][.prec]

type_char

int x = 5;
float y = 10;
printf(“x = %d, y = %f, z = %d”, x, y, x + y);

x = 5, y = 10, z = 15


%[flags][width][.prec]type_char
Flag

Description

-


Align left

+

Display sign of number

white space

Display sign of negative number

None

Align right

Width

Description

Precision

Description

n

Minimum number of characters
to be printed

.number

The minimum number of

digits to be written

*

Get from parameter

.*

Get from parameter

None

Depend on number input

None

Depend on number input


%[flags][width][.prec]type_char
int x = -45;
int y = 15;
printf("x = %d\n",x);
printf("y = %d\n",y);
printf("y = %+d\n",y);
printf("x = %5d\n",x);
printf("x = %-5d\n",x);
printf("y = %5d\n",y);

x

y
y
x
x
y

=
=
=
=
=
=

-45
15
+15
-45
-45
15


%[flags][width][.prec]type_char

int x = -145;
float y = -10.13596;
printf("x = %010d\n",x);
printf("y = %7.2f\n",y);
printf("y = %12.5f\n",y);
printf("-y= %012.5f\n",-y);
printf("y = %-3.3f\n",y);

printf("y = %*.*f\n",8,3,y);

x = -000000145
y = -10.14
y =
-10.13596
-y= 000010.13596
y = -10.136
y = -10.136


Output – putch, puts
int putch(int ch)
int puts(const char *s)

char ch=„a‟;
putch(ch);
putch(68);
puts(“hello world!”);

a
D
helloworld!
_


In - scanf
int scanf(const char *format [, address,…]);

int x,y;

printf(“Enter x,y: ");
scanf("%d %d",&x,&y);
printf("x = %d\ny = &d",x,y);

Enter x, y: 10_4
x = 10
y = 4_


In - gets
gets(char *s)

Micheal Balack
Name is: Micheal Balack

char name[30];
gets(name);
printf(“Name is: %s”,name);
But
Micheal Balack
Name is: Micheal
char name[30];
scanf(“%s”,name);
printf(“Name: %s”,name);


In – getch, getchar
int getchar(void);
int getch(void);
Input a character: c

Character is entered: c

int ch;
printf(“Input a character: ”);
ch = getchar();
printf(“Character is entered: %c”,ch);
Different:

getchar – display the character entered.
getch – don‟t display.


Exercises
Available:
nguyendung622.wordpress.com
Dealline: 19h before the

learning day of next week.
Send to:


The end



×