- 相关推荐
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,”%d\n”,a[j]);
fclose(fp1);
fclose(fp2);
return 0;
}
可谓是反序的经典例程.
void inverse(char *p)
{
if( *p = = ‘\0′ )
return;
inverse( p+1 );
printf( “%c”, *p );
}
int main(int argc, char *argv[])
{
inverse(“abc\0″);
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
【C语言编程题】相关文章:
c 面试编程问题08-11
c语言课程设计的心得体会(精选19篇)07-14
C/C++笔试题目大全08-22
VB编程:使用枚举的情况05-15
《维生素c的故事》教学反思10-14
C++面试笔试题目07-16
IQ题笔试题07-06
家乐福笔试真题07-01
阿里巴巴C++工程师笔试题目10-17
笔试中的硬件类题09-24