String assignment to understand some of the differences between sizeof and strlen

Source: Internet
Author: User

#include <stdio.h>
#include <string.h> 
int main ()
{
 int a,i=0;
 char ch[10000];
 while (scanf ("%d", &a)!=eof&&a!=-1)
 {
  ch[i++]=a;
 }
 /*
  test Results The sizeof test is the total space   including the \ n
 strlen test is the length before the first hit
 */
  printf ("sizeof calculates the length:%d\n", sizeof (CH));
 printf ("strlen Calculated length:%d\n", strlen (CH));
 char th[]={"abc"};
 puts (TH);
 printf ("sizeof calculates the length:%d\n", sizeof (TH));
 printf ("strlen Calculated length:%d\n", strlen (TH));
// char sh[];
 char sh[]={' A ', ' B ', ' C ', ' \0  '};//char sh[]={' A ', ' B ', ' C '}; This is the wrong assignment so that the assignment does not follow the result of abcabc  printing   sizeof One less
// memset (sh,0,sizeof (SH));
 sh[0]= ' a ';
 puts (SH);
 printf ("sizeof calculates the length:%d\n", sizeof (SH));
 printf ("strlen Calculated length:%d\n", strlen (SH));
}//Test data
/*
1 0 2 0 1 0 0-1
Normally assumed to be  7
High precision operation when initialized is empty so add character 0 to calculate it's going to happen.
*/

String assignment method to understand some differences between sizeof and strlen

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.