输入:scanf、gets()、getchar()
用scanf输入字符串时,识别到空格或者回车自动结束。
当我们需要输入空格时,就需要用gets()函数,在调用gets()函数输入时,遇到空格会将空格输入,遇到回车输入结束。
getchar在C语言中,getchar()函数用于从标准输入(通常是键盘)读取一个字符。
输出:printf,puts,putchar
用printf输出时,我们往往需要格式化输出(但是printf本来接受的就是一个地址,我们也可以不按照%s的方式输出,直接传入地址)
输出字符串的时候,我们也可以直接用puts,简化输出的格式。
(不管用printf还是puts,接受的都时指向字符的指针)
int main()
{
printf("hello world\n");
puts("hello world\n");
char *p = "hello world";//字符指针接收字符串首元素的地址
char arr[] = "hello world";//把字符串放到字符型数组中
printf(p);
printf("\n%s\n", p);
printf(arr);
printf("\n%s\n", p);
puts(arr);
puts(p);
}
putchar()函数就是一次输出一个字符,接受的参数时整形(或者说字符类型,字符类型是以ASCII码值的形式存储的)
#include <stdio.h>
int main ()
{
char c;
for (c = 'A' ; c <= 'Z' ; c++)
putchar (c);
return 0;
}