I've written the following code to reverse a string in C. The code seems to works properly and that's why I'm confused. Does anyone know why there is not an error here? I was expecting an array out of bounds or an infinite loop on the for loop, but it seems the loop breaks before it gets to negative values.
#include <stdio.h>
#include <string.h>
void reverse(char* str);
void reverse(char* str)
{
size_t len = strlen(str);
for(int i = (int)len-1; i<=len; i--)
{
printf("%c", str[i]);
}
}
int main (int argc, const char * argv[])
{
char string[] = {'h', 'e', 'l', 'l', 'o', '\0'};
reverse(string);
return 0;
}