Any one can explain this question to me including the output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Any one can explain this question to me including the output?

Part D. On executing the below program, what will be the output of the following program if the source file contains a line "This is a sample."? [5 marks] #include<stdio.h> int main() { char ch; int k, n=0; FILE * fs = fopen("source.txt", "r"); while(1) { ch = fgetc(fs); if(ch == EOF) break; else { for(k=0;k<4;k++) fgetc(fs); printf("%c", ch); n += k; } } printf("%d\n", n); fclose(fs); return 0; }

31st Mar 2020, 8:29 AM
Jiayang Zhu
1 Answer
0
The program essentially grabs a character from the file and skips the following four characters, until the end of the file is encountered. For example, in the first iteration of the while-loop, we retrieve and store 'T' in "ch", then get the next four letters "his ", but don't store them, and print ch. Since the for-loop always runs four times, 'n' is incremented by that each time. If you follow this cycle, you get the letters "Tise" and 'n' will equal 16, since the while-loop runs four times until EOF is encountered, thus n = 4* 4.
31st Mar 2020, 11:06 AM
Shadow
Shadow - avatar