C语言中的Input,Output
来源:互联网 发布:淘宝裤子折叠方法图解 编辑:程序博客网 时间:2024/06/05 05:43
C|Input,Output
写在前面:
这是一篇习题练习总结,会有后续,其中的全部习题均来自GeeksforGeeks。
1.Scanf
问题.1:
下段代码的输出是什么
#include "stdio.h" int main() { char arr[100]; printf("%d", scanf("%s", arr)); /* Suppose that input value given for above scanf is "GeeksQuiz" */ return 1; }
答案:1
解释:
scanf函数返回成功读入的数据项数,读入数据时遇到了“文件结束”则返回EOF。刚开始一直以为scanf函数返回成功读入的字符数,因此应该输出9。其实,scanf函数返回值是成功输入的参数个数。
/*scanf 函数声明*/ int scanf(const char *format, ...)
另一个例子:
scanf("%d %d",&a,&b);
函数返回值为int型。如果 a 和 b 都被成功读入,那么scanf的返回值就是 2;
如果只有 a 被成功读入,返回值为 1;
如果 a 和 b 都未被成功读入,返回值为 0;
如果遇到错误或遇到 end of file,返回值为 EOF。end of file为 Ctrl+z 或者 Ctrl+d。
问题.2
scanf("%4s", str);
解释:
格式命令可以说明最大域宽。 在百分号(%)与格式码之间的整数用于限制从对应域读入的最大字符数。
问题.3
/*input x y*/scanf("%c%c%c",&a,&b,&c);
解释:
2. 转义字符 /b,/n
问题:
#include <stdio.h> int main() { printf("\new_c_question\by"); printf("\rgeeksforgeeks"); getchar(); return 0; }
答案:Depends on terminal configuration
解释:
参考Stack Overflow上的一个问题:
\b and \r are rarely used in practice. I just found out that I misunderstood these two escape sequences. A simple test:
printf(“foo\bbar\n”);
I expected it to output fobar, because \b will backspace the cursor, and b will overwrite the second o, but instead it outputs: foobar
The same is with \r:
printf(“foo\rbar\n”);
I thought \r will move the cursor to the beginning of the current line, so bar will replace foo, so the final output should be bar. However, it actually outputs:
foo
bar最佳答案:
The characters will get send just like that to the underlying output device (in your case probably a terminal emulator).
It is up to the terminal’s implementation then how those characters get actually displayed. For example, a bell (\a) could trigger a beep sound on some terminals, a flash of the screen on others, or it will be completely ignored. It all depends on how the terminal is configured.Usage of \b and \r in C
3.printf
问题:
#include <stdio.h>// Assume base address of "GeeksQuiz" to be 1000int main(){ printf(5 + "GeeksQuiz"); return 0;}
答案:Quiz
解释:
编译器在字符串 “GeeksQuiz”的基地址上加 5 ,然后将字符串”Quiz” 作为参数返回。
printf的返回值:
On success, the total number of characters written is returned.
If a writing error occurs, the error indicator (ferror) is set and a negative number is returned.
If a multibyte character encoding error occurs while writing wide characters, errno is set to EILSEQ and a negative number is returned.
printf C++ Reference
问题:
#include <stdio.h>int main(){ printf("%c ", 5["GeeksQuiz"]); printf("%c ", "GeeksQuiz"[5]); return 0;}
答案:Q Q
解释:
关键在于表达式 5[“GeeksQuiz”] 和 “GeeksQuiz”[5] ,这两个表达式均被编译器分解为 *(5 + “GeeksQuiz”),对指向字符串的头指针加5,即指向字符Q
问题:
include <stdio.h>int main(void) { int x = printf("GeeksQuiz"); printf("%d", x); return 0;}
答案:GeeksQuiz9
解释:
printf函数返回成功输入的字符数,”GeeksQuiz” 有9个字符。
4.gets
思考:
void read(){ char str[20]; gets(str); printf("%s", str); return;}
上面这段代码很简单,它从标准输入读取字符串,然后打印字符串,但当时用 gets() 时,会产生缓冲区溢出,gets() 不做任何数组界限检验,gets会一直读取输入,直到遇到新的行,为了避免缓冲区溢出,应该使用 fgets() 代替 gets(),fgets() 会确保读取的字符数不超过 MAX_LIMIT 。
其他
在”stdio.h”中哪个函数可以替代printf()?
fprintf().尽管 fputs() 和 fwrite() 可以接受从标准输入的文件流,但输出流是无格式的,而 fprintf() 则可以格式化输出。
- C语言中的Input,Output
- HDOJ中的A+B for Input-Output Practice (I) java语言
- C语言 杭电POJ 1089 A+B for Input-Output Practice (I)
- C语言 杭电POJ 1090 A+B for Input-Output Practice (II)
- C语言 杭电POJ 1091 A+B for Input-Output Practice (III)
- C语言 杭电POJ 1092 A+B for Input-Output Practice (IV)
- C语言 杭电POJ 1093 A+B for Input-Output Practice (V)
- C语言 杭电 POJ 1094 A+B for Input-Output Practice (VI)
- C语言 杭电POJ 1095 A+B for Input-Output Practice (VII)
- C语言 杭电POJ 1096 A+B for Input-Output Practice (VIII)
- 理工大学ACM平台题答案关于C语言 1014 A+B for Input-Output Practice (V)
- 理工大学ACM平台题答案关于C语言 1010 A+B for Input-Output Practice (I)
- 理工大学ACM平台题答案关于C语言 1016 A+B for Input-Output Practice (VII)
- 理工大学ACM平台题答案关于C语言 1017 A+B for Input-Output Practice
- 理解Socket通信中的Input和Output
- Warm up:C input and output
- input/output
- 山东理工大学ACM平台题答案关于C语言 1012 A+B for Input-Output Practice (III)
- 人证合一系统,人脸识别与OCR识别相结合
- 【考研】第一周总结
- 第一周总结
- 设计模式(二)
- Python
- C语言中的Input,Output
- 训练总结 9.3
- [Leetcode]_41 First Missing Positive
- 二叉树
- 关于自己定义一个图片启动app做广告。
- Python 日期和时间
- 基础代码学生管理系统IO版本代码实现
- Tomcat做一个简单的jsp(和Myeclipse)
- 阿里x-paxos基础库