【C语言】教材5.1 字符串拼接

avatar

azurekiln

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

int main() {
    int i=1, max=100, sum=0;
    char result[1000] = "";
    char buffer[10];
    for (i=1; i<=max; i++) {
      sum=sum+i;

      snprintf(buffer, sizeof(buffer), "%d", i);
      if (i==max) {
        strcat(result, buffer);
        strcat(result, "=");
      } else {
        strcat(result, buffer);
        strcat(result, "+");
      }
    }
    printf("%s%d",result,sum);
    return 0;
}

扫描二维码,在手机上阅读
收藏
请先 登录 再评论
powered by emlog pro
服务器供应商 景云数据

友情链接
白衣Ink


sitemap