Why does the output in C-code begin by counting the index of the array from the number 1, and not from 0?

So, I have this line in C code, which I got from a book that has a quote, and then another line that has one word from this quote. When he tells the program to find the position of the substring, it starts the count from number 1, not 0. Why is this? Here is what I mean:

#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 } 

So this will say: Index found at number 3

But shouldn't you print the index found at number 2 because you are starting from scratch? Or you can sometimes start with number 1 ??!

+7
c
source share
3 answers

Yes, it always starts with 0, the space is also considered a symbol here, so the output is 3, not 2.

+3
source share

No, it starts from scratch:

 No Time... ^^^^ 0123 
+16
source share

Do not confuse index and length . index and length different.

 char[] str = "stackoverflow"; 

The length of str will return 13 .

Index 'c' in str will return 3 .

+1
source share

All Articles