C语言练习题带答案

5.

#include main( )

{ int a[ ]={1, 2, 3, 4, 5, 6}; int x, y, *p; p = &a[0]; x = *(p+2); y = *(p+4);

printf(“*p=%d, x=%d, y=%d\\n”, *p, x, y); }

运行结果为: *p=1, x=3, y=5

6.

#include main( )

{ static char a[ ]=”Program”, *ptr; for(ptr=a, ptr

运行结果为: Prom 7.

#include char s[]=”ABCD”; main() { char *p;

for(p=s;p

printf(“%c %s\\n”,*p,p); }

运行结果为: A ABCD B BCD C CD D D

结构体

1.

#include struct st { int x; int y;

} a[2]={5, 7, 2, 9} ; main() {

printf(\}

运行结果是: 14

2.

#include main( )

{struct stu {int num;

char a[5]; float score;

}m={1234,”wang”,89.5}; printf(“%d,%s,%f”,m.num,m.a,m.score); }

运行结果是: 1234,wang,89.5 3.

#include struct cmplx { int x; int y;

} cnum[2]={1, 3, 2, 7}; main( ) {

printf(“%d\\n”, cnum[0].y * cnum[1].x ); }

运行结果是:6

4.

#include struct abc { int a, b, c; }; main()

{ struct abc s[2]={{1,2,3},{4,5,6}}; int t;

t=s[0].a+s[1].b; printf(\}

运行结果是:6

三、 程序填空

1.输入一个字符,判断该字符是数字、字母、空格还是其他字符。

main( ) { char ch; ch=getchar();

if( ch>=’a’&&ch<=’z’|| ch>=’A’&&ch<=’Z’ ) printf(\ else if( ch>=’0’&&ch<=’9’ )

printf(\else if( ch== ‘ ’ )

printf(\ else

printf(\ }

2.下列程序的功能是从输入的整数中,统计大于零的整数个数和小于零的整数个数。用输入0来结束输入,用i,j来放统计数,请填空完成程序。 void main()

{ int n,i=0,j=0;

printf(“input a integer,0 for end\\n”); scanf(“%d”,&n);

while ( n或n!=0 ) {

if(n>0) i= i+1 ;

else j=j+1; }

printf(“i=M,j=M\\n”,i,j); }

3.编程计算1+3+5+……+101的值

#include void main() {

int i, sum = 0;

for (i = 1; i<=101 ; i++ )

sum = sum + i;

printf(\ }

4.编程计算1+3+5…+99的值

main()

{ int i, sum = 0;

i=1;

while ( i<100 )

{

sum = sum + i;

i++ ; } printf(\}

5.从键盘输入一个字符,判断它是否是英文字母。 #include void main() {char c;

printf(\ c=getchar();

if(c>= ‘A’ &&c<= ‘Z’ || c>=‘a’ &&c<= ‘z’) printf(\\\n\ else printf(\}

6. 下面程序的功能是在a数组中查找与x值相同的元素所在位置,请填空。 #include void main() { int a[10],i,x;

printf(“input 10 integers:”); for(i=0;i<10;i++)

scanf(“%d”,&a[i]);

printf(“input the number you want to find x:”);

scanf(“%d”, &x );

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

if( x==a[i] ) break;

if( i<10 )

printf(“the pos of x is:%d\\n”,i);

else printf(“can not find x!\\n”); }

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4