2023c语言面试题

康华0 分享 时间:

这是一份比较全面的PHP面试题,对准备去新公司应聘PHP职位的开发者应该有帮助。下面是小编为大家整理的2023c语言面试题,如果大家喜欢可以分享给身边的朋友。

2023c语言面试题

2023c语言面试题篇1

c语言面试编程题

1、读文件 file1.txt 的'内容(例如):

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、写一段程序,找出数组中第 k 大小的数,输出数所在的位置,例如{2,4,3,4,7}中,第一大的数是 7,位置在 4。第二大、第三大的数都是 4,位置在 1、3 随便输出哪一个均可。

函数接口为:int find_orderk(const int__ narry,const int n,const int k)

要求算法复杂度不能是 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的'内容(例如):

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;

}

可谓是反序的经典例程.

void inverse(char __p)

{

if( __p = = ‘′ )

return;

inverse( p+1 );

printf( “%c”, __p );

}

int main(int argc, char __argv[])

{

inverse(“abc″);

return 0;

}

借签了楼上的“递规反向输出”

#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=____;

希望各位达人给出答案和原因,谢谢拉

解答:假设在32位CPU上,

sizeof(long) = 4 bytes

sizeof(char __) = 4 bytes

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

sizeof(char) = 1 bytes

2023c语言面试题篇3

考查的是结构体和数组的内存布局情况。

#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

从字符串数组和指针字符串在内存中的分配情况考查指针的使用。

#include

#include

#include

char __GetMemory(char __p)

{

p = (char __)malloc(100);

return p;

}//当调用此函数时,会在栈里分配一个空间存储p, p指向堆当中的一块内存区,当函数调用结束后,若函数没有返回值,

//系统自动释放栈中的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”; 就会在函数调用结束后,释放掉为“Test1”的拷贝分配的空间,返回的P只是一个野指针

void Test1

{

char __str = “”;

str=GetMemory1;

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

//str=GetMemory;

}

void GetMemory2(char p, int num)

{

__p = (char __)malloc(num);

}//当调用此函数时,会在栈里分配一个空间存储p, p指向栈中的一变量str,在此函数中为str在堆当中分配了一段内存空间

//函数调用结束后,会释放p, 但str所在的函数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));//字符串数组的大小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));//字符串数组的大小100

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

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

}

1436256