700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > c语言scanf可以和putchar C语言getchar和putchar和scanf函数_缓冲区

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

时间:2024-01-14 02:53:45

相关推荐

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

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

1.getchar

intgetchar(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 anintvalue).

The return type isintto accommodate for the special value EOF, which indicates failure:

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

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

EOF:/EOF

2.putchar

intputchar(intcharacter);

Write character to stdout

Writes a characterto the standard output( stdout).

It is equivalent to calling putcwith stdoutas second argument.

Parameters

character

Theintpromotion of the character to be written.

The value is internally converted to anunsigned charwhen written.

Return Value

On success, the characterwritten 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

intmain()

{

printf("sdsdsdsdsd\r");

printf("111\n");

printf("222222222222222");

return0;

}

运行结果:

111dsdsdsd

222222222222222

Processreturned0(0x0)executiontime:0.191s

Pressanykeytocontinue.

5.getchar和putchar的使用

#include

intmain()

{

intc;

puts("Entertext.Includeadot('.')inasentencetoexit:");

do{

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

putchar(c);

}while(c!='.');

return0;

}

运行结果:

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

intmain()

{

intc;

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

putchar(c);

}

return0;

}

结果:

sdsda

sdsd

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

Press any key to continue.

#include

intmain()

{

intc;

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

putchar(c);

}

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

printf("%c",c);

return0;

}

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

sdsda

sdsd

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

Press any key to continue.

#include

intmain()

{

intc;

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

putchar(c);

}

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

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

printf("%c",c);

return0;

}

结果:输入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"分隔符就是逗号了。

摘自:/reference/cstdio/scanf/

详细:/hackbuteer1/article/details/6704779

引用:/hackbuteer1/article/details/6704779

====END====

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。