Programming in C Unit V
Programming in C Unit V
Example:
extern int c;
register int d;
Example:
void func() {
auto int a = 5;
printf("%d\n", a);
Example:
void counter() {
count++;
printf("%d\n", count);
}
4. Explain the extern storage class with example.
Declares a variable defined elsewhere (e.g., another file).
Example:
File1.c:
int x = 100;
File2.c:
extern int x;
printf("%d\n", x);
Example:
void func() {
register int i = 0;
void func() {
void f() {
8. How does the scope of a static variable inside a function differ from a local variable?
Static variable scope is limited to function but value persists between calls.
Example:
void func() {
static int x = 0;
x++;
printf("%d\n", x);
Program:
#include <stdio.h>
int main() {
} else {
printf("File opened successfully.\n");
fclose(fp);
return 0;
if (fp == NULL) {
Example:
FILE *fp = fopen("file.txt", "r");
char ch;
putchar(ch);
fclose(fp);
Example:
FILE *fp = fopen("file.bin", "rb");
char ch = fgetc(fp);
printf("%c\n", ch);
fclose(fp);
Example:
fclose(fp);
Program:
#include <stdio.h>
int main() {
return 0;
Program:
#include <stdio.h>
int main() {
char str[100];
printf("%s", str);
fclose(fp);
return 0;
}
17. What is feof()? Provide example.
Checks if end of file has been reached.
Example:
while (!feof(fp)) {
char ch = fgetc(fp);
printf("%c", ch);
fclose(fp);
Example:
if (ferror(fp)) {
printf("File error occurred\n");
fclose(fp);
Example:
if (fp != NULL) {
20. Explain difference between text and binary files with example.
Example:
Text files store readable characters:
Example:
auto int a = 5;
Program:
#include <stdio.h>
void demo() {
auto int a = 5;
b++;
int main() {
demo();
demo();
return 0;
Example:
void func() {
Program:
#include <stdio.h>
int x = 10;
void show() {
int x = 20;
int main() {
show();
printf("Global x = %d\n", x);
return 0;
Example:
int globalVar = 5;
void test() {
Program:
#include <stdio.h>
int g = 100;
void display() {
int g = 200;
int main() {
display();
Example:
void count() {
static int x = 0;
x++;
}
Program:
#include <stdio.h>
void counter() {
static int c = 0;
c++;
int main() {
counter();
counter();
counter();
return 0;
Example:
extern int x;
Program:
File1.c
int x = 50;
File2.c
#include <stdio.h>
extern int x;
int main() {
printf("Extern variable: %d\n", x);
return 0;
Example:
register int a = 5;
Program:
#include <stdio.h>
int main() {
register int i;
return 0;
Example:
Program:
#include <stdio.h>
int main() {
8. Write a program for reading and writing using fgetc() and fputc().
Example:
char ch = fgetc(fp);
fputc(ch, stdout);
Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("sample.txt", "w");
fputc('A', fp);
fclose(fp);
fp = fopen("sample.txt", "r");
char ch = fgetc(fp);
fclose(fp);
return 0;
Example:
fputs(buffer, stdout);
Program:
#include <stdio.h>
int main() {
char str[100];
fp = fopen("test.txt", "r");
fclose(fp);
return 0;
}
Example:
Program:
#include <stdio.h>
int main() {
fprintf(fp, "OpenAI");
fclose(fp);
char name[20];
fp = fopen("data.txt", "r");
return 0;
Text files store data as readable characters, while binary files store data in machine-readable
format.
Example:
Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("binary.dat", "wb");
fclose(fp);
fp = fopen("binary.dat", "rb");
fclose(fp);
return 0;
Sequential access reads or writes data in a linear order, from beginning to end.
Example:
fopen("data.txt", "r");
Program:
#include <stdio.h>
int main() {
fclose(fp);
char line[50];
fp = fopen("data.txt", "r");
while (fgets(line, sizeof(line), fp)) {
printf("%s", line);
fclose(fp);
return 0;
13. Explain random access file handling using fseek() and ftell().
Random access lets you move to any part of the file using fseek() and check position using
ftell().
Example:
fseek(fp, 0, SEEK_SET);
Program:
#include <stdio.h>
int main() {
fputc('Z', fp);
rewind(fp);
char ch;
putchar(ch);
fclose(fp);
return 0;
Example:
if (ferror(fp)) {...}
Program:
#include <stdio.h>
int main() {
if (fp == NULL) {
perror("Error");
return 1;
char ch;
while (!feof(fp)) {
ch = fgetc(fp);
putchar(ch);
if (ferror(fp)) {
printf("File read error\n");
fclose(fp);
return 0;
Program:
#include <stdio.h>
int main() {
char ch;
fputc(ch, dest);
fclose(src);
fclose(dest);
return 0;
16. Describe how to count the number of characters, words, and lines in a text file.
Use loops to read characters and check for spaces, newlines, and EOF.
Example:
Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("test.txt", "r");
chars++;
fclose(fp);
return 0;
Program:
#include <stdio.h>
int main() {
fclose(fp);
return 0;
Example:
remove("filename.txt");
Program:
#include <stdio.h>
int main() {
if (remove("delete_me.txt") == 0)
printf("File deleted.\n");
else
return 0;
19. Explain the rewind() function and its use in file handling.
Program:
#include <stdio.h>
int main() {
char ch;
rewind(fp);
printf("\nRewind called.\n");
fclose(fp);
return 0;
20. Write a program that stores student records and displays them using files.
Example:
fprintf() and fscanf() for storing structured data.
Program:
#include <stdio.h>
struct Student {
int id;
char name[30];
};
int main() {
FILE *fp = fopen("students.txt", "w");
fclose(fp);
fp = fopen("students.txt", "r");
fclose(fp);
return 0;
Example:
static int c = 0;
extern int d;
Program:
#include <stdio.h>
void staticTest() {
static int count = 0;
count++;
int main() {
auto int a = 1;
register int b = 2;
staticTest();
}
return 0;
Example:
Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("example.txt", "w");
fprintf(fp, "This is a file example.\n");
fclose(fp);
char line[50];
fp = fopen("example.txt", "r");
printf("%s", line);
fclose(fp);
return 0;
Example:
fgetc(fp); // Sequential
Program:
#include <stdio.h>
int main() {
fputs("ABCDEFG", fp);
rewind(fp);
printf("Sequential: ");
fseek(fp, 4, SEEK_SET);
printf("\nRandom: ");
putchar(fgetc(fp));
fclose(fp);
return 0;
4. Explain file modes and demonstrate how each mode works with a file.
File modes:
• "r": read
• "w+": write/read
• "a+": read/append
Example:
fopen("data.txt", "a+");
Program:
#include <stdio.h>
int main() {
rewind(fp);
char ch;
return 0;
}
5. Describe the use of file handling functions in C with example.
• fopen(), fclose()
• fgetc(), fputc()
• fgets(), fputs()
• fprintf(), fscanf()
Example:
Program:
#include <stdio.h>
int main() {
int id = 101;
fclose(fp);
fp = fopen("record.txt", "r");
fclose(fp);
return 0;
6. Write a program to read and write student records using structures and files.
Example:
fprintf(fp, "%d %s", s.id, s.name);
Program:
#include <stdio.h>
struct Student {
int id;
char name[30];
};
int main() {
fp = fopen("stud.txt", "r");
fclose(fp);
return 0;
}
Example:
if (ferror(fp)) perror("Error:");
Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("error.txt", "r");
if (fp == NULL) {
perror("File error");
return 1;
char ch;
while (!feof(fp)) {
ch = fgetc(fp);
if (ferror(fp)) {
printf("Error reading\n");
break;
}
putchar(ch);
fclose(fp);
return 0;
8. Write a C program to count vowels, consonants, digits and spaces in a text file.
Example:
if (isalpha(ch))...
Program:
#include <stdio.h>
#include <ctype.h>
int main() {
if (isalpha(ch)) {
ch = tolower(ch);
vowels++;
else
consonants++;
} else if (isdigit(ch))
digits++;
else if (ch == ' ')
spaces++;
fclose(fp);
return 0;
}
Example:
strstr(line, "word");
Program:
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp = fopen("doc.txt", "r");
char line[100];
int found = 0;
if (strstr(line, "searchword")) {
found = 1;
break;
fclose(fp);
if (found)
printf("Word found.\n");
else
return 0;
Example:
Program:
#include <stdio.h>
#include <stdlib.h>
int main() {
int choice;
char ch;
FILE *fp;
do {
printf("\n1.Write\n2.Read\n3.Exit\nChoice: ");
scanf("%d", &choice);
getchar();
switch (choice) {
case 1:
fp = fopen("menu.txt", "w");
fclose(fp);
break;
case 2:
fp = fopen("menu.txt", "r");
while ((ch = fgetc(fp)) != EOF) {
putchar(ch);
fclose(fp);
break;
case 3:
exit(0);
}
} while (1);
return 0;
11. Develop a C program to manage employee records using files. Include operations
like add, display, and search.
Example:
fprintf(fp, "%d %s %f", id, name, salary);
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int id;
char name[30];
float salary;
};
void addEmployee() {
fclose(fp);
void displayAll() {
FILE *fp = fopen("emp.dat", "r");
struct Employee e;
fclose(fp);
void searchEmployee() {
struct Employee e;
int target, found = 0;
printf("Enter ID to search: ");
scanf("%d", &target);
if (e.id == target) {
found = 1;
break;
}
fclose(fp);
int main() {
int choice;
while (1) {
printf("1.Add 2.Display 3.Search 4.Exit\nChoice: ");
scanf("%d", &choice);
if (choice == 1) addEmployee();
return 0;
12. Explain file pointer manipulation using ftell(), fseek(), and rewind() with example.
Example:
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
Program:
#include <stdio.h>
int main() {
fseek(fp, 0, SEEK_END);
rewind(fp);
char ch;
return 0;
Example:
fgetc(src);
fputc(ch, dest);
Program:
#include <stdio.h>
int main() {
FILE *src = fopen("source.txt", "r");
char ch;
fputc(ch, dest);
fclose(src);
fclose(dest);
return 0;
Example:
strcmp(user, entered);
Program:
#include <stdio.h>
#include <string.h>
int main() {
fp = fopen("users.txt", "r");
printf("Login now:\n");
int found = 0;
found = 1;
break;
if (found)
printf("Login successful.\n");
else
printf("Login failed.\n");
fclose(fp);
return 0;
15. Differentiate between text and binary file processing with a C example.
Example (Binary):
int main() {
fclose(fp);
fp = fopen("binfile.dat", "rb");
int readNum;
fclose(fp);
return 0;
16. Explain how to use fscanf() and fprintf() in structured data storage.
Example:
fprintf(fp, "%d %s", id, name);
Program:
#include <stdio.h>
int main() {
int id = 10;
fclose(fp);
int rid;
char rname[20];
fp = fopen("data.txt", "r");
fclose(fp);
return 0;
}
17. Implement a C program to sort student records using file and structure.
Example:
Program:
#include <stdio.h>
#include <stdlib.h>
struct Student {
int id;
char name[30];
};
int main() {
}
fclose(fp);
temp = s[i];
s[i] = s[j];
s[j] = temp;
printf("Sorted records:\n");
return 0;
}
18. Describe how storage class affects recursion and static counters.
Example:
static int count = 0;
Program:
#include <stdio.h>
void countFunc() {
count++;
printf("Static count: %d\n", count);
}
int main() {
countFunc();
return 0;
19. Write a program that uses ftell() and fseek() to find the size of a file and read from a
specific byte.
Example:
fseek(fp, 5, SEEK_SET);
Program:
#include <stdio.h>
int main() {
FILE *fp = fopen("size.txt", "w+");
fputs("1234567890", fp);
fseek(fp, 0, SEEK_END);
fseek(fp, 5, SEEK_SET);
char ch = fgetc(fp);
fclose(fp);
return 0;
#include <stdio.h>
extern int x;
void printX();
int main() {
printX();
return 0;
}
File 2 – lib.c:
#include <stdio.h>
int x = 10;
void printX() {
}
To compile: