What is a way in C that someone could find the length of a character array?
I will happily accept pseudo-code, but am not averse to someone writing it out if they'd like to :)
first, the char variable is defined in charType and the char array in arr. Then, the size of the char variable is calculated using sizeof() operator. Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.
The first - arr object size is printed to be 24 bytes because the strlen function iterates through a char array until the terminating null byte is encountered.
printf ( "Size of String is %lu\n" , sizeof (str)); } Output: Length of String is 8 Size of String is 9. Since size of char in C is 1 byte but then also we find that strlen() gave one less value than sizeof().
The simplest procedural way to get the value of the length of an array is by using the sizeof operator. First you need to determine the size of the array. Then you need to divide it by the size of one element. It works because every item in the array has the same type, and as such the same size.
Provided the char array is null
terminated,
char chararray[10] = { 0 }; size_t len = strlen(chararray);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With