This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.

The strlen() and sizeof() functions are commonly used to calculate the length of an array or string. It’s easy to get confused when using it. Let’s take a look at the specific differences between the two functions with a few simple examples.

int main(a)
{
	char str[]={1.2.3};
	int cnt = 0;
	
	cnt = strlen(str);
	printf("strlen:%d\r\n",cnt);
	
	cnt = sizeof(str);
	printf("sizeof:%d\r\n",cnt);
	

	system("pause");
	return 0;
}
Copy the code

Define an array and print it out using strlen() and sizeof() respectively.These two functions compute the same length, and then change the integer array to a string.

int main(a)
{
	char str[]="123";
	int cnt = 0;
	
	cnt = strlen(str);
	printf("strlen:%d\r\n",cnt);
	
	cnt = sizeof(str);
	printf("sizeof:%d\r\n",cnt);
	

	system("pause");
	return 0;
}
Copy the code

After changing the array to a string, sizeof() calculates one more length than strlen(). Because the end of the string ends with the character ‘\0’. The sizeof() function takes the ending character into account, while the strlen() function simply takes the valid character into account.

The above array does not specify the length, after fixing the array length in the test.

int main(a)
{
	char str[10] ="123";
	int cnt = 0;
	
	cnt = strlen(str);
	printf("strlen:%d\r\n",cnt);
	
	cnt = sizeof(str);
	printf("sizeof:%d\r\n",cnt);
	
	system("pause");
	return 0;
}
Copy the code

Set the length of the array to 10 and test it. The output is as follows:

The sizeof() function calculates a length of 10, which is the true sizeof the array. The length calculated by strlen() is 1, which is the length of the valid string.

The sizeof() function measures the sizeof the memory used by the data type. Regardless of whether the data currently stored is valid or not. The strlen() function calculates the size of a valid character.

So you usually use the strlen() function to calculate the length of the string and sizeof() to calculate the sizeof the space taken up by the data type. One of the most popular is to use it to calculate the length of an array.

The sizeof() function is used to calculate the length of the array

int main(a)
{
	int val[]= {8.7.6.5.4.3.2};
	int cnt = 0,cnt1 = 0,cnt2 = 0;

	cnt1 = sizeof(val);
	cnt2 = sizeof(val[0]);
	
	cnt = cnt1 / cnt2;	
	
	printf("sizeof:%d,%d,%d\r\n",cnt1,cnt2,cnt);

	system("pause");
	return 0;
}
Copy the code

The print result is as follows:

It can be seen from the printed result that CNt1 is 28, which is the memory space occupied by the entire array. Cnt2 is 4, which is the memory space of the first element in the array. Because the integer takes up 4 bytes in memory. So the length of the array is the size of the entire array divided by the size of each byte, which is 7.

Can we use strlen() as well? Replace sizeof with strlen in the code above.

Strlen () can only be used in strings. For other types of arrays, sizeof () is the only sizeof () function to calculate the length.