C语言成长学习题(二)

六、编写程序,在scanf函数中指定输入数据的宽度。

1 #include <stdio.h>
2 
3 void main(void)
4 {
5     int a;
6     scanf("a=%3d", &a);
7     printf("a=%d
", a);
8 }

结果:

输入      输出

a=12     a=12

a=1234     a=123

12       a=(未知)

  可以在scanf函数的格式说明符前,用一个整数指定输入的最大宽度,但对实型数不可以指定宽度。

七、编写程序,从键盘输入三个数据12、A、34,分别存放在a、b、c中。

 1 #include <stdio.h>
 2 
 3 void main(void)
 4 {
 5     int a, c;
 6     char b;
 7 
 8     printf("Enter a, b, c: ");
 9     scanf("%d%c%d", &a, &b, &c);
10     printf("%d, %c, %d
", a, b, c);
11 }

结果:

Enter a, b, c: 12A34

12, A, 34

八、编写一个使用字符输入函数getch(getche)和字符输出函数putch的程序。

 1 #include <stdio.h>
 2 #include <conio.h>
 3 
 4 void main(void)
 5 {
 6     char ch;
 7 
 8     printf("Input two character:
");
 9     ch = getch();                                    //或者ch = getche();
10     putch(ch);
11     ch = getch();
12     putch(ch);
13 }

结果:

Input two character:

ab(使用getch()时输入的符号没有显示在屏幕上)

Mark:

getchar, getch, getche的区别
函数

是否需要回车

是否显示在屏幕上
getchar
getch
getche

 九、编写一个getch函数的应用程序。

1 #include <stdio.h>
2 #include <conio.h>
3 
4 void main(void)
5 {
6     printf("Let' study the C language.
");
7     printf("按任意键继续!
");
8     getch();
9 }

  "clrscr()"是清屏函数。

原文地址:https://www.cnblogs.com/zero-jh/p/5022722.html