0% found this document useful (0 votes)
8 views41 pages

Programming in C Unit V

The document provides an overview of storage classes and file handling in C programming, detailing the four storage classes: auto, static, extern, and register, along with their characteristics and examples. It also covers various file operations such as opening, reading, writing, and closing files, as well as functions like fopen(), fclose(), feof(), and ferror(). Additionally, it explains the differences between text and binary files, sequential and random access, and includes example programs for practical understanding.

Uploaded by

boomikag2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views41 pages

Programming in C Unit V

The document provides an overview of storage classes and file handling in C programming, detailing the four storage classes: auto, static, extern, and register, along with their characteristics and examples. It also covers various file operations such as opening, reading, writing, and closing files, as well as functions like fopen(), fclose(), feof(), and ferror(). Additionally, it explains the differences between text and binary files, sequential and random access, and includes example programs for practical understanding.

Uploaded by

boomikag2006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Programming in C

Unit 5 — Storage Classes and Files


Two-Mark based Questions (Note : Just for Reference, not a question-paper answers)

1. What are storage classes in C?


Storage classes define variable lifetime, scope, and visibility: auto, static, extern, register.

Example:

auto int a = 10;

static int b = 20;

extern int c;

register int d;

2. Define the auto storage class with example.


Default for local variables; exists only during function execution.

Example:

void func() {

auto int a = 5;

printf("%d\n", a);

3. What is the static storage class? Explain with example.


Retains value between function calls, local scope.

Example:
void counter() {

static int count = 0;

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);

5. What is the register storage class? Give example.


Suggests storing variable in CPU register for faster access.

Example:
void func() {

register int i = 0;

for (; i < 5; i++) {

printf("%d ", i);

6. What is variable scope? Give example.


Scope is where a variable is accessible.
Example:

int a = 10; // global scope

void func() {

int b = 5; // local scope

7. What is the difference between global and local variable?


Global declared outside any function, accessible everywhere; local declared inside function,
accessible only there.
Example:
int x = 20; // global

void f() {

int y = 10; // local

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);

9. Define visibility in C programming. Give example.


Visibility means where a variable or function name is accessible in the program.
Example:

static int a = 10; // visible only within this file

extern int b; // visible across files

10. What is a file in C programming? Explain with a program opening a file.


A file is a collection of data stored on disk, accessed via file pointers.

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("example.txt", "r");


if (fp == NULL) {

printf("File not found.\n");

} else {
printf("File opened successfully.\n");
fclose(fp);

return 0;

11. What is the purpose of fopen()? Provide example.


fopen() opens a file with specified mode.
Example:

FILE *fp = fopen("data.txt", "r");

if (fp == NULL) {

printf("Error opening file\n");

12. What is sequential file access? Explain with example.


Data is read or written in order from start to end.

Example:
FILE *fp = fopen("file.txt", "r");

char ch;

while ((ch = fgetc(fp)) != EOF) {

putchar(ch);

fclose(fp);

13. What is random file access? Provide example using fseek().


Data can be accessed at any position directly.

Example:
FILE *fp = fopen("file.bin", "rb");

fseek(fp, 10, SEEK_SET); // Move to byte 10

char ch = fgetc(fp);
printf("%c\n", ch);
fclose(fp);

14. What does fclose() do? Provide example.


Closes an opened file and frees resources.

Example:

FILE *fp = fopen("file.txt", "w");

fclose(fp);

15. How do you write a string to a text file? Provide program.

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("file.txt", "w");

fputs("Hello World", fp);


fclose(fp);

return 0;

16. How to read a string from a file? Provide program.

Program:

#include <stdio.h>
int main() {

char str[100];

FILE *fp = fopen("file.txt", "r");

fgets(str, 100, fp);

printf("%s", str);

fclose(fp);

return 0;
}
17. What is feof()? Provide example.
Checks if end of file has been reached.

Example:

FILE *fp = fopen("file.txt", "r");

while (!feof(fp)) {

char ch = fgetc(fp);
printf("%c", ch);

fclose(fp);

18. What does ferror() do? Provide example.


Detects file operation errors.

Example:

FILE *fp = fopen("file.txt", "r");

if (ferror(fp)) {
printf("File error occurred\n");

fclose(fp);

19. What is a binary file? Provide example opening a binary file.


A binary file stores data in raw bytes, not readable text.

Example:

FILE *fp = fopen("image.bin", "rb");

if (fp != NULL) {

// Process binary data


fclose(fp);

20. Explain difference between text and binary files with example.
Example:
Text files store readable characters:

FILE *fp = fopen("text.txt", "r");

Binary files store raw bytes:

FILE *fp = fopen("data.bin", "rb");

1. Explain the different storage classes in C with examples.


C supports auto, static, extern, and register storage classes. They define variable lifetime,
scope, and linkage.

Example:

auto int a = 5;

static int b = 10;


extern int c;

register int d = 15;

Program:

#include <stdio.h>

int externalVar = 20;

void demo() {

auto int a = 5;

static int b = 10;

register int c = 15;

printf("%d %d %d\n", a, b, c);

b++;

int main() {

demo();
demo();
return 0;

2. Describe the scope and visibility of variables in C.


Scope is where a variable can be accessed. Visibility is where a variable is known to the
compiler.

Example:

int globalVar = 100;

void func() {

int localVar = 50;

Program:

#include <stdio.h>

int x = 10;

void show() {

int x = 20;

printf("Local x = %d\n", x);

int main() {

show();
printf("Global x = %d\n", x);

return 0;

3. Differentiate between local and global variables. Explain with example.


Local variables are declared inside functions. Global variables are declared outside and
accessible across functions.

Example:

int globalVar = 5;

void test() {

int localVar = 10;


}

Program:

#include <stdio.h>

int g = 100;

void display() {
int g = 200;

printf("Local g: %d\n", g);

int main() {

display();

printf("Global g: %d\n", g);


return 0;

4. Explain the working of a static variable in a function.


A static variable retains its value across function calls.

Example:

void count() {

static int x = 0;
x++;
}

Program:

#include <stdio.h>

void counter() {

static int c = 0;

c++;

printf("Counter: %d\n", c);

int main() {
counter();
counter();

counter();

return 0;

5. Write a program to demonstrate the use of extern variables.


extern is used to refer to a global variable defined in another file.

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;

6. Describe the use of register storage class with example.


Used for fast access by storing variable in CPU register.

Example:
register int a = 5;

Program:

#include <stdio.h>

int main() {

register int i;

for (i = 0; i < 5; i++) {


printf("%d\n", i);

return 0;

7. Explain file handling in C with basic file operations.


File handling includes opening, reading/writing, and closing a file.

Example:

FILE *fp = fopen("file.txt", "r");

Program:
#include <stdio.h>

int main() {

FILE *fp = fopen("file.txt", "w");

fprintf(fp, "Welcome to file handling");


fclose(fp);
return 0;

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);

printf("Character: %c\n", ch);

fclose(fp);
return 0;

9. Explain fgets() and fputs() functions with example.

Example:

fgets(buffer, size, fp);

fputs(buffer, stdout);
Program:

#include <stdio.h>
int main() {

char str[100];

FILE *fp = fopen("test.txt", "w");

fputs("Hello File", fp);


fclose(fp);

fp = fopen("test.txt", "r");

fgets(str, 100, fp);

printf("String: %s", str);

fclose(fp);

return 0;
}

10. Describe the purpose of fprintf() and fscanf() in file handling.

Example:

fprintf(fp, "Name: %s", name);

fscanf(fp, "%s", name);

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("data.txt", "w");

fprintf(fp, "OpenAI");

fclose(fp);

char name[20];
fp = fopen("data.txt", "r");

fscanf(fp, "%s", name);


printf("Read: %s\n", name);
fclose(fp);

return 0;

11. Explain the difference between text and binary files in C.

Text files store data as readable characters, while binary files store data in machine-readable
format.
Example:

fopen("file.txt", "r"); // text

fopen("file.dat", "rb"); // binary

Program:

#include <stdio.h>

int main() {
FILE *fp = fopen("binary.dat", "wb");

int num = 12345;

fwrite(&num, sizeof(num), 1, fp);

fclose(fp);

fp = fopen("binary.dat", "rb");

fread(&num, sizeof(num), 1, fp);


printf("Binary value: %d\n", num);

fclose(fp);

return 0;

12. Describe sequential access file handling with a program.

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() {

FILE *fp = fopen("data.txt", "w");

fprintf(fp, "Line 1\nLine 2\n");

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() {

FILE *fp = fopen("random.txt", "w+");


fputs("ABCDEFG", fp);
fseek(fp, 3, SEEK_SET);

fputc('Z', fp);

rewind(fp);
char ch;

while ((ch = fgetc(fp)) != EOF)

putchar(ch);

fclose(fp);

return 0;

14. Write a program to demonstrate the use of feof() and ferror().

Example:

while (!feof(fp)) {...}

if (ferror(fp)) {...}

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("sample.txt", "r");

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;

15. Explain how to copy the contents of one file to another.


Open one file in read mode and the other in write mode. Read from the first and write to the
second.
Example:

fgetc() and fputc() for character copying.

Program:

#include <stdio.h>

int main() {

FILE *src = fopen("source.txt", "r");


FILE *dest = fopen("copy.txt", "w");

char ch;

while ((ch = fgetc(src)) != EOF) {

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:

Check ch == ' ', '\n', and EOF.

Program:

#include <stdio.h>

int main() {
FILE *fp = fopen("test.txt", "r");

int ch, words = 0, lines = 0, chars = 0;

while ((ch = fgetc(fp)) != EOF) {

chars++;

if (ch == ' ' || ch == '\n') words++;

if (ch == '\n') lines++;


}

fclose(fp);

printf("Chars: %d, Words: %d, Lines: %d\n", chars, words, lines);

return 0;

17. Write a program to append data to a file.

Use "a" mode in fopen() to add data at the end of a file.


Example:
fopen("file.txt", "a");

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("append.txt", "a");

fprintf(fp, "New line appended.\n");

fclose(fp);

return 0;

18. Explain how to delete a file in C.


Use the remove() function to delete a file.

Example:

remove("filename.txt");

Program:

#include <stdio.h>

int main() {
if (remove("delete_me.txt") == 0)

printf("File deleted.\n");

else

perror("Error deleting file");

return 0;

19. Explain the rewind() function and its use in file handling.

rewind() resets the file pointer to the beginning of the file.


Example:
rewind(fp);

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("rewind.txt", "r+");

char ch;

while ((ch = fgetc(fp)) != EOF) putchar(ch);

rewind(fp);

printf("\nRewind called.\n");

while ((ch = fgetc(fp)) != EOF) putchar(ch);

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");

struct Student s = {1, "John"};

fprintf(fp, "%d %s\n", s.id, s.name);

fclose(fp);

fp = fopen("students.txt", "r");

fscanf(fp, "%d %s", &s.id, s.name);

printf("Student: %d %s\n", s.id, s.name);

fclose(fp);

return 0;

1. Explain different storage classes in C with examples.

C has four storage classes: auto, register, static, and extern.


They control the lifetime, visibility, and memory location of variables.

• auto: Default for local variables. Exists during function execution.

• register: Suggests storage in CPU register. No & allowed.

• static: Persists the value between function calls.

• extern: Refers to a global variable defined elsewhere.

Example:

auto int a = 10;


register int b = 5;

static int c = 0;

extern int d;

Program:

#include <stdio.h>

void staticTest() {
static int count = 0;
count++;

printf("Count: %d\n", count);

int main() {

auto int a = 1;

register int b = 2;

printf("Auto: %d, Register: %d\n", a, b);

for (int i = 0; i < 3; i++) {

staticTest();
}

return 0;

2. Describe file handling in C and the steps involved in using files.

C provides functions to handle files for data storage:

1. Declare a file pointer: FILE *fp;


2. Open file using fopen()

3. Read/write using fgetc, fputc, fgets, fputs, fprintf, fscanf, etc.

4. Close using fclose()

Example:

FILE *fp = fopen("file.txt", "w");

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");

fgets(line, 50, fp);

printf("%s", line);

fclose(fp);

return 0;

3. Explain sequential and random access file handling with examples.


• Sequential access reads/writes data from start to end linearly.

• Random access uses fseek() to move to a specific location.

Example:

fseek(fp, position, SEEK_SET); // Random

fgetc(fp); // Sequential

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("data.txt", "w+");

fputs("ABCDEFG", fp);

rewind(fp);

printf("Sequential: ");

for (int i = 0; i < 3; i++)


putchar(fgetc(fp));

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 (clears file)


• "a": append
• "r+": read/write

• "w+": write/read

• "a+": read/append

Example:

fopen("data.txt", "a+");

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("log.txt", "a+");

fprintf(fp, "Logging activity...\n");

rewind(fp);

char ch;

while ((ch = fgetc(fp)) != EOF) putchar(ch);


fclose(fp);

return 0;
}
5. Describe the use of file handling functions in C with example.

Important file handling functions:

• fopen(), fclose()
• fgetc(), fputc()

• fgets(), fputs()

• fprintf(), fscanf()

Example:

fprintf(fp, "%d %s", id, name);

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("record.txt", "w");

int id = 101;

char name[] = "David";

fprintf(fp, "%d %s", id, name);

fclose(fp);

fp = fopen("record.txt", "r");

fscanf(fp, "%d %s", &id, name);

printf("ID: %d, Name: %s\n", id, name);

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() {

struct Student s1 = {1, "Anil"}, s2;

FILE *fp = fopen("stud.txt", "w");


fprintf(fp, "%d %s", s1.id, s1.name);
fclose(fp);

fp = fopen("stud.txt", "r");

fscanf(fp, "%d %s", &s2.id, s2.name);

printf("Student: %d %s\n", s2.id, s2.name);

fclose(fp);

return 0;
}

7. Demonstrate error handling using ferror(), perror(), and feof() in C.

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() {

FILE *fp = fopen("text.txt", "r");


int vowels = 0, consonants = 0, digits = 0, spaces = 0;
char ch;

while ((ch = fgetc(fp)) != EOF) {

if (isalpha(ch)) {
ch = tolower(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

vowels++;

else

consonants++;

} else if (isdigit(ch))

digits++;
else if (ch == ' ')
spaces++;

fclose(fp);

printf("Vowels: %d\nConsonants: %d\nDigits: %d\nSpaces: %d\n",

vowels, consonants, digits, spaces);

return 0;
}

9. Write a C program to search a word in a file.

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;

while (fgets(line, sizeof(line), fp)) {

if (strstr(line, "searchword")) {

found = 1;

break;

fclose(fp);
if (found)

printf("Word found.\n");

else

printf("Word not found.\n");

return 0;

10. Create a menu-driven file handling system in C.

Example:

switch (choice) { case 1: ... }

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");

printf("Enter text (end with #): ");


while ((ch = getchar()) != '#') {
fputc(ch, fp);

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() {

FILE *fp = fopen("emp.dat", "a");


struct Employee e;

printf("Enter ID, Name, Salary: ");

scanf("%d %s %f", &e.id, e.name, &e.salary);

fprintf(fp, "%d %s %.2f\n", e.id, e.name, e.salary);

fclose(fp);

void displayAll() {
FILE *fp = fopen("emp.dat", "r");

struct Employee e;

while (fscanf(fp, "%d %s %f", &e.id, e.name, &e.salary) != EOF) {

printf("ID: %d, Name: %s, Salary: %.2f\n", e.id, e.name, e.salary);


}

fclose(fp);

void searchEmployee() {

FILE *fp = fopen("emp.dat", "r");

struct Employee e;
int target, found = 0;
printf("Enter ID to search: ");

scanf("%d", &target);

while (fscanf(fp, "%d %s %f", &e.id, e.name, &e.salary) != EOF) {

if (e.id == target) {

printf("Found: %d %s %.2f\n", e.id, e.name, e.salary);

found = 1;

break;
}

if (!found) printf("Not found.\n");

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();

else if (choice == 2) displayAll();

else if (choice == 3) searchEmployee();


else break;

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() {

FILE *fp = fopen("sample.txt", "w+");


fputs("Hello file pointer!", fp);

fseek(fp, 0, SEEK_END);

long size = ftell(fp);

printf("File size: %ld bytes\n", size);

rewind(fp);
char ch;

while ((ch = fgetc(fp)) != EOF) putchar(ch);


fclose(fp);

return 0;

13. Write a C program to copy the contents of one file to another.

Example:

fgetc(src);

fputc(ch, dest);

Program:

#include <stdio.h>

int main() {
FILE *src = fopen("source.txt", "r");

FILE *dest = fopen("copy.txt", "w");

char ch;

while ((ch = fgetc(src)) != EOF) {

fputc(ch, dest);

fclose(src);

fclose(dest);

printf("File copied successfully.\n");

return 0;

14. Develop a C program to maintain a text-based login system using files.

Example:
strcmp(user, entered);
Program:

#include <stdio.h>

#include <string.h>

int main() {

FILE *fp = fopen("users.txt", "a+");

char user[20], pass[20], u[20], p[20];

printf("Register username and password:\n");

scanf("%s %s", user, pass);

fprintf(fp, "%s %s\n", user, pass);


fclose(fp);

fp = fopen("users.txt", "r");

printf("Login now:\n");

scanf("%s %s", user, pass);

int found = 0;

while (fscanf(fp, "%s %s", u, p) != EOF) {


if (strcmp(u, user) == 0 && strcmp(p, pass) == 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.

• Text file: Human-readable, stores data in ASCII.

• Binary file: Stores data in raw binary form.

Example (Binary):

fwrite(&num, sizeof(num), 1, fp);

Program (Binary File):


#include <stdio.h>

int main() {

FILE *fp = fopen("binfile.dat", "wb");

int num = 12345;

fwrite(&num, sizeof(num), 1, fp);

fclose(fp);

fp = fopen("binfile.dat", "rb");

int readNum;

fread(&readNum, sizeof(readNum), 1, fp);

printf("Read: %d\n", readNum);

fclose(fp);

return 0;

16. Explain how to use fscanf() and fprintf() in structured data storage.
Example:
fprintf(fp, "%d %s", id, name);

fscanf(fp, "%d %s", &id, name);

Program:

#include <stdio.h>

int main() {

FILE *fp = fopen("data.txt", "w");

int id = 10;

char name[] = "Kumar";

fprintf(fp, "%d %s", id, name);

fclose(fp);

int rid;

char rname[20];

fp = fopen("data.txt", "r");

fscanf(fp, "%d %s", &rid, rname);

printf("ID: %d, Name: %s\n", rid, rname);

fclose(fp);

return 0;
}

17. Implement a C program to sort student records using file and structure.

Example:

Use array + structure + qsort or bubble sort

Program:

#include <stdio.h>
#include <stdlib.h>

struct Student {
int id;

char name[30];

};

int main() {

struct Student s[3] = {{2, "Kavi"}, {1, "Anu"}, {3, "Ravi"}};

FILE *fp = fopen("studsort.txt", "w");

for (int i = 0; i < 3; i++) {

fprintf(fp, "%d %s\n", s[i].id, s[i].name);

}
fclose(fp);

struct Student temp;

for (int i = 0; i < 2; i++) {

for (int j = i+1; j < 3; j++) {

if (s[i].id > s[j].id) {

temp = s[i];

s[i] = s[j];
s[j] = temp;

printf("Sorted records:\n");

for (int i = 0; i < 3; i++)


printf("%d %s\n", s[i].id, s[i].name);

return 0;
}

18. Describe how storage class affects recursion and static counters.

Example:
static int count = 0;

Program:

#include <stdio.h>

void countFunc() {

static int count = 0;

count++;
printf("Static count: %d\n", count);
}

int main() {

for (int i = 0; i < 5; i++)

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);

printf("Size: %ld\n", ftell(fp));

fseek(fp, 5, SEEK_SET);

char ch = fgetc(fp);

printf("6th char: %c\n", ch);

fclose(fp);

return 0;

20. Demonstrate the concept of extern with multiple files in C.


File 1 – main.c:

#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() {

printf("Extern value: %d\n", x);

}
To compile:

gcc main.c lib.c -o program


./program

You might also like