strlen
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).
This should not be confused with the size of the array that holds the string. 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.
which C++ header file declares strlen?
Why does it work without including library string or cstring?
Should I include cstring or string there? string.h?
5 Answers 5
Which library does strlen() belong to? Does it belong to cstring? or string?
Neither. cstring and string are not libraries, they are header files which define the interface to various functions and classes.
The C language standard says that the strlen function is declared in the header file <string.h> . In C++, including <string.h> places strlen into the global namespace, while including <cstring> instead places strlen into the std namespace.
The actual implementation of the strlen function is in the C standard library (aka libc or CRT on certain platforms). Ordinarily, this is linked in with your executable at link time.
Why it works without including library string or cstring?
In your particular compiler and toolchain, it just so happens that the header file <iostream> includes <cstring> into it, which means that any code that includes the former also gets the latter for free. This is an implementation detail and should not be relied upon—if your compile your code with another compiler, you may suddenly find yourself in a sea of compiler errors.
The proper thing to do is to also include <cstring> here; even though it’s not necessary with your particular compiler, it may be necessary with other compilers.
Функция strlen
Длина Си-строки определяется по достижению нулевого символа — нуль терминатор. Функция strlen видит начало Си-строки и начинает сначала считать количество символов (байтов, отводимых под каждый символ), этот процесс выполняется до тех пор, пока не будет достигнут завершающий нулевой символ. Обратите внимание на то, что завершающий нулевой символ не входит в длину строки. Он является служебным символом, для обозначения завершения Си-строки.
Не путайте размер массива, который содержит строку и длину строки. Например:
В данной строке массив имеет размер 100 символов, но строка в этом массиве имеет длину всего 11 символов. Таким образом, если выполнится оператор sizeof(string) , ответ будет 100, а если — функция strlen(string) , ответ 6.
C library function — strlen()
The C library function size_t strlen(const char *str) computes the length of the string str up to, but not including the terminating null character.
Declaration
Following is the declaration for strlen() function.
Parameters
str − This is the string whose length is to be found.
Return Value
This function returns the length of string.
Example
The following example shows the usage of strlen() function.
Let us compile and run the above program that will produce the following result −