C语言中的Input,Output

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型。如果 ab 都被成功读入,那么scanf的返回值就是 2
如果只有 a 被成功读入,返回值为 1
如果 ab 都未被成功读入,返回值为 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

解释:

符号意义
/bBackspace
/n换行
/r回车

参考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 1000
int 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() 则可以格式化输出。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值