Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the length of a Character Array in C

Tags:

arrays

c

strlen

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 :)

like image 328
not_l33t Avatar asked Nov 15 '10 01:11

not_l33t


People also ask

How do I find the length of a char array?

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.

What is the size of a character array in C?

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.

What is length of char in C?

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().

How does C know the length of an array?

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.


1 Answers

Provided the char array is null terminated,

char chararray[10] = { 0 }; size_t len = strlen(chararray); 
like image 63
Daniel A. White Avatar answered Sep 21 '22 07:09

Daniel A. White