Provided the char array is null terminated,

char chararray[10] = { 0 };
size_t len = strlen(chararray);
Answer from Daniel A. White on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ find-the-length-of-character-array-in-c
Find the Length of Character Array in C - GeeksforGeeks
July 23, 2025 - In C, a character array is a collection ... length of a character array is defined as the total number of characters present in the array, excluding the null character ('\0') at the end....
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 149823-how-get-length-char-array.html
How to get the length of char array
C doesn't give you a way to dynamically find the length of an array. your code has to keep track of lengths itself. thats why things like memcpy etc have a length parameter. as manasij said, you can get the length of an array statically if it is declared using the [] syntax. but if you malloc one for example you have to keep track yourself. ... if you have 0's in your array, then its not a string and strlen is not the function to use. What? Isn't that the actual definition of a string in C? A string in C is a '\0' terminated character array.
๐ŸŒ
Quora
quora.com โ€บ How-do-you-get-the-size-of-a-char-array-in-C
How to get the size of a char array in C - Quora
Answer (1 of 12): Find the length of a char array using the sizeof operator. Regardless of the data type of an element, the size of operator can be used to calculate an array's size.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ length of char array in c
How to Get Length of Char Array in C | Delft Stack
February 22, 2024 - In the output, arr contains 7 elements, while arr2 holds 17 elements, including the null terminator automatically added when initialized with a string literal. This illustrates the effectiveness of using the sizeof operator to accurately determine ...
๐ŸŒ
Cplusplus
cplusplus.com โ€บ reference โ€บ cstring โ€บ strlen
Cplusplus
For example: char mystr[100]="test string"; defines an array of characters with a size of 100 chars, but the C string with which mystr has been initialized has a length of only 11 characters. Therefore, while sizeof(mystr) evaluates to 100, strlen(mystr) returns 11.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ size-of-char-datatype-and-char-array-in-c
size of char datatype and char array in C - GeeksforGeeks
July 12, 2025 - Then the size of the char array is find by dividing the size of the complete array by the size of the first variable.
Find elsewhere
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ forum 2005-2010 (read only) โ€บ software โ€บ syntax & programs
C style char array length function - Syntax & Programs - Arduino Forum
November 23, 2010 - Hello, just in case someone may need a function to determine the length of a char array, I give the one I made: int strLength(char *buffer){ int i = 0, length =0; while (buffer *!= '\0'){ * if (buffer[i+1] == '\0') {* length = i+1;* return length;* }* i++;* }* } Andreas
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 189716
Beginner Question - Size of Char Array - C++ Forum
In your first example, char bacon[ 6 ], that code declares an array that will hold up to six characters; however, if you want to use strlen() or any of the functions that work on "C" strings (null-terminated character arrays), it should hold at most five characters and a null byte.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ how does c know the size of an array?
r/C_Programming on Reddit: How does C know the size of an array?
January 6, 2022 -

A char array has the terminating \0 byte at the last index, so you can easily iterate through it. I can access the array by using it's address, stored in a pointer variable. The pointer is just a number, and by adding digits i can get any array index i want.

Integer arrays do not have a terminating byte and there is no information about the size in the pointer address. So how does for example free() know what to free?

Top answer
1 of 7
35
The allocator needs to keep that information itself. There's a couple of approaches at doing this. The straight-forward way is for the memory allocator to keep a small amount of metadata for of each memory allocation. That metadata would contain the size of the allocation (the size you passed to malloc rounded up to a more convenient value). When you pass back a pointer to free, it can use that size to know how big the allocation was. A common approach is for the metadata to be placed in memory immediately before the pointer given to the program in malloc. Another approach is for the memory to be allocated from a slab of equal-sized blocks. There might be a slab that allocates 8-byte blocks, a slab for 16-byte blocks, a slab for 32-byte blocks, and so on. If you malloc(12), say, the allocator gives the program one of the 16-byte blocks. When the pointer is passed back to free, the memory allocator knows how big the allocation was since it can determine which slab the pointer came from. The allocator still needs to keep some metadata to know which blocks within a slab have been allocated, but not as much as would be needed to track the size of each allocation individually. C is fairly agnostic as to what a pointer actually is, so I suppose you could even have a C implementation with fat pointers that also encoded the sizes (or perhaps their bounds) of their allocations. I'm not sure if any system ever worked this way though.
2 of 7
33
How does C know the size of an array? It does not, because it can not know, since C arrays are just pointers to the memory address of the first element. With other words, you have to tell it the size. This because there is no array data type in the machine. C models memory as a linear space of addresses starting from 0 to N and counting bytes. An array is just chunk or consecutive memory addresses. If you need the size of an array, you usually keep that in some variable. The pointer is just a number, and by adding digits i can get any array index i want. You are not adding digits; you are adding offsets. C has "pointer arithmetic", i.e. pointers are an intrinsic data type in C and have types, so int* is not the same as char*. That, so you can actually work with indexes and not with bytes. If you have an int32_t *i, and a int8_t *c; when you do i+1 and c+1, the compiler will know how many bytes to add, 4 or 1, so that you get address of next element in memory, otherwise you would have to do yourself this low arithmetic to get correct offset to next element. A char array has the terminating \0 byte at the last index, so you can easily iterate through it. Not really. A char array would be just an array of characters, i.e. of integers, since there is no char data type in a machine either. What you think of is a null-terminated string, typically a const char*. If you declare a string as a literal, something like char* s = "hello world", the compiler will add that terminating null for you. If you declare char[10], an array of 10 chars, there would be no terminating null at the end. If you wish to store a string in that array for use with string functions from string.h, you would have to put terminating null in it yourself. That terminating null is added because arrays are not intrinsic data types in C either, but pointers, and do not record length automatically. So how does for example free() know what to free? free is an API into a memory allocation/deallocation routines. It keeps internal structures that keeps track of allocated memory.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ how to find the length of an array in c?
How to Find the Length of an Array in C? - Scaler Topics
August 16, 2022 - Arrays have a fixed length that is specified in the declaration of the array. In C, there is no built-in method to get the size of an array. A little effort is required to get the length of the array by utilizing the in-built methods or pointers in C.
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ projects โ€บ programming
char* array get length of string? - Programming - Arduino Forum
June 14, 2016 - With an array like this..... char* myStrings[]={"This is string 1", "This is string 2", "This is string 3", "This is string 4", "This is string 5","This is string 6"}; What is the correct way to get the length of "mySโ€ฆ
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ what is character array in c?
What is Character Array in C? - Scaler Topics
October 10, 2025 - There are many syntaxes for creating a character array in c. The most basic syntax is, The name depicts the name of the character array and size is the length of the character array.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 281381
How to find the size of a char array - C++ Forum
Also, the above code only works with an actual array, it does ยท not work with plain char* pointers at all !!! If you want to determine the "length" of a string, not counting the final NULL character, use strlen() function: https://www.cplusplus.com/reference/cstring/strlen/ Warning: When using ...
๐ŸŒ
Experts Exchange
experts-exchange.com โ€บ questions โ€บ 24146644 โ€บ C-char-array-size-limitation.html
Solved: C char array size limitation | Experts Exchange
February 16, 2009 - There is no such limitation imposed by the standard. It basically depends on the amount of memory you have available for use for the array. There is however a limit on the length of a string literal - that can be maximum 4095 characters long.
๐ŸŒ
Cplusplus
cplusplus.com โ€บ forum โ€บ beginner โ€บ 12305
Find the size of a char array - C++ Forum
Helios is correct, you can't, you have to pass the size as a parameter. My solution only works when you have direct access to the array. You could always get rid of the function, load that message into a char array, and then put the loop in main and use the thing I suggested.
๐ŸŒ
CodinGame
codingame.com โ€บ playgrounds โ€บ 14213 โ€บ how-to-play-with-strings-in-c โ€บ array-of-c-string
Array Of C String - How to play with strings in C
CodinGame is a challenge-based training platform for programmers where you can play with the hottest programming topics. Solve games, code AI bots, learn from your peers, have fun.