bufdemo.c代码解释

博客介绍了一段代码的功能为输入输出字符串,展示了代码块并给出测试结果。当字符串长度小于4时正常输出,超过数组长度时,puts函数中printf遇‘\n’才结束,计算机不检测数组长度也可输出。

代码功能

输入输出字符串。

代码块

#include <stdio.h>
#include <stdlib.h>

/* Implementation of library function gets() */
char *gets(char *dest)//定义指针函数gets
{
  int c = getchar();
  char *p = dest;   //定义一个指针变量p,p的内容为dest地址。
  while (c != EOF && c != '\n') {
    *p++ = c;
    c = getchar();
  }
  *p = '\0';
  return dest;
}

/* Read input line and write it back */
void echo()
{
    char buf[4];  //定义一个定长数组,数组长度为4
    gets(buf);
    puts(buf);//输出字符串,puts函数中使用了printf函数
}

void call_echo() 
{
    echo();  
}

/*void smash() 
{
    printf("I've been smashed!\n");
    exit(0);
}
*/

int main()
{
    printf("Type a string:");
    call_echo();
    return 0;
}

测试结果

在这里插入图片描述

测试结果解释

当字符串的长度<4时,正常输出,超过数组长度时,puts函数中printf遇到‘\n’才结束,计算机不会检测数组长度,所以也可以输出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值