Interesting questions about C

Source: Internet
Author: User

Today, I went to a company to take the test. I personally felt that there were several interesting questions. I would like to share them with you:

1,

void main(){unsigned char i =0;char a[1024] ={0};for(i = 0;i<1024;i++){printf("a[%d]:0x%02x\n",i,a[i]);}}

What is the output result?

The answer should be: 1. endless loop, and then

A [0]: 0x00

A [1]: 0x00

A [2]: 0x00

......

 

A [255]: 0x00

 

Note that the maximum value of I is 255, so it will keep repeating.

 

2,

void main(){int a =0;a|=(0x01<<4);printf("%d\n",a);//a=16;a&=~(0x01<<4);printf("%d\n",a);//a=0;}

You can calculate it by yourself.

3,

void main(){int a =1;printf("%d\n",*((unsigned char *)&a));//output is 1a=0x12345678;printf("%d\n",*((unsigned char *)&a+1));//output is 86}

The second output is 86, which is not clear yet.

 

4,

void function1(char*a){printf("%d\n",strlen(a));}void function2(char*a){printf("%d\n",sizeof(a));}void main(){char a[100] ={0};//0 is '\0' function1(a);// 0 is '\0' ,so strlen(a) is 0;function2(a);// pointer length is 4 bytes;}

Function1 is printed as 0, because a [100] is initialized to 0, 0 is '\ 0 '.

Function2 prints 4, which is the length of the pointer type.

5,

void main(){char a[8] ={"hello"};char b[8]={0};printf("a:0x%08x\n",a);strcpy(b,"01234567");printf("a:%s\n",a);printf("b:%s\n",b);}

The result is:
A: 0xbfc2dccc
A:
B: 01234567
I haven't figured it out yet.

 

6,

Unsigned int A = 10; // when unsigned, the highest bit is not used to indicate positive and negative. Int B =-100; // 1 of the highest bit of int indicates a negative number if (a + B> 10) // A + B is added only when all values are converted to the unsigned type, so> 10 printf ("> 10 \ n"); elseprintf ("<10 \ n ");

Print "> 10 ".

 

7,

void main(){char a[10]; printf("%d\n",strlen(a) );}

The output is not 10, because strlen (a) ends when it finds '\ 0'. We do not know where the end mark' \ 0' of a [10] is.

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.