2023c語言面試題

康華0 分享 時間:

這是一份比較全面的PHP面試題,對準備去新公司應(yīng)聘PHP職位的開發(fā)者應(yīng)該有幫助。下面是小編為大家整理的2023c語言面試題,如果大家喜歡可以分享給身邊的朋友。

2023c語言面試題

2023c語言面試題篇1

c語言面試編程題

1、讀文件 file1.txt 的'內(nèi)容(例如):

12

34

56

輸出到 file2.txt:

56

34

12

#include

#include

int main(void)

{

int MAX = 10;

int __a = (int __)malloc(MAX __ sizeof(int));

int __b;

FILE __fp1;

FILE __fp2;

fp1 = fopen(“a.txt”,“r”);

if(fp1 == NULL)

{printf(“error1”);

exit(-1);

}

fp2 = fopen(“b.txt”,“w”);

if(fp2 == NULL)

{printf(“error2”);

exit(-1);

}

int i = 0;

int j = 0;

while(fscanf(fp1,“%d”,&a[i]) != EOF)

{

i++;

j++;

if(i >= MAX)

{

MAX = 2 __ MAX;

b = (int__)realloc(a,MAX __ sizeof(int));

if(b == NULL)

{

printf(“error3”);

exit(-1);

}

a = b;

}

}

for(;--j >= 0;)

fprintf(fp2,“%dn”,a[j]);

fclose(fp1);

fclose(fp2);

return 0;

}

2、寫一段程序,找出數(shù)組中第 k 大小的數(shù),輸出數(shù)所在的位置,例如{2,4,3,4,7}中,第一大的數(shù)是 7,位置在 4。第二大、第三大的數(shù)都是 4,位置在 1、3 隨便輸出哪一個均可。

函數(shù)接口為:int find_orderk(const int__ narry,const int n,const int k)

要求算法復(fù)雜度不能是 O(n^2)

可以先用快速排序進行排序,其中用另外一個進行地址查找代碼如下,在 VC++6.0 運行通過。

//快速排序

#include

usingnamespacestd;

intPartition (int__L,intlow,int high)

{

inttemp = L[low];

intpt = L[low];

while (low < high)

{

while (low < high && L[high] >= pt)

--high;

L[low] = L[high];

while (low < high && L[low] <= pt)

++low;

L[low] = temp;

}

L[low] = temp;

returnlow;

}

voidQSort (int__L,intlow,int high)

{

if (low < high)

{

intpl = Partition (L,low,high);

QSort (L,low,pl - 1);

QSort (L,pl + 1,high);

}

}

intmain

{

intnarry[100],addr[100];

intsum = 1,t;

cout << “Input number:” << endl;

cin >> t;

while (t != -1)

{

narry[sum] = t;

addr[sum - 1] = t;

sum++;

cin >> t;

}

sum -= 1;

QSort (narry,1,sum);

for (int i = 1; i <= sum;i++)

cout << narry[i] << 't';

cout << endl;

intk;

cout << “Please input place you want:” << endl;

cin >> k;

intaa = 1;

intkk = 0;

for (;;)

{

if (aa == k)

break;

if (narry[kk] != narry[kk + 1])

{

aa += 1;

kk++;

}

}

cout << “The NO.” << k << “number is:” << narry[sum - kk] << endl;

cout << “And it's place is:” ;

for (i = 0;i < sum;i++)

{

if (addr[i] == narry[sum - kk])

cout << i << 't';

}

return0;

}

2023c語言面試題篇2

C語言編程題

1)讀文件file1.txt的'內(nèi)容(例如):

12

34

56

輸出到file2.txt:

56

34

12

(逆序)

第一題,注意可增長數(shù)組的應(yīng)用.

#include

#include

int main(void)

{

int MAX = 10;

int __a = (int __)malloc(MAX __ sizeof(int));

int __b;

FILE __fp1;

FILE __fp2;

fp1 = fopen(“a.txt”,”r”);

if(fp1 == NULL)

{printf(“error1″);

exit(-1);

}

fp2 = fopen(“b.txt”,”w”);

if(fp2 == NULL)

{printf(“error2″);

exit(-1);

}

int i = 0;

int j = 0;

while(fscanf(fp1,”%d”,&a[i]) != EOF)

{

i++;

j++;

if(i >= MAX)

{

MAX = 2 __ MAX;

b = (int__)realloc(a,MAX __ sizeof(int));

if(b == NULL)

{

printf(“error3″);

exit(-1);

}

a = b;

}

}

for(;–j >= 0;)

fprintf(fp2,”%dn”,a[j]);

fclose(fp1);

fclose(fp2);

return 0;

}

可謂是反序的經(jīng)典例程.

void inverse(char __p)

{

if( __p = = ‘′ )

return;

inverse( p+1 );

printf( “%c”, __p );

}

int main(int argc, char __argv[])

{

inverse(“abc″);

return 0;

}

借簽了樓上的“遞規(guī)反向輸出”

#include

void test(FILE __fread, FILE __fwrite)

{

char buf[1024] = {0};

if (!fgets(buf, sizeof(buf), fread))

return;

test( fread, fwrite );

fputs(buf, fwrite);

}

int main(int argc, char __argv[])

{

FILE __fr = NULL;

FILE __fw = NULL;

fr = fopen(“data”, “rb”);

fw = fopen(“dataout”, “wb”);

test(fr, fw);

fclose(fr);

fclose(fw);

return 0;

}

在對齊為4的情況下

struct BBB

{

long num;

char __name;

short int data;

char ha;

short ba[5];

}__p;

p=0×1000000;

p+0×200=____;

(Ulong)p+0×200=____;

(char__)p+0×200=____;

希望各位達人給出答案和原因,謝謝拉

解答:假設(shè)在32位CPU上,

sizeof(long) = 4 bytes

sizeof(char __) = 4 bytes

sizeof(short int) = sizeof(short) = 2 bytes

sizeof(char) = 1 bytes

2023c語言面試題篇3

考查的是結(jié)構(gòu)體和數(shù)組的內(nèi)存布局情況。

#include

#include

typedef struct array1{

int ID;

struct array1__ next;

}A;

typedef struct array2{

int ID;

int a;

int b;

int c;

}__ B;

int main

{

A s1[15];

A__ s2;

B s3;

for(int i=0;i<10;i++)

{

s1[i].ID=i+64;

}

s2=s1+3;

s3=(B)s2;

printf(“%d/n”,s3->b);

return 0;

}

2023c語言面試題篇4

從字符串數(shù)組和指針字符串在內(nèi)存中的分配情況考查指針的使用。

#include

#include

#include

char __GetMemory(char __p)

{

p = (char __)malloc(100);

return p;

}//當調(diào)用此函數(shù)時,會在棧里分配一個空間存儲p, p指向堆當中的一塊內(nèi)存區(qū),當函數(shù)調(diào)用結(jié)束后,若函數(shù)沒有返回值,

//系統(tǒng)自動釋放棧中的P

void Test(void)

{

char __str = NULL;

str=GetMemory(str);

strcpy(str, “test”);

printf(“%s/n”,str);

}

char __GetMemory1(void)

{

char __p = “Test1”;

return p;

}//若換成char p=“hello world”; 就會在函數(shù)調(diào)用結(jié)束后,釋放掉為“Test1”的拷貝分配的空間,返回的P只是一個野指針

void Test1

{

char __str = “”;

str=GetMemory1;

printf(“%s/n”,str);

//str=GetMemory;

}

void GetMemory2(char p, int num)

{

__p = (char __)malloc(num);

}//當調(diào)用此函數(shù)時,會在棧里分配一個空間存儲p, p指向棧中的一變量str,在此函數(shù)中為str在堆當中分配了一段內(nèi)存空間

//函數(shù)調(diào)用結(jié)束后,會釋放p, 但str所在的函數(shù)Test2還沒運行完,所以str此時還在棧里.

void Test2(void)

{

char __str = NULL;

GetMemory2(&str, 100);

strcpy(str, “hello”);

printf(“%s/n”,str);

}

void Test3(void)

{

char __str=(char __)malloc(100);

strcpy(str, “hello”);//此時的str指向的是拷貝到棧里的“hello”,所以當釋放掉str指向的堆空間時,str指向的棧里的值還是不變

free(str);

if(str != NULL)

{

strcpy(str, “world”);

printf(“%s/n”,str);

}

}

int main

{

Test;

Test1;

Test2;

Test3;

}

2023c語言面試題篇5

C語言中sizeof的用法

void fun(char s[10])

{

printf(“%s/n”,s);

printf(“%d/n”,sizeof(s));//引用的大小

}

int main

{

char str={“sasdasdes”};

printf(“%d/n”,sizeof(str));//字符串數(shù)組的大小10(包含了字符'/0')

printf(“%d/n”,strlen(str)));//字符串的長度9

char __p=str;

printf(“%d/n”,sizeof(p));//指針的大小4

printf(“%d/n”,strlen(p));//字符串的長度9

fun(str);

void __h=malloc(100);

char ss[100]=“abcd”;

printf(“%d/n”,sizeof(ss));//字符串數(shù)組的大小100

printf(“%d/n”,strlen(ss));//字符串的長度4

printf(“%d/n”,sizeof(h));//指針的大小4

}

1436256