The most straightforward way to obtain the length of a string in C is by using the strlen
function. This function is part of the C standard library (string.h
) and is widely used for its simplicity and efficiency.
Here’s how you would do that:
#include<stdio.h>
#include<string.h>
int main(){
char str[7] = "Hello";
int len = strlen(str);
printf("%u\n", len);
return 0;
}
So this is how you would get the length of the string in C.
You can also get the length of a number in C, check out my article on this.
Thanks for reading!