C Pen question (1)

Source: Internet
Author: User

the difference between 1.sizeof and strlen

#include <stdio.h>

#include <string.h>

int main ()

{

Char a[10] = "12345";

Char *p = "12345";

Char b[20];

printf ("%d%d\n", sizeof (a), strlen (a));

/*sizeof is an operator,strlen is a function,andsizeof wants to add '/0 ',

Strlen represents the length of the string, and the strlen parameter can only be ' char* ' * /

printf ("%d%d\n", sizeof (*a), strlen (a));

printf ("%d%d\n", sizeof (p), strlen (p));

printf ("%d%d\n", sizeof (*P), strlen (p));

/* because b is unknown, so strlen is uncertain * /

printf ("%d%d\n", sizeof (b), strlen (b));

printf ("%d%d\n", sizeof (*B), strlen (b));

return 0;

}

Operation Result:

10 5

1 5

4 5

1 5

20 23

1 23

2. Running results of the following programs

#include <stdio.h>

int main ()

{

Double a[2]={3,9},*p,*q;

p=&a[0];

q=p+1;

printf ("%d%d\n", (q-p), (int) q (int) p);

return 0;

}

Run Result:1 8

3. The following procedures

#include <stdio.h>

int main ()

{

#define SUN 0

printf ("1");

#ifndef SUN

printf ("2");

#endif

printf ("3");

return 0;

}

Operation Result:

13

4. When the arrayint *p= (int*) malloc (10*sizeof (int))at the time of releaseFree (p), this is because the compilermallocsome special processing is done to ensure that the memory can be freed correctly. And whenint *p=new int[10]the release should beDelete []p, note[]the function of the description is to release an array of memory ifDelete Pit's just released.P[0], the remaining9aintthe memory is not released because it is specified as[], the compiler actually does a loop to release all the memory for this array.

5. Find out1~13in the integer1Number of occurrences,and figure out100~1300in the integer1The number of occurrences? For that, he counted a few.1~13included in1the number has1,Ten, One, A, -so there is a total6Times,But he has no problem with the latter. AcmerI hope you 'll help him.,and to make things more universal.,can quickly find out any non-negative integer range1the number of occurrences.

int numberof1between1andn_solution (int n)

{

int count=0;

for (int j=1;j<=n;j++)

{

int i=j;

while (i)

{

if (i%10==1)

count++;

I=I/10;

}

}

return count;

}

6. For statements such as a= (b++,c++), the value of a is C + +.

The header file of the 7.fork () function is #include<unistd.h>, and the fork () function is incremented by 2 of the n-th.

C Pen question (1)

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.