Strlen() in C
0 271
strlen is a function in C used to determine the length of a string.
It counts the number of characters in a string until it encounters the null terminator '\0'.
This function is part of the C standard library
It's commonly used in string processing tasks to find the length of a string for memory allocation, copying, or comparison operations.
The string "Hello\0" is represented in memory.
Each character occupies one memory location.
strlen() starts counting from the beginning of the string until it encounters the null terminator '\0', which marks the end of the string.
In this example, the length of the string is determined by the number of characters before the null terminator, which is 5.
Syntax of strlen()
size_t strlen(const char *str);
For example:
// Program for strlen() in C
#include<stdio.h>#include<string.h> int main() { char str[] = "Hello, world!"; int length = strlen(str); printf("Length of string: %d\n", length); return 0;
}
Output:
Length of string: 13
Here, strlen calculates the length of the string "Hello, world!" excluding the null terminator, which is 13 characters.
Share:
Comments
Waiting for your comments