"String" contains function in C This function checks whether or not a substring needle exists in another string haystack and returns the position if it does or 0 if it doesn't, unless the position is 0 in which case, it won't be located.

Looking for ways to improve this code, specifically better error handling.

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;

size_t contains(const char * needle, const char *haystack);

int main(void)
{
    char *needle = "test";
    char *haystack = "This is a dinosaurtest.";
    printf("Position: %lu", contains(needle, haystack));

    return EXIT_SUCCESS;
}

size_t contains(const char * needle, const char *haystack)
{
    if(needle == NULL || haystack == NULL)
    {
        return 0;
    }
    long int first_char_pos = -1;
    size_t len_h = strlen(haystack);
    size_t len_n = strlen(needle);
    size_t i, j;
    size_t exist_count = 0;

    // Find the first character. If it doesn't exist, we're done.
    for(i = 0; i &lt; len_h; i++)
    {
            if((haystack[i] == needle[0]) &amp;&amp; (first_char_pos == -1))
            {
                first_char_pos = i;
                exist_count++;
            }
    }

    if(first_char_pos == -1)
    {
        return 0;
    }
    printf("First char match index: %li\n", first_char_pos);
    printf("Char: %c\n", haystack[first_char_pos]);
    size_t current_index = (size_t) first_char_pos;
    for(i = first_char_pos; i &lt; len_h; i++)
    {
        if(haystack[i] == needle[exist_count] &amp;&amp; (i == (current_index + 1)))
        {
            current_index = i;
            exist_count++;
        }
        printf("Exist count: %lu\n", exist_count); //&lt;--Debugging
        if(exist_count == len_n)
        {
            return first_char_pos;
        }
    }
    return 0;
}
Just a couple of remarks:


You should add a newline after the last line:

  $ ./nh
  First char match index: 18
  Char: t
  Exist count: 1
  Exist count: 2
  Exist count: 3
  Exist count: 4
  Position: 18 $

I don't know what compiler you use but with when compiled with gcc and -Wall -Wextra -pedantic you get:

gcc -O2 nh.c -lm -o nh -Wall -Wextra -pedantic
nh.c: In function ‘contains’:
nh.c:25:15: warning: unused variable ‘j’ [-Wunused-variable]
 size_t i, j;
           ^

Code formatting should be more consistent. For example, in this line you put a whitespace before needle but don't put a whitespace before haystack:

size_t contains(const char * needle, const char *haystack);

%lu is not a portable specifier for size_t type, you should use %zu introduced in C99.
You said:



  returns the position if it does or 0 if it doesn't, unless the
  position is 0 in which case, it won't be located.


This is really not good. For example, with this it returns 0:

char *needle = "This";
char *haystack = "This is a dinosaurtest.";


With this, it also returns zero:

char *needle = "non-existent";
char *haystack = "This is a dinosaurtest.";


You can't tell the difference between success and failure in this two
examples. Actually, atoi() has the same problem. I don't know what
operating system you use but maybe you could use ssize_t as the
return type if it's available and return -1 in case of failure.