Debug Ques

8
test 1: printf("test %s\n", NULL); printf("test %s\n", NULL); prints: test (null) test (null) test 2: printf("%s\n", NULL); printf("%s\n", NULL); prints Segmentation fault (core dumped) what is the difference in the above tests ? Why is segmentation fault not thrown in test 1 above ? 0 of 0 votes 32 Answers What is wrong with this program : main() { char *p,*q; p=(char *)malloc(25); q=(char*) malloc(25); strcpy(p,"amazon" ); strcpy(q,"hyd"); strcat(p,q); printf("%s",p); }

description

c qns

Transcript of Debug Ques

Page 1: Debug Ques

test 1: printf("test %s\n", NULL); printf("test %s\n", NULL);

prints: test (null) test (null)

test 2: printf("%s\n", NULL); printf("%s\n", NULL); prints Segmentation fault (core dumped)

what is the difference in the above tests ? Why is segmentation fault not thrown in test 1 above ?

0

of 0 votes

32 Answers

What is wrong with this program : main() { char *p,*q; p=(char *)malloc(25); q=(char*) malloc(25); strcpy(p,"amazon" ); strcpy(q,"hyd"); strcat(p,q); printf("%s",p); }

0

of 0 votes

Page 2: Debug Ques

22 Answers

what is the output of for(;0;) printf("\n guess");

What will be the output?

main()

{

int i=3;

switch(i)

{

default:printf( "zero");

case 1: printf("one" );

break;

case 2:printf("two" );

break;

case 3: printf("three" );

break;

}

}

Answer :

three

Explanation :

The default case can be placed anywhere inside the loop. It is executed only when all other cases doesn't match.

What s the error in this snippet.

Page 3: Debug Ques

main()

{

extern int i;

i=20;

printf("%d", i);

}

What is the o/p explain.

main()

{

float me = 1.1;

double you = 1.1;

if(me==you)

printf("I love U");

else

printf("I hate U");

}

find the error ..

main()

{

float me = 1.1;

double you = 1.1;

if(me==you)

printf("I love U");

else

printf("I hate U");

}

Page 4: Debug Ques

main()sturct date {char name[20];int age ;float sal;};sturct data d ={"rajesh"};printf("%d%f",d.age,d.sal);}tell the output & explain

9.What is the error :main(){int j=10;switch(j){ case 20:pritnf("Less than 20");break;case 30:printf("Less than 30");break;default:printf("hello");}

15.main(){int i;if(i=0) //it,s assisnment not logical operatorprintf(" Hell ");elseprintf("Heaven");}Output & explain..

What looks wrong with these programs? #include <stdio.h> #include <stdlib.h>

Page 5: Debug Ques

main() { int i; for (i=0; i<10; i=i+1); printf("i is %d\n",i); } #include <stdio.h> #include <stdlib.h> main() { int numbers[10]; int i; for (i=1;i<=10;i++) numbers[i]=i; for (i=1;i<=10;i++) printf("numbers[%d]=%d\n", i, numbers[i]); } #include <stdio.h> #include <stdlib.h> main() { int i; for (i=0; i<10; i=i+1) if (i=2) printf("i is 2\n"); else printf("i is not 2\n"); } #include <stdio.h> #include <stdlib.h> main() { int i; for (i=0; i<10; i=i+1) if (i<2) printf("%d is less than 2\n",i); printf("and %d is not equal to, 2 either\n",i); } #include <stdio.h> #include <stdlib.h> main() { int i; i = 0; while (i < 10); i = i + 1;

Page 6: Debug Ques

printf("Finished. i = %d\n",i); } #include <stdio.h> #include <stdlib.h> main() { int i; for (i=0; i<10; i=i+1) switch(i){ case 0: printf("i is 0\n"); case 1: printf("i is 1\n"); default: printf("i is more than 1\n"); } } #include <stdio.h> #include <stdlib.h> main() { int i; for (i=0; i<10; i=i+1) /* check the value of i*/ switch(i){ /* is i 0? case 0: printf("i is 0\n"); break; /* is i 1? case 1: printf("i is 1\n"); break; /* now the default case */ default: printf("i is more than 1\n"); } } #include <stdio.h> #include <stdlib.h> main() { int i; i=3; i=i+2*i++; printf("i is now %d\n",i); } #include <stdio.h> int main() { int a,b,c;

Page 7: Debug Ques

int *pointer; c = 3; pointer = &c; /* divide c by itself */ a = c/*pointer; b = c /* set b to 3 */; printf("a=%d, b=%d\n", a,b); } When you have an if-else statement nested in another if statement,

always put braces around the if-else. Thus, never write like this: if (foo) if (bar) win (); else lose ();

(the else matches the closest if), always like this:

if (foo) { if (bar) win (); else lose (); }

scanf takes pointers to the variables that are going to be set. The following fragment will cause a crash

... int i; scanf("%d",i); /* this should be scanf("%d",&i) */