Sunday, April 6, 2014

C PUZZLE


  •    main(){

      char name[10],s[12];
            scanf(" "%[^"]"",s);
      }
      How scanf will execute?
Answer:
First it checks for the leading white space and discards it.Then it matches with a quotation mark and then it  reads all character upto another quotation mark.

      What is the problem with the following code segment?
      while ((fgets(receiving array,50,file_ptr)) != EOF)               ;
Answer & Explanation:
fgets returns a pointer. So the correct end of file check is checking for != NULL.

  •    main(){

      main();
      }
Answer:
Runtime error : Stack overflow.
Explanation:
main function calls itself again and again. Each time the function is called its return address is stored in the call stack. Since there is no condition to terminate the function call, the call stack overflows at runtime. So it terminates the program and results in an error.

  •   main(){

      char *cptr,c;
            void *vptr,v;
      c=10;  v=0;
            cptr=&c; vptr=&v;
      printf("%c%v",c,v);
      }
Answer:
Compiler error (at line number 4): size of v is Unknown.
Explanation:
You can create a variable of type void * but not of type void, since void is an empty type. In the second line you are creating variable vptr of type void * and v of type void hence an error.

  •    main()      {

      char *str1="abcd";
            char str2[]="abcd";
      printf("%d %d %d",sizeof(str1),sizeof(str2),sizeof("abcd"));
      }
Answer:
2 5 5
Explanation:
In first sizeof, str1 is a character pointer so it gives you the size of the pointer variable. In second sizeof the name str2 indicates the name of the array whose size is 5 (including the

Related Posts by Categories

0 comments:

Post a Comment