if im going to enter some elements into an array, after entering the total number of elements(size of array). And im wandering the following code of (char test [80]), is the elements size it can carry or the maximum array size. Wish someone can explain to me how strlen work and wat does the "n" represent. thank you.
1 2 3 4 5 6 7 8
char test[80];
char choice;
int n;
printf("String to sort>> ");
gets(test);
n=strlen(test);
n is a integer variable which will hold the result returned from the strlen function.
Re: strlen - quote from this website:
The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without including the terminating null character itself).
The parameter passed to strlen() is a pointer to the start of the array or string. The function simply tests each successive character until it finds a zero. C-strings are represented as an array of characters terminated by a null (zero) byte. Thus the result from strlen is not related to size of the array - if the null terminator is missing, the function will continue to step through memory beyond the end of the array until it finds one.
example:
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <stdio.h>
#include <string.h>
int main()
{
char array[80] = { 'e', 'x', 'a', 'm', 'p', 'l', 'e', 0 };
printf ("Size of array: %d\n", sizeof(array));
printf ("String is: %s\n", array);
printf ("length of string: %d\n", strlen(array));
return 0;
}
output:
Size of array: 80
String is: example
length of string: 7