int main() { int i, fss; char ch, source["> int main() { int i, fss; char ch, source[">
#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; }
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.
#include<stdio.h> int main() { FILE *fp; fp=fopen("trial", "r"); return 0; }
#include<stdio.h> int main() { FILE *fp; int t; fp = fopen("DUMMY.C", "w"); t = fileno(fp); printf("%d\n", t); return 0; }
t = fileno(fp); returns the handle for the fp stream and it stored in the variable t
printf("%d\n", t); It prints the handle number.
FILE *fp; fp = fopen("source.txt", "rb");
#include<stdio.h> int main() { FILE *fs, *ft, *fp; fp = fopen("A.C", "r"); fs = fopen("B.C", "r"); ft = fopen("C.C", "r"); fclose(fp, fs, ft); return 0; }
fgets reads characters from stream into the string s. It stops when it reads either n - 1 characters or a newline character, whichever comes first.
Therefore, the string str contain "I am a boy\n\0"
FILE *fp; fp = fopen("NOTES.TXT", "r+");
#include<stdio.h> int fun(int **ptr); int main() { int i=10; const int *ptr = &i; fun(&ptr); return 0; } int fun(int **ptr) { int j = 223; int *temp = &j; printf("Before changing ptr = %5x\n", *ptr); const *ptr = temp; printf("After changing ptr = %5x\n", *ptr); return 0; }
#include<stdio.h> int fun(int *f) { *f = 10; return 0; } int main() { const int arr[5] = {1, 2, 3, 4, 5}; printf("Before modification arr[3] = %d", arr[3]); fun(&arr[3]); printf("\nAfter modification arr[3] = %d", arr[3]); return 0; }
arr[0] = 1, arr[1] = 2, arr[2] = 3, arr[3] = 4, arr[4] = 5
Step 2: printf("Before modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 4).
Step 3: fun(&arr[3]); The memory location of the arr[3] is passed to fun() and arr[3] value is modified to 10.
A const variable can be indirectly modified by a pointer.
Step 4: printf("After modification arr[3] = %d", arr[3]); It prints the value of arr[3] (ie. 10).
Hence the output of the program is
Before modification arr[3] = 4
After modification arr[3] = 10
#include<stdio.h> int get(); int main() { const int x = get(); printf("%d", x); return 0; } int get() { return 20; }
Step 2: const int x = get(); The constant variable x is declared as an integer data type and initialized with the value "20".
The function get() returns the value "20".
Step 3: printf("%d", x); It prints the value of the variable x.
Hence the output of the program is "20".
#include<stdio.h> int main() { int y=128; const int x=y; printf("%d\n", x); return 0; }
Step 2: const int x=y; The constant variable 'x' is declared as an integer and it is initialized with the variable 'y' value.
Step 3: printf("%d\n", x); It prints the value of variable 'x'.
Hence the output of the program is "128"
Comments
There are no comments.Copyright ©CuriousTab. All rights reserved.