为什么C代码中的输出从数字1而不是0开始计算数组索引?

7 c

所以我在一个C代码中得到了这个字符串,我从一本书中有一个引用,然后是另一个字符串,其中有一个单词来自该引用.当它告诉程序找到子串的位置时,它从数字1开始计数而不是0.这是为什么?这就是我的意思:

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

int main()
{
    char str[]="No Time like the Present";
    char sub[]="Time";
    if (strstr(str, sub)== NULL)
    {
        printf("not found");
    }
    else
    {
        printf("Index number found at %d",strstr(str,sub)-str);
    }
    return 0
}
Run Code Online (Sandbox Code Playgroud)

所以它会说:索引在3号找到

但是不应该是在2号找到的打印索引,因为你从零开始?或者你有时可以从数字1开始??

tro*_*foe 16

不,它从零开始:

No Time...
^^^^
0123
Run Code Online (Sandbox Code Playgroud)


Nar*_*gis 3

是的,它总是从 0 开始,这里空格也算作一个字符,所以输出是 3 而不是 2。