In the previous blog, I also explored strlen,sizeof differences, details can be seen in the blog http://10740184.blog.51cto.com/10730184/1705820.
About strlen,sizeof can find the length of the string, both of which are the knowledge points in the test interview .
#include <stdio.h> #include <stdlib.h> #include <string.h>//void test1 ()//{// char* arr = "hello world!"; / int len1 = sizeof (arr); &NBSP;&NBSP;&NBSP;&NBSP;//4, addresses are 4 bytes in size// int len2 = strlen (arr); //12, find ' n ', total 12 bytes// printf ("sizeof:%d\n", len1);// printf ("strlen:%d\n", len2) ;//}//void test2 ()//{// char arr[20] = "hello world!"; / int len1 = sizeof (arr); //-->20 only look at type size// int len2 = strlen (arr); //12, find ' n ', no '% ' length// printf ("sizeof:%d\n", len1);// printf ("strlen:%d\n", LEN2);//}//void test3 ()//{// char arr[] = " Hello world! "; / int len1 = sizeof (arr); //13, with the length// int len2 = strlen (arr); //12, find ' n ', no '/' length// printf ("sizeof:%d\n", len1);// printf ("strlen:%d\n", len2) ;//}//void test4 ()//{// char arr[] = { ' h ', ' e ', ' l ' , ' l ', ' o ' };// int len1 = sizeof (arr); &NBSP;//5, self-judged out of length, no '// int len2 = strlen ' Length (arr); //random value, cannot find '// printf ' ("sizeof:%d\n", len1);// printf ("strlen:%d\n", len2);//}void test5 () { char arr[10] ; int len1 = sizeof (arr); &NBSP;&NBSP;&NBSP;&NBSP;//10, judging by its length, without the length &nbsP; int len2 = strlen (arr); //random value, cannot find ' ' printf ("sizeof:%d\n", len1); printf ("strlen:%d\n", len2);} Int main () { /*test1 (); */ /*test2 ();*/ /*test3 (); */ /*test4 (); */ test5 (); System ("pause"); return 0;}
This article is from the "C language 100-200 Prime" blog, please be sure to keep this source http://10740184.blog.51cto.com/10730184/1740461
"Written test frequently" C language: Deep analysis strlen,sizeof