C标准库函数'strncpy'无法正常工作

Tec*_*ing -1 c c-strings standard-library

C代码:

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

#define STRINGS 10
#define STR_LEN 20

int main(void)
{
    char words[STRINGS][STR_LEN];

    char input[STR_LEN];

    int i;
    int mycount;

    for(i = 0;i < STRINGS;++i;)
    {
        printf("Enter a word (or 0 to quit)\n:");

        scanf("%19s", input);

        if(input[0] == '0') break;

        strncpy(words[i], input, STR_LEN);

        mycount++;
    }

    printf("A total of %d strings were entered!\n",mycount);

}
Run Code Online (Sandbox Code Playgroud)

问题:当我运行此代码并输入一些字符串时,它不会打印出我输入的字符串数量

在此输入图像描述

Jay*_*266 10

你需要将mycount初始化为0.

int mycount =0;
Run Code Online (Sandbox Code Playgroud)