Custom Search

Thursday, March 15, 2007

C - Review II 8 - 11

8. main(){

int i;

i=1,2,3,4;

printf(“%d”,i);

}

output: 1
i=1 is one expression. After that 2, 3, 4 are taken as three statemets. C will ignore those three.

9. Is this code correct? If not, suggest possible modifications.

main(){

int i,j;

i=10; j=20;

i > j ? i : j = 0;

printf(“i=%d, j=%d”,i,j);

}

Output: Lvalue required

Modify the expression as *(i > j ? i : j) = 0;

The larger one will be assigned to zero.
10. main(){

int a[10],i;

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

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

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

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

}

input : 1 2 3 4 5 6 7 8 9 1

output : 1 2 3 4 5 6 7 8 9 1

a[i] and i[a] are similar


11. main(){

char *p;

gets(p);

puts(p);

}

Output: segmentation fault.

We are using the pointer without allocating memory

No comments: