"Written test frequently" C language: Deep analysis strlen,sizeof

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.