c语言scanf可以和putchar,C语言getchar和putchar和scanf函数_缓冲区

C语言getchar和putchar和scanf函数_缓冲区

1.getchar

int getchar ( void );

Get character from stdin

Returns the next character from the standard input ( stdin).

It is equivalent to calling  getcwith  stdinas argument.

Parameters

(none)

Return Value

On success, the character read is returned (promoted to an int value).

The return type is int to accommodate for the special value  EOF, which indicates failure:

If the standard input was at the  end-of-file, the function returns  EOF and sets the  eof indicator (feof) of stdin.

If some other reading error happens, the function also returns  EOF, but sets its  error indicator (ferror) instead.

EOF:http://www.cplusplus.com/EOF

2.putchar

int putchar ( int character );

Write character to stdout

Writes a  character to the  standard output ( stdout).

It is equivalent to calling  putcwith  stdoutas second argument.

Parameters

character

The int promotion of the character to be written.

The value is internally converted to an unsigned char when written.

Return Value

On success, the  character written is returned.

If a writing error occurs,  EOFis returned and the  error indicator ( ferror) is set.

3.C语言中EOF:

EOF:在C语言中,或更精确地说成C标准函数库中表示文件结束符(end of file)。在while循环中以EOF作为文件结束标志,这种以EOF作为文件结束标志的文件,必须是文本文件。在文本文件中,数据都是以字符的ASCII代码值的形式存放。我们知道,ASCII代码值的范围是0~255,不可能出现-1,因此可以用EOF作为文件结束标志。

4.C语言中的回车和换行:

在C语言里回车和换行是两个概念,回车是指光标由行中任意位置移动到行首,换行指换到下一行的情况。

\n  :  回车换行

\r  :  回车(不换行)

\b  :  光标向左退一个格

#include 

int main ()

{

printf("sdsdsdsdsd\r");

printf("111\n");

printf("222222222222222");

return 0;

}

运行结果:

111dsdsdsd

222222222222222

Process returned 0 (0x0)   execution time : 0.191 s

Press any key to continue.

5.getchar和putchar的使用

#include 

int main ()

{

int c;

puts ("Enter text. Include a dot ('.') in a sentence to exit:");

do {

c=getchar(); //每个getchar()依次读入一个字符

putchar (c);

} while (c != '.');

return 0;

}

运行结果:

Enter text. Include a dot ('.') in a sentence to exit:

abcdefg.

abcdefg.

Process returned 0 (0x0)   execution time : 8.791 s

Press any key to continue.

输入abcdefg. 然后敲回车,输出abcdefg.

解释:键盘输入的字符都存到缓冲区内,一旦键入回车,getchar就进入缓冲区读取字符,一次只返回第一个字符作为getchar函数的值,如果有循环或足够多的getchar语句,就会依次读出缓冲区内的所有字符直到'\n '。要理解这一点,之所以你输入的一系列字符被依次读出来,是因为循环的作用使得反复利用getchar在缓冲区里读取字符,而不是getchar可以读取多个字符,事实上getchar每次只能读取一个字符。如果需要取消' \n'的影响,可以用getchar();来清除,这里getchar();只是取得了'\n '但是并没有赋给任何字符变量,所以不会有影响,相当于清除了这个字符。还要注意的是这里你在键盘上输入abcdefg.看到的回显正是来自于getchar的作用,如果用getch就看不到你输入了什么。

6.使用getchar()清除回车符

#include 

int main ()

{

int c;

while((c=getchar())!='a'){

putchar(c);

}

return 0;

}

结果:

sdsda

sdsd

Process returned 0 (0x0)   execution time : 6.000 s

Press any key to continue.

#include 

int main ()

{

int c;

while((c=getchar())!='a'){

putchar(c);

}

c = getchar(); //只读取一个字符,在这里读取的就是回车符

printf("%c",c);

return 0;

}

结果:读取回车符,然后输出换行,程序结束

sdsda

sdsd

Process returned 0 (0x0)   execution time : 3.616 s

Press any key to continue.

#include 

int main ()

{

int c;

while((c=getchar())!='a'){

putchar(c);

}

getchar(); //去除回车的影响

c = getchar(); //只读取一个字符

printf("%c",c);

return 0;

}

结果:输入sdsda回车后,输出sdsd,光标在这里闪烁,等待输入字符,输入了123后,读取第一个字符后输出,程序结束。

sdsda

sdsd123

1

Process returned 0 (0x0)   execution time : 9.656 s

Press any key to continue.

7.scanf函数

scanf不会把回车、空格赋给字符串但是会赋给字符,就如同getchar一样,这时就要考虑'\n '的存在了。

回车是一定要有的,不管getchar还是scanf只要是通过缓冲区输入数据的函数都是等待回车键'\n '出现才进入缓冲区的。

回车、空格等都有对应的ASCII码,所以用scanf输入字符时要小心这些东西被当成字符输进去,而输入字符串和整型、实型等数据时这些都被当成分隔符而不会被输入到字符数组或变量里。当然如果输入格式不是"%s%s"而是"%s,%s"分隔符就是逗号了。

摘自:http://www.cplusplus.com/reference/cstdio/scanf/

详细:http://blog.csdn.net/hackbuteer1/article/details/6704779

引用:http://blog.csdn.net/hackbuteer1/article/details/6704779

====END====

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值