溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C/C++ 面試題

發(fā)布時(shí)間:2020-07-16 10:00:15 來源:網(wǎng)絡(luò) 閱讀:785 作者:990487026 欄目:編程語(yǔ)言


分析一下程序的行為

分析程序的行為1
[chunli@CentOS c]$ cat file_1.c 
#include <stdio.h>
int main()
{
    char *function_1()
    {
        return "Hello";
    }
    char *str = function_1();
    printf("%s \n", str);
    return 0;
}
[chunli@CentOS c]$ gcc  file_1.c -Wall && ./a.out
Hello 



---------------------------------
分析程序的行為2
[chunli@CentOS c]$ cat file_1.c 
#include <stdio.h>
void test()
{
    char *function_1()
    {
        return "Hello";
    }
    char *str = function_1();
    printf("%s \n", str);

}
int main()
{
    test();
    return 0;
}
[chunli@CentOS c]$ gcc  file_1.c -Wall && ./a.out
Hello 
[chunli@CentOS c]$







用遞歸判斷一個(gè)數(shù)組 是不是遞增的

chunli@Linux:~$ cat main.c 
#include <stdio.h>
int IsIncreArray(int *arr,int len)
{
	if(len == 1)
		return 0;
	else
	{
		if(arr[len-1] < arr[len-2])
			return -1;
		else
			return IsIncreArray(arr,--len);
	}
}

int main()
{
	int array1[10] = {0,1,9,3,4,5,6,7,8,9};
	int len1 = sizeof(array1)/sizeof(array1[0]);
	if(IsIncreArray(array1,len1) == 0)
		printf("yes\n");
	else
		printf("no\n");


	int array2[10] = {0,1,2,3,4,5,6,7,8,9};
	int len2 = sizeof(array2)/sizeof(array2[0]);
	if(IsIncreArray(array2,len2) == 0)
		printf("yes\n");
	else
		printf("no\n");
	return 0;
}
chunli@Linux:~$ gcc main.c -Wall  && ./a.out 
no
yes
chunli@Linux:~$



求一個(gè)整數(shù),二進(jìn)制表示出來,有多少個(gè)1

#include <iostream>
using namespace std;
int func(int x)
{
        int count = 0;
        while (x)
        {
                count ++;
                x = x&(x-1);
        }
        return count;
}


int main(void)
{
        cout << func(9999) << endl;
        return 0;

}

chunli@pc0003:~$ g++ main.cpp && ./a.out 
8



i++問題

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
#include <string.h>
int main()
{
	int a;
	a= 4;	a += (a++);	printf("%d\n",a);	//9
	a= 4;	a += (++a);	printf("%d\n",a);	//10
	//a= 4;	(a++) += a;	printf("%d\n",a);	//編譯報(bào)錯(cuò)
	//a= 4;	(++a) += a;	printf("%d\n",a);	//編譯報(bào)錯(cuò)

	return 0;
}
chunli魂斗羅~$ gcc main.c  && ./a.out 
9
10
chunli魂斗羅~$


關(guān)于i++,++i

#include <iostream>
using namespace std;
int main(void)
{
        int a,x;
        for(a=0,x=0; a<=1 && !x++;a++)
        {
                a++;
        }
        cout<<a<<x<<endl;
        return 0;
}
chunli@pc0003:~$ g++ main.cpp && ./a.out 
21
#include <iostream>
using namespace std;
int main(void)
{
        int a,x;
        for(a=0,x=0; a<=1 && !x++;)
        {
                a++;
        }
        cout<<a<<x<<endl;
        return 0;
}
chunli@pc0003:~$ g++ main.cpp && ./a.out 
12


解析:這兩段代碼的不同點(diǎn)就在for循環(huán)那里,前者是for(a=0,x=0; a<=1 &&!x++;a++),
后者是for(a=0,x=0;a<=1 &&!x++;)。
先說第1段代碼。
第1步:初始化定義a=0,x=0。
第2步:a小于等于1,x的非為1,符合循環(huán)條件。
第3步:x++后x自增為1。
第4步:進(jìn)入循環(huán)體,a++,a自增為1。
第5步:執(zhí)行for(a=0,x=0;a<=1 &&!x++;a++)中的a++,a自增為2。
第6步:a現(xiàn)在是2,已經(jīng)不符合小于等于1的條件了,所以“&&”后面的“!x++”不執(zhí)行,x還是1,不執(zhí)行循環(huán)體。
第7步:打印a和b,分別是2和1。


再說第2段代碼。
第1步:初始化定義a=0,x=0。
第2步:a小于等于1,x的非為1,符合循環(huán)條件。
第3步:x++后x自增為1。
第4步:進(jìn)入循環(huán)體,a++,a自增為1。
第5步:a現(xiàn)在是1,符合小于等于1的條件,所以“&&”后面的“!x++”被執(zhí)行,x現(xiàn)在是1,x的非為0,不符合循環(huán)條件,不執(zhí)行循環(huán)體,但x++依然執(zhí)行,自增為2。
第6步:打印a和b,分別是1和2。
答案:第一段輸出結(jié)果是21,第二段輸出結(jié)果是12。

#include <stdio.h>
int main(void)
{
        int b =3;
        int arr[] = {6,7,8,9,10};
        int *p = arr;
        *(p++) += 123;
        printf("%d,%d\n",*p,*(++p));
}
chunli@pc0003:~$ gcc main.c && ./a.out 
8,8

解析:C中printf計(jì)算參數(shù)時(shí)是從右到左壓棧的。
幾個(gè)輸出結(jié)果分別如下:
printf("%d\n ",*ptr);此時(shí)ptr應(yīng)指向第一個(gè)元素6。
*(ptr++)+=123應(yīng)為*ptr=*ptr+123;ptr++,此時(shí)ptr應(yīng)指向第二個(gè)元素7。
printf("%d\n ",*(ptr-1));此時(shí)輸出第一個(gè)元素129,注意此時(shí)是經(jīng)過計(jì)算的。
printf("%d\n ",*ptr);此時(shí)輸出第二個(gè)元素7,此時(shí)ptr還是指向第二個(gè)元素7。
printf("%d,%d\n",*ptr,*(++ptr));從右到左運(yùn)算,第一個(gè)是(++ptr),也就是ptr++,*ptr=8,此時(shí)ptr指向第三個(gè)元素8,所以全部為8。



百雞百錢問題:

#include <stdio.h>
#include <stdlib.h>
void main()
{

        int i = 0;
        int j = 0;
        int k = 0;
        for(i=0;i<20;i++)
        {
                for(j = 0;j<34;j++)
                {
                        if((100 -i - j) == (100-5*i-3*j)*3 && (100-j-i)%3 == 0 )
                        {
                                printf("%d,%d,%d\n",i,j,100-i-j);
                        }
                }
        }
}

編譯:

chunli@Linux:~/high$ ./a.out 
0,25,75
4,18,78
8,11,81
12,4,84
chunli@Linux:~/high$


蛇形數(shù),

1,層數(shù)分析:

C/C++ 面試題

chunli@http://990487026.blog.51cto.com~/snake$ cat main.c 
#include <stdio.h>
#define  N  9	//定義矩形的邊長(zhǎng)

int main()
{
	int arr[N][N] = `0`;//抽象為二維數(shù)組模型
	int data = 1;//初始值
	int y = 0;//控制上下
	int x = 0;//控制左右
	int ring = 0;//控制層數(shù)
	//蛇形大法
	for(y=0,x=0,ring=0;ring<(N+1)/2;ring++)//控制層數(shù)
	{
		//向右跑
		while(x<N-ring)
		{
			arr[y][x] = data;
			x++;
			data++;
		}

		//向下跑
		x--;//此時(shí)x == N,向左回退,調(diào)整方向
		y++;//向下轉(zhuǎn)一行,相當(dāng)于定位到第二行末尾
		while (y<N-ring)
		{
			arr[y][x] = data;
			y++;
			data++;
		}
		
		//向左跑
		y--;//向上回退,相當(dāng)于定位到N行的末尾
		x--;//此時(shí)x == N,調(diào)整方向
		while(x>ring-1)
		{
			arr[y][x] = data;
			x--;
			data++;
		}

		//向上跑
		y--;//
		x++;//
		while(y>ring)
		{
			arr[y][x] = data;
			y--;
			data++;
		}
		
		y++;//恢復(fù)為正常向下
		x++;//恢復(fù)為正常向右
	}

	//打印二維數(shù)組
	for(y = 0;y<N;y++)
	{
		for(x=0;x<N;x++)
		{
			printf("%3d",arr[y][x]);
		}
		printf("\n");
	}
	return 0;
}
chunli@http://990487026.blog.51cto.com~/snake$ gcc main.c  -Wall && ./a.out 
  1  2  3  4  5  6  7  8  9
 32 33 34 35 36 37 38 39 10
 31 56 57 58 59 60 61 40 11
 30 55 72 73 74 75 62 41 12
 29 54 71 80 81 76 63 42 13
 28 53 70 79 78 77 64 43 14
 27 52 69 68 67 66 65 44 15
 26 51 50 49 48 47 46 45 16
 25 24 23 22 21 20 19 18 17
chunli@http://990487026.blog.51cto.com~/snake$

蛇形數(shù),換個(gè)方向:

C/C++ 面試題

chunli@http://990487026.blog.51cto.com~/snake$ cat main.c 
#include <stdio.h>
#define  N  25	//定義矩形的邊長(zhǎng)

int main()
{
	int arr[N][N] = `0`;//抽象為二維數(shù)組模型
	int data = 1;//初始值
	int y = 0;//控制上下
	int x = 0;//控制左右
	int ring_number = 0;//控制層數(shù)
	//蛇形大法
	for(y=0,x=0,ring_number=0;ring_number<(N+1)/2;ring_number++)//控制層數(shù)
	{
		while(y<N-ring_number)
		{
			arr[y++][x] = data++;
		}

		x++;y--;//調(diào)整方向
		while(x<N-ring_number)
		{
			arr[y][x++] = data++;
		}

		x--;y--;//調(diào)整方向
		while(y>ring_number-1)
		{
			arr[y--][x] = data++;
		}	
		
		x--;y++;//調(diào)整方向
		while(x>ring_number)
		{
			arr[y][x--] = data++;
		}

		x++;y++;//調(diào)整方向
	}
	//打印二維數(shù)組
	for(y = 0;y<N;y++)
	{
		for(x=0;x<N;x++)
		{
			printf("%4d",arr[y][x]);
		}
		printf("\n");
	}
	return 0;
}


chunli@http://990487026.blog.51cto.com~/snake$ gcc main.c  -Wall && ./a.out 
   1  16  15  14  13
   2  17  24  23  12
   3  18  25  22  11
   4  19  20  21  10
   5   6   7   8   9
chunli@http://990487026.blog.51cto.com~/snake$ gcc main.c  -Wall && ./a.out 
   1  56  55  54  53  52  51  50  49  48  47  46  45  44  43
   2  57 104 103 102 101 100  99  98  97  96  95  94  93  42
   3  58 105 144 143 142 141 140 139 138 137 136 135  92  41
   4  59 106 145 176 175 174 173 172 171 170 169 134  91  40
   5  60 107 146 177 200 199 198 197 196 195 168 133  90  39
   6  61 108 147 178 201 216 215 214 213 194 167 132  89  38
   7  62 109 148 179 202 217 224 223 212 193 166 131  88  37
   8  63 110 149 180 203 218 225 222 211 192 165 130  87  36
   9  64 111 150 181 204 219 220 221 210 191 164 129  86  35
  10  65 112 151 182 205 206 207 208 209 190 163 128  85  34
  11  66 113 152 183 184 185 186 187 188 189 162 127  84  33
  12  67 114 153 154 155 156 157 158 159 160 161 126  83  32
  13  68 115 116 117 118 119 120 121 122 123 124 125  82  31
  14  69  70  71  72  73  74  75  76  77  78  79  80  81  30
  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29
chunli@http://990487026.blog.51cto.com~/snake$ gcc main.c  -Wall && ./a.out 
   1  96  95  94  93  92  91  90  89  88  87  86  85  84  83  82  81  80  79  78  77  76  75  74  73
   2  97 184 183 182 181 180 179 178 177 176 175 174 173 172 171 170 169 168 167 166 165 164 163  72
   3  98 185 264 263 262 261 260 259 258 257 256 255 254 253 252 251 250 249 248 247 246 245 162  71
   4  99 186 265 336 335 334 333 332 331 330 329 328 327 326 325 324 323 322 321 320 319 244 161  70
   5 100 187 266 337 400 399 398 397 396 395 394 393 392 391 390 389 388 387 386 385 318 243 160  69
   6 101 188 267 338 401 456 455 454 453 452 451 450 449 448 447 446 445 444 443 384 317 242 159  68
   7 102 189 268 339 402 457 504 503 502 501 500 499 498 497 496 495 494 493 442 383 316 241 158  67
   8 103 190 269 340 403 458 505 544 543 542 541 540 539 538 537 536 535 492 441 382 315 240 157  66
   9 104 191 270 341 404 459 506 545 576 575 574 573 572 571 570 569 534 491 440 381 314 239 156  65
  10 105 192 271 342 405 460 507 546 577 600 599 598 597 596 595 568 533 490 439 380 313 238 155  64
  11 106 193 272 343 406 461 508 547 578 601 616 615 614 613 594 567 532 489 438 379 312 237 154  63
  12 107 194 273 344 407 462 509 548 579 602 617 624 623 612 593 566 531 488 437 378 311 236 153  62
  13 108 195 274 345 408 463 510 549 580 603 618 625 622 611 592 565 530 487 436 377 310 235 152  61
  14 109 196 275 346 409 464 511 550 581 604 619 620 621 610 591 564 529 486 435 376 309 234 151  60
  15 110 197 276 347 410 465 512 551 582 605 606 607 608 609 590 563 528 485 434 375 308 233 150  59
  16 111 198 277 348 411 466 513 552 583 584 585 586 587 588 589 562 527 484 433 374 307 232 149  58
  17 112 199 278 349 412 467 514 553 554 555 556 557 558 559 560 561 526 483 432 373 306 231 148  57
  18 113 200 279 350 413 468 515 516 517 518 519 520 521 522 523 524 525 482 431 372 305 230 147  56
  19 114 201 280 351 414 469 470 471 472 473 474 475 476 477 478 479 480 481 430 371 304 229 146  55
  20 115 202 281 352 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 370 303 228 145  54
  21 116 203 282 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 302 227 144  53
  22 117 204 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 226 143  52
  23 118 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 142  51
  24 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141  50
  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49
chunli@http://990487026.blog.51cto.com~/snake$






指針問題1,求n的值

#include <stdio.h>
int main()
{

	char *p = new char[4];
	p[0] = 1;
	p[1] = 2;
	p[2] =3;
	p[3] = 4;
	int * pInt = (int *)p;
	int n = *pInt;
	printf("%x\n",n);
	printf("%d\n",n);
}
chunli@http://990487026.blog.51cto.com~/seq_queue$ g++ 1.cpp  && ./a.out 
4030201
67305985
chunli@http://990487026.blog.51cto.com~/seq_queue$

解析:

char *轉(zhuǎn)譯地址時(shí)按1個(gè)字節(jié)取值,

強(qiáng)轉(zhuǎn)為int*,轉(zhuǎn)譯地址時(shí)按4個(gè)字節(jié)取值


指針問題2,求n的值

chunli@http://990487026.blog.51cto.com~/seq_queue$ cat 1.cpp 
#include <stdio.h>
int main()
{
	size_t *p = (size_t *)1000;
	size_t n = (size_t)(p + 100);
	printf("%ld\n",n);
}
chunli@http://990487026.blog.51cto.com~/seq_queue$ g++ 1.cpp  && ./a.out 
1800
chunli@http://990487026.blog.51cto.com~/seq_queue$

解析:

考察不同數(shù)據(jù)類型地址偏移的計(jì)算


繼續(xù)指針步長(zhǎng)問題

chunli魂斗羅~/review$ cat main.c 
#include <stdio.h>
int main()
{
	int a[5] ={1,2,3,4,5};
	int *ptr = (int*)(&a+1);
	printf("%d %d\n",*(a+1),*(ptr-1));
	return 0;
}
chunli魂斗羅~/review$ gcc -Wall main.c  && ./a.out 
2 5
chunli魂斗羅~/review$


當(dāng)指針與引用在一起:

[root@linux tmp]# cat main.c
#include <stdio.h>
int fun(int * &a)
{
	printf("a=%d\n",*a);
	return 0;
}
int main()
{ 
	int a = 10;
	int *b = &a;
	fun(b);
	return 0;
} 
[root@linux tmp]# g++ main.c -Wall && ./a.out 
a=10
[root@linux tmp]#


當(dāng)指針與引用在一起:

[root@linux tmp]# cat main.cpp 
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
void fun(int *&p)
{
	p = (int *)malloc(0);
	printf("in fun  %p\n",p);
}
int main(void)
{
	int *p = NULL;	printf("in main %p\n",p);
	fun(p);		printf("in main %p\n",p);

	return 0;
}
[root@linux tmp]# g++ main.cpp  -Wall && ./a.out 
in main (nil)
in fun  0x87e7008
in main 0x87e7008
[root@linux tmp]#



求數(shù)組中第N大的數(shù)

chunli魂斗羅~/review$ cat main.c 
#include <stdio.h>
#include <stdlib.h>
void sort(int  *arr,int len)
{
	int i = 0;
	int j = 0;
	for(i = 0;i<len-1;i++)
	{
		int index = i;
		for(j=i+1;j<len;j++)
		{
			if(arr[index]<arr[j])
			{
				index = j;		
			}
		}
		int t= arr[i];
		arr[i] = arr[index];
		arr[index] = t;
	}
}

int  max(int *arr,int len,int num_max)
{
	sort(arr,len);
	int max = 0;
	int i = 0;
	for(i = 0;i<len && num_max >0;i++)
	{
		if(arr[i]!=arr[i+1])
		{
			num_max--;		
			max = arr[i];
		}
	}
	return max;
}

int main()
{
	int arr[] = {7,1,3,99,9,7,77,56,99,7,9,5,2,4};
	int len = sizeof(arr)/sizeof(int);
	printf("第1大的數(shù)是%d\n",max(arr,len,1));
	printf("第2大的數(shù)是%d\n",max(arr,len,2));
	printf("第3大的數(shù)是%d\n",max(arr,len,3));
	return 0;
}
chunli魂斗羅~/review$ gcc -Wall main.c  && ./a.out 
第1大的數(shù)是99
第2大的數(shù)是77
第3大的數(shù)是56
chunli魂斗羅~/review$



結(jié)構(gòu)體字節(jié)對(duì)齊1

chunli魂斗羅~/review$ cat main.c 
#include <stdio.h>
typedef struct A
{
	char t:4;
	char k:4;
	unsigned short i:8;
	unsigned long m;
	
}A;
int main()
{
	printf("%ld\n",sizeof(A));
	return 0;
}
chunli魂斗羅~/review$ gcc main.c  && ./a.out 
16
chunli魂斗羅~/review$


結(jié)構(gòu)體字節(jié)對(duì)齊2

chunli魂斗羅~/review$ cat main.c 
#include <stdio.h>
typedef struct A
{
	char str;	//1
	short x;	//2 與char對(duì)齊
	int num;	//4 基本寬度
}A;
int main()
{
	printf("%ld\n",sizeof(A));
	return 0;
}
chunli魂斗羅~/review$ gcc main.c  && ./a.out 
8
chunli魂斗羅~/review$



結(jié)構(gòu)體字節(jié)對(duì)齊3

chunli魂斗羅~/review$ cat main.c 
#include <stdio.h>
typedef struct A
{
	char str;	//1
	int num;	//4 
	short x;	//2 
}A;
int main()
{
	printf("%ld\n",sizeof(A));
	return 0;
}
chunli魂斗羅~/review$ gcc main.c  && ./a.out 
12
chunli魂斗羅~/review$




遞歸實(shí)現(xiàn)圖形輸出

chunli魂斗羅~$ cat main.c 
#include <stdio.h>

void my_print(int num)
{
	if(!num)
	{
		return ;
	}
	my_print(num-1);
	int i = 0;	
	int j = 0;	
	for(i = 0;i<num;i++)
	{
		printf("*");
		for(j=1;j<num;j++)
		{
			printf(".");
		}
	}
	printf("\n");
}

int main()
{
	my_print(9);
	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
*
*.*.
*..*..*..
*...*...*...*...
*....*....*....*....*....
*.....*.....*.....*.....*.....*.....
*......*......*......*......*......*......*......
*.......*.......*.......*.......*.......*.......*.......*.......
*........*........*........*........*........*........*........*........*........
chunli魂斗羅~$



宏定義swap函數(shù)

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
//#define swap(x,y) (x)=(x)^(y);(y)=(x)^(y);(x)=(x)^(y);//標(biāo)準(zhǔn)答案
#define swap(x,y) (x)=(x)+(y);(y)=(x)-(y);(x)=(x)-(y);//答案2

int main()
{
	int a = 1;
	int b = 2;
	printf("a=%d b=%d \n",a,b);
	swap(a,b);
	printf("a=%d b=%d \n",a,b);
	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
a=1 b=2 
a=2 b=1 
chunli魂斗羅~$


宏定義mix函數(shù)

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
#define mix(x,y) ((x)<(y)?(x):(y))

int main()
{
	printf("mix=%d\n",mix(4,7));
	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
mix=4
chunli魂斗羅~$


宏定義函數(shù)求數(shù)組的長(zhǎng)度

#define ST(t) ((sizeof(t)/sizeof(t[0])))
int arr[30];
int len = ST(arr);


定義宏,傳入變量名,傳出變量名

#include <stdio.h>
#define VAR_P(var) printf(#var)
int main()
{
        int a = 0;
        VAR_P(a);
        return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
achunli魂斗羅~$


插入法排序面試題:

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
#include <time.h>
void arr_init(int *arr,int len)
{
	struct timespec tp;  
	clock_gettime(CLOCK_THREAD_CPUTIME_ID,&tp);  
	srand(tp.tv_nsec);
	int i = 0;
	for(i = 0;i<len;i++)
	{
		arr[i] = rand()%50;
	}
}
void arr_print(int *arr,int len)
{
	int i = 0;
	for(i = 0;i<len;i++)
	{
		printf("%d ",arr[i]);
	}
	printf("\n");
}
void arr_sort(int *arr,int len)
{
	int i = 0;
	int j = 0;
	int t = 0;
	for(i=1;i<len;i++)
	{
		t = arr[i];
		for(j=i-1;(j>=0 && arr[i-1]>t);j--)
		{
			arr[i] = arr[i-1];
			i--;
		}
		arr[i] = t;
	}
}
int main()
{
	int arr[30];
	int len = sizeof(arr)/sizeof(int);
	arr_init(arr,len);
	arr_print(arr,len);
	arr_sort(arr,len);
	arr_print(arr,len);
	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
29 24 41 17 32 36 39 26 45 37 38 14 1 23 15 49 22 25 28 6 29 1 14 17 30 49 49 31 10 16 
1 1 6 10 14 14 15 16 17 17 22 23 24 25 26 28 29 29 30 31 32 36 37 38 39 41 45 49 49 49 
chunli魂斗羅~$ 
chunli魂斗羅~$ gcc main.c && ./a.out
38 18 39 16 49 36 34 26 12 18 0 44 18 27 35 45 44 26 24 20 27 26 0 20 47 20 1 20 41 32 
0 0 1 12 16 18 18 18 20 20 20 20 24 26 26 26 27 27 32 34 35 36 38 39 41 44 44 45 47 49 
chunli魂斗羅~$ 
chunli魂斗羅~$ gcc main.c && ./a.out
19 34 44 20 9 30 1 45 0 12 44 9 14 42 2 5 13 36 5 14 11 11 46 17 43 15 46 34 15 1 
0 1 1 2 5 5 9 9 11 11 12 13 14 14 15 15 17 19 20 30 34 34 36 42 43 44 44 45 46 46 
chunli魂斗羅~$



判斷一個(gè)數(shù)是不是2的冪指數(shù)

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
#include <string.h>
int main()
{
	int a ;
	a = 4;
	printf("%c\n",a&(a-1)?'n':'y');
	a = 7;
	printf("%c\n",a&(a-1)?'n':'y');
	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
y
n
chunli魂斗羅~$




數(shù)據(jù)類型自動(dòng)轉(zhuǎn)換問題

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
#include <string.h>
int main()
{
	unsigned int a = 6;
	int b = -20;
	(a+b>6)?puts(">6"):puts("<=6");
	unsigned long c = a+b;
	printf("%ld\n",c);
	

	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
>6
4294967282
chunli魂斗羅~$


自己完成strcpy函數(shù)

chunli魂斗羅~$ cat main.c 
#include <stdio.h>

char* my_strcpy(char *dest,const char* src)
{
	if(dest==NULL || src==NULL)
		return NULL;
	while(*dest++=*src++);
}
int main()
{
	char buf[30];
	char *str = "Hello World!";
	my_strcpy(buf,str);
	printf("%s\n",buf);


	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
Hello World!
chunli魂斗羅~$


字符轉(zhuǎn)數(shù)字

chunli魂斗羅~$ cat main.c 
#include <stdio.h>

int char_to_int(char *str)
{
	int num = 0;
	while(*str != '\0')
	{
		num = num*10+*str-48;;
		str++;
	}
	return num;
}

int main()
{
	printf("%d\n",char_to_int("1213141516"));


	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
1213141516
chunli魂斗羅~$


求一個(gè)數(shù)的右4位

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
int main()
{
	unsigned int n = 0x12345678;
	n = n<<28;
	n = n>>28;
	printf("%x\n",n);
	return 0;
}
chunli魂斗羅~$ gcc main.c && ./a.out
8
chunli魂斗羅~$


數(shù)組定義問題1,sizeof編譯時(shí)為常量

chunli?魂斗羅~$ cat main.c 
#include <stdio.h>
int main()
{
	char arr[sizeof(char)];
	printf("%ld\n",sizeof(arr));
	return 0;
}
chunli?魂斗羅~$ gcc main.c  -Wall && ./a.out 
1
chunli?魂斗羅~$



數(shù)組定義問題2,const編譯時(shí)為常量

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
int main()
{
	const int a = 5;
	char arr[a];
	printf("%ld\n",sizeof(arr));
	return 0;
}
chunli魂斗羅~$ gcc main.c  -Wall && ./a.out 
5
chunli魂斗羅~$


數(shù)組定義問題1,const編譯時(shí)為常量,

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
int main()
{
	int a = 5;
	const char brr[a];//無(wú)法初始化
	char const arr[a];
	printf("%ld\n",sizeof(brr));
	printf("%ld\n",sizeof(arr));
	return 0;
}
chunli魂斗羅~$ gcc main.c  -Wall && ./a.out 
5
5
chunli魂斗羅~$


函數(shù)指針面試題1:

void*(*(*fun)(int))[10]

分析1:這是指針數(shù)組

分析2:fun為函數(shù)指針,參數(shù)類型為int,返回值為void*

綜合:數(shù)組里面有10個(gè)元素,每個(gè)元素都指向一個(gè)void*的函數(shù)指針


函數(shù)指針面試題3:[超難]

int(*(*fun)())[10]();

fun是一個(gè)指針,指向一個(gè)函數(shù),參數(shù)為空,返回值是指針,指針指向一個(gè)數(shù)組,

數(shù)組有10個(gè)元素,每個(gè)元素都是一個(gè)指針,指向一個(gè)參數(shù)為空返回值為int類型的函數(shù)




多態(tài)中虛函數(shù)表是編譯時(shí)建立的,運(yùn)行時(shí)調(diào)用




回調(diào)函數(shù)

chunli魂斗羅~$ cat main.c 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *fun2(char *str){
	char *p = (char*)malloc(100);
	strcpy(p,str);
	return p;
}
char *fun1(char* (*p)(char *pp1),char *p2){
	return p(p2);
}
int main(){
	char *str = "abcdefghijkl";
	char *p = fun1(fun2,str);
	printf("%s\n",p);
	free(p);
	p = NULL;
	return 0;
}
chunli魂斗羅~$ gcc main.c  -Wall && ./a.out 
abcdefghijkl
chunli魂斗羅~$




C/C++ 面試題

C/C++ 面試題

C/C++ 面試題

C/C++ 面試題

C/C++ 面試題

C/C++ 面試題

C/C++ 面試題


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI