int main() { int i, fss; char ch, source["> int main() { int i, fss; char ch, source[">
logo

CuriousTab

CuriousTab

Discussion


Home C Programming Input / Output See What Others Are Saying!
  • Question
  • On executing the below program what will be the contents of 'target.txt' file if the source file contains a line "To err is human"?
    #include<stdio.h>
    
    int main()
    {
        int i, fss;
        char ch, source[20] = "source.txt", target[20]="target.txt", t;
        FILE *fs, *ft;
        fs = fopen(source, "r");
        ft = fopen(target, "w");
        while(1)
        {
            ch=getc(fs);
            if(ch==EOF)
                break;
            else
            {
                fseek(fs, 4L, SEEK_CUR);
                fputc(ch, ft);
            }
        }
        return 0;
    }
    


  • Options
  • A. r n
  • B. Trh
  • C. err
  • D. None of above

  • Correct Answer
  • Trh 

    Explanation
    The file source.txt is opened in read mode and target.txt is opened in write mode. The file source.txt contains "To err is human".

    Inside the while loop,

    ch=getc(fs); The first character('T') of the source.txt is stored in variable ch and it's checked for EOF.

    if(ch==EOF) If EOF(End of file) is true, the loop breaks and program execution stops.

    If not EOF encountered, fseek(fs, 4L, SEEK_CUR); the file pointer advances 4 character from the current position. Hence the file pointer is in 5th character of file source.txt.

    fputc(ch, ft); It writes the character 'T' stored in variable ch to target.txt.

    The while loop runs three times and it write the character 1st and 5th and 11th characters ("Trh") in the target.txt file.


    More questions

    • 1. In C all functions except main() can be called recursively.

    • Options
    • A. True
    • B. False
    • Discuss
    • 2. Input/output function prototypes and macros are defined in which header file?

    • Options
    • A. conio.h
    • B. stdlib.h
    • C. stdio.h
    • D. dos.h
    • Discuss
    • 3. For a function receives variable number of arguments it is necessary that the function should receive at least one fixed argument.

    • Options
    • A. True
    • B. False
    • Discuss
    • 4. What will be the output of the program?
      #include<stdio.h>
      int X=40;
      int main()
      {
          int X=20;
          printf("%d\n", X);
          return 0;
      }
      

    • Options
    • A. 20
    • B. 40
    • C. Error
    • D. No Output
    • Discuss
    • 5. What will be the output of the program (myprog.c) given below if it is executed from the command line?
      cmd> myprog one two three
      /* myprog.c */
      #include<stdio.h>
      #include<stdlib.h>
      
      int main(int argc, char **argv)
      {
          int i;
          for(i=1; i<=3; i++)
              printf("%u\n", &argv[i]);
          return 0;
      }
      
      If the first value printed by the above program is 65517, what will be the rest of output?


    • Options
    • A. 65525 65531
    • B. 65519 65521
    • C. 65517 65517
    • D. 65521 65525
    • Discuss
    • 6. What will be the output of the program?
      #include<stdio.h>
      #include<stdlib.h>
      
      int main()
      {
          int *p;
          p = (int *)malloc(20); /* Assume p has address of 1314 */
          free(p);
          printf("%u", p);
          return 0;
      }
      

    • Options
    • A. 1314
    • B. Garbage value
    • C. 1316
    • D. Random address
    • Discuss
    • 7. Size of short integer and long integer would vary from one platform to another.

    • Options
    • A. True
    • B. False
    • Discuss
    • 8. Names of functions in two different files linked together must be unique

    • Options
    • A. True
    • B. False
    • Discuss
    • 9. What will be the output of the program?
      #include<stdio.h>
      
      int main()
      {
          int arr[2][2][2] = {10, 2, 3, 4, 5, 6, 7, 8};
          int *p, *q;
          p = &arr[1][1][1];
          q = (int*) arr;
          printf("%d, %d\n", *p, *q);
          return 0;
      }
      

    • Options
    • A. 8, 10
    • B. 10, 2
    • C. 8, 1
    • D. Garbage values
    • Discuss
    • 10. The macro va_arg is used to extract an argument from the variable argument list and advance the pointer to the next argument.

    • Options
    • A. True
    • B. False
    • Discuss


    Comments

    There are no comments.

Enter a new Comment