当前位置:文档之家› C语言面试题和答案

C语言面试题和答案

1.什么是编辑器,编译器,源代码,目标代码?略2.编写一个最简单的程序。

答案:void main(void) {} 或void main(void){}3.C语言有哪两种存储数值的方式?答案:变量和常量4.请写出下列代码的输出内容#include <stdio.h>int main(void){int a,b,c,d;a=10;b=a++;c=++a;d=10*a++;printf("b,c,d:%d,%d,%d",b,c,d); return 0;}答:10,12,1205. 请写出下列代码的输出内容#include <stdio.h>int a,b;int main(void){a =b = 5;/* Print them, decrementing each time. *//* Use prefix mode for b, postfix mode for a */printf("\nPost Pre");printf("\n%d %d",a--,--b);printf("\n%d %d",a--,--b);printf("\n%d %d",a--,--b);printf("\n%d %d",a--,--b);printf("\n%d %d\n",a--,--b);system("PAUSE");return 0;}答案:5 44 33 22 11 06.请写出下列代码的输出内容#include <stdio.h>/* Initialize variables. Note that c is not less than d, *//* which is one of the conditions to test for. *//* Therefore, the entire expression should evaluate as false.*/int a = 5, b = 6, c = 5, d = 1;int x;int main( void ){/* Evaluate the expression without parentheses */x = a < b || a < c && c < d;printf("\nWithout parentheses the expression evaluates as %d", x);/* Evaluate the expression with parentheses */x = (a < b || a < c) && c < d;printf("\nWith parentheses the expression evaluates as %d\n", x);return 0;}答案:Without parentheses the expression evaluates as 1With parentheses the expression evaluates as 07.下面表达式的值是多少?10 % 3 * 3 —(1 + 2)答案:08.请写出下列代码的输出内容/* Demonstrates function recursion. Calculates the *//* factorial of a number. */#include <stdio.h>unsigned int f, x;unsigned int factorial(unsigned int a);int main( void ){puts("Enter an integer value between 1 and 8: ");scanf("%d", &x);if( x > 8 || x < 1){printf("Only values from 1 to 8 are acceptable!");}else{f = factorial(x);printf("%u factorial equals %u\n", x, f);}return 0;}unsigned int factorial(unsigned int a){if (a == 1)return 1;else{a *= factorial(a-1);return a;}}9.请写出下列代码的输出内容/* Demonstrates nesting two for statements */#include <stdio.h>void draw_box( int, int);int main( void ){draw_box( 4, 9 );return 0;}void draw_box( int row, int column ){int col;for ( ; row > 0; row--){for (col = column; col > 0; col--)printf("X");printf("\n");}}答案:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX10.用C语言写出冒泡排序。

#include <stdio.h>main(){ int a[10];int i,j,t ;printf("input 10 numbers:");for(i=0;i<=9;i++)scanf("%d",&a[i]);printf("\n");for(j=0;j<=9;j++)for(i=0;i<=9-j;i++)if(a[i]>a[i+1]){t=a[i];a[i]=a[i+1];a[i+1]=t;} printf("the sorted number:\n");for(i=0;i<=9;i++)printf("%d ",a[i]);}11.在Windows下,写出运行结果。

#include <stdio.h>/* Declare several 100-element arrays */int intarray[100];float floatarray[100];double doublearray[100];char chararray[] = "12345";char *p = chararray;int n = 10;int main(){printf("\nSize of intarray = %d bytes", sizeof(intarray));printf("\nSize of floatarray = %d bytes", sizeof(floatarray));printf("\nSize of doublearray = %d bytes\n", sizeof(doublearray));printf("\nSize of chararray = %d bytes\n", sizeof(chararray));printf("\nSize of n = %d bytes\n", sizeof(n));printf("\nSize of p= %d bytes\n", sizeof(p));}答案:Size of intarray =400Size of floatarray =400Size of doublearray=800Size of chararray =6Size of n =4Size of p =412.下面语句声明的分别是什么?A. int *var1;B. int var2;C. int **var3;D. int a[3][12];E. int (*b)[12];F. int *c[12];答案:A. var1是一个int指针;B. var2是一个int变量;C. var3是一个指向int指针的指针;D. a 是一个包含36个int元素的数组;E. b是一个指针,指向一个包含12个元素的int数组;F. c是一个包含12元素的数组,其中的元素为int指针。

13.使用strcpy()函数写一段代码/* Demonstrates strcpy(). */#include <stdlib.h>#include <stdio.h>#include <string.h>char source[] = "The source string.";int main( void ){char dest1[80];char *dest2, *dest3;printf("\nsource: %s", source );/* Copy to dest1 is okay because dest1 points to *//* 80 bytes of allocated space. */strcpy(dest1, source);printf("\ndest1: %s", dest1);/* To copy to dest2 you must allocate space. */dest2 = (char *)malloc(strlen(source) +1);strcpy(dest2, source);printf("\ndest2: %s\n", dest2);/* Copying without allocating destination space is a no-no. *//* The following could cause serious problems. *//* strcpy(dest3, source); */return 0;}14.请填写BOOL , float, 指针变量与“零值”比较的if 语句。

相关主题