I read in a char array with scanf and want to check if the length is bigger than 15.
It works only sometimes. (If not I get an error --> core dumped.)
My Code:
#include <stdio.h>
int checkArray(char string[], int length) {
int i;
for(i=0; string[i] != '\0'; i++);
if(i > length) {
return -1;
}
return 0;
}
int main ()
{
const int length = 15;
char x[15];
scanf("%s", x);
if(checkArray(x, length) == -1) {
printf("max. 15 chars!");
return 1;
}
return 0;
}