重新学习C语言 ----- getchar()&&&&putchar()

该函数声明在stdio.h头文件中,使用的时候要包含stdio.h头文件

getchar有一个int型的返回值.当程序调用getchar时.程序就等着用户按键.用户输入的字符被存放在键盘缓冲区中.直到用户按回车为止(回车字符也放在缓冲区中).当用户键入回车之后,getchar才开始从stdin流中每次读入一个字符.getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕.如用户在按回车之前输入了不止一个字符,其他字符会保留在键盘缓存区中,等待后续getchar调用读取.也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键.

函数名: getchar
功  能: 从stdin流中读字符
用  法: int getchar(void);
程序例:

1 #include <stdio.h>
2
3  int main(void)
4 {
5 int c;
6
7 /* Note that getchar reads from stdin and
8 is line buffered; this means it will
9 not return until you press ENTER. */
10
11 while ((c = getchar()) != '\n')
12 printf("%c", c);
13
14 return 0;
15 }

  功能:::::输入字符的下一个字符,程序例:

View Code
1 #include<stdio.h>
2 #include<stdlib.h>
3  int main()
4 {
5 char ch;
6 ch=getchar();
7 while(ch!='\n')
8 {
9 putchar(ch+1);
10 ch=getchar();
11 }
12 putchar(ch);
13 system("pause");
14 return 0;
15 }

函数名: putchar
功  能: 在stdout上输出字符
用  法: int putchar(int ch);
程序例:

1 #include <stdio.h>
2
3  /* define some box-drawing characters */
4  #define LEFT_TOP 0xDA
5  #define RIGHT_TOP 0xBF
6  #define HORIZ 0xC4
7  #define VERT 0xB3
8  #define LEFT_BOT 0xC0
9  #define RIGHT_BOT 0xD9
10
11  int main(void)
12 {
13 char i, j;
14
15 /* draw the top of the box */
16 putchar(LEFT_TOP);
17 for (i=0; i<10; i++)
18 putchar(HORIZ);
19 putchar(RIGHT_TOP);
20 putchar('\n');
21
22 /* draw the middle */
23 for (i=0; i<4; i++)
24 {
25 putchar(VERT);
26 for (j=0; j<10; j++)
27 putchar(' ');
28 putchar(VERT);
29 putchar('\n');
30 }
31
32 /* draw the bottom */
33 putchar(LEFT_BOT);
34 for (i=0; i<10; i++)
35 putchar(HORIZ);
36 putchar(RIGHT_BOT);
37 putchar('\n');
38
39 return 0;
40 }
41
42
posted @ 2011-06-14 23:30  左手心_疼  阅读(305)  评论(0编辑  收藏  举报