Advanced C Programming introduces students to programming concepts that are used to create more powerful, organized, and efficient C programs. After learning variables, operators, conditions, and loops, students can now work with arrays, strings, functions, pointers, structures, unions, enumerations, and file handling.
These concepts help programmers develop programs that can manage larger amounts of data and divide complex tasks into smaller, reusable components.
An array is a collection of elements of the same data type stored under one variable name.
Instead of creating separate variables:
int mark1, mark2, mark3, mark4, mark5;
we can use:
int marks[5];
The elements of an array are accessed using an index.
C array indexing starts from
0.
For an array:
int marks[5];
the indexes are:
0 1 2 3 4
A one-dimensional array stores data in a single sequence.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 90, 85};
printf("First mark = %dn", marks[0]);
printf("Second mark = %dn", marks[1]);
return 0;
}
Loops are commonly used to process array elements.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 90, 85};
int i;
for (i = 0; i < 5; i++)
{
printf("%dn", marks[i]);
}
return 0;
}
75
82
68
90
85
#include <stdio.h>
int main()
{
int marks[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter marks %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("nStudent Marks:n");
for (i = 0; i < 5; i++)
{
printf("%dn", marks[i]);
}
return 0;
}
A two-dimensional array stores data in rows and columns.
It is useful for:
int marks[2][3] =
{
{70, 80, 90},
{65, 75, 85}
};
This represents:
| Student | Subject 1 | Subject 2 | Subject 3 |
|---|---|---|---|
| Student 1 | 70 | 80 | 90 |
| Student 2 | 65 | 75 | 85 |
A string is a sequence of characters.
C does not have a separate built-in string data type. Strings are stored using character arrays.
char name[20] = "Rahul";
A C string ends with a special null character:
''
The %s format specifier can be used to read a simple word.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter your name: ");
scanf("%29s", name);
printf("Hello %s", name);
return 0;
}
scanf("%s", ...) stops reading at whitespace, so it is not suitable for a full name containing spaces.
The <string.h> header provides many useful string functions.
| Function | Purpose |
|---|---|
strlen() |
Finds string length |
strcpy() |
Copies a string |
strcat() |
Concatenates strings |
strcmp() |
Compares strings |
#include <stdio.h>
#include <string.h>
int main()
{
char name[] = "Computer";
printf("Length = %zu", strlen(name));
return 0;
}
A function is a block of code designed to perform a specific task.
Functions help make programs:
#include <stdio.h>
void welcome()
{
printf("Welcome to C Programming");
}
int main()
{
welcome();
return 0;
}
A function generally involves three important parts.
int add(int, int);
int add(int a, int b)
{
return a + b;
}
result = add(10, 20);
#include <stdio.h>
void display(int number)
{
printf("Number = %d", number);
}
int main()
{
display(50);
return 0;
}
Here, number is a parameter of the function.
A function can return a value to the calling code.
#include <stdio.h>
int square(int number)
{
return number * number;
}
int main()
{
int result;
result = square(5);
printf("Square = %d", result);
return 0;
}
Square = 25
Recursion occurs when a function calls itself.
A recursive function should have a condition that stops further calls.
#include <stdio.h>
int factorial(int n)
{
if (n <= 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
printf("Factorial = %d", factorial(5));
return 0;
}
Factorial = 120
A pointer is a variable that stores the memory address of another variable.
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
printf("Value = %dn", number);
printf("Address = %pn", (void *)ptr);
return 0;
}
| Operator | Meaning |
|---|---|
& |
Address-of operator |
* |
Dereference operator |
Dereferencing means accessing the value stored at the address held by a pointer.
#include <stdio.h>
int main()
{
int number = 25;
int *ptr = &number;
printf("Value = %d", *ptr);
return 0;
}
Here, *ptr accesses the value of number.
Pointers can be used to allow a function to modify variables in the calling function.
#include <stdio.h>
void changeValue(int *x)
{
*x = 100;
}
int main()
{
int number = 20;
changeValue(&number);
printf("Number = %d", number);
return 0;
}
Number = 100
A structure allows different types of data to be grouped together under one name.
For example, student information may contain:
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student student = {101, "Rahul", 85.5};
printf("Roll No: %dn", student.rollNo);
printf("Name: %sn", student.name);
printf("Marks: %.2fn", student.marks);
return 0;
}
The dot (.) operator is used to access members of a structure.
student.rollNo
student.name
student.marks
printf("%d", student.rollNo);
An array of structures can store information about multiple records.
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student students[2] =
{
{101, "Rahul", 85.5},
{102, "Amit", 78.0}
};
int i;
for (i = 0; i < 2; i++)
{
printf("%d %s %.2fn",
students[i].rollNo,
students[i].name,
students[i].marks);
}
return 0;
}
This concept is useful for developing student management and record-management programs.
A union is similar to a structure, but its members share the same memory location.
union Data
{
int number;
float value;
char letter;
};
Unlike a structure, a union generally stores only one member’s value meaningfully at a time.
| Structure | Union |
|---|---|
| Each member has separate storage | Members share storage |
| Multiple members can hold values at the same time | Only one member should generally be treated as active at a time |
| Usually requires more memory | Can save memory |
An enumeration (enum) is a user-defined type containing named integer constants.
#include <stdio.h>
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY
};
int main()
{
enum Day today = WEDNESDAY;
printf("Day number = %d", today);
return 0;
}
By default, the first enumerator usually has the value 0, the next 1, and so on unless values are explicitly assigned.
File handling allows a C program to store and retrieve information from files.
Files can be used to store:
The <stdio.h> header provides functions for file operations.
The fopen() function is used to open a file.
FILE *fp;
fp = fopen("filename.txt", "mode");
| Mode | Purpose |
|---|---|
"r" |
Read |
"w" |
Write |
"a" |
Append |
"r+" |
Read and write |
"w+" |
Write and read |
"a+" |
Append and read |
Opening a file in
"w"mode can replace existing contents, so it should be used carefully.
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.");
return 1;
}
fprintf(fp, "ADCA Student Recordn");
fprintf(fp, "Roll No: 101n");
fprintf(fp, "Marks: 85n");
fclose(fp);
return 0;
}
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("Unable to open file.");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
return 0;
}
| Function | Purpose |
|---|---|
fopen() |
Opens a file |
fclose() |
Closes a file |
fprintf() |
Writes formatted data |
fscanf() |
Reads formatted data |
fgetc() |
Reads one character |
fputc() |
Writes one character |
fgets() |
Reads a line/string |
fputs() |
Writes a string |
Always check whether fopen() successfully opened the file before performing file operations.
Dynamic memory allocation allows programs to request memory during runtime.
Common functions are provided by <stdlib.h>:
malloc()calloc()realloc()free()#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = malloc(5 * sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed.");
return 1;
}
ptr[0] = 10;
ptr[1] = 20;
ptr[2] = 30;
printf("%d", ptr[1]);
free(ptr);
return 0;
}
Memory obtained dynamically with malloc(), calloc(), or realloc() should be released using free() when it is no longer needed.
Preprocessor directives begin with #.
#include <stdio.h>
#define PI 3.14159
#includeUsed to include header files.
#defineUsed to define macros.
Example:
#define MAX 100
Header files contain declarations and information needed by programs.
| Header | Common Purpose |
|---|---|
stdio.h |
Input/output |
stdlib.h |
General utilities and memory allocation |
string.h |
String operations |
math.h |
Mathematical functions |
ctype.h |
Character testing/conversion |
#include <stdio.h>
int main()
{
int numbers[5];
int i, largest;
printf("Enter 5 numbers:n");
for (i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
largest = numbers[0];
for (i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest number = %d", largest);
return 0;
}
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student s;
printf("Enter Roll Number: ");
scanf("%d", &s.rollNo);
printf("Enter Name: ");
scanf("%29s", s.name);
printf("Enter Marks: ");
scanf("%f", &s.marks);
printf("n--- Student Record ---n");
printf("Roll No: %dn", s.rollNo);
printf("Name: %sn", s.name);
printf("Marks: %.2fn", s.marks);
return 0;
}
Advanced C Programming introduces students to programming concepts that are used to create more powerful, organized, and efficient C programs. After learning variables, operators, conditions, and loops, students can now work with arrays, strings, functions, pointers, structures, unions, enumerations, and file handling.
These concepts help programmers develop programs that can manage larger amounts of data and divide complex tasks into smaller, reusable components.
An array is a collection of elements of the same data type stored under one variable name.
Instead of creating separate variables:
int mark1, mark2, mark3, mark4, mark5;
we can use:
int marks[5];
The elements of an array are accessed using an index.
C array indexing starts from
0.
For an array:
int marks[5];
the indexes are:
0 1 2 3 4
A one-dimensional array stores data in a single sequence.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 90, 85};
printf("First mark = %dn", marks[0]);
printf("Second mark = %dn", marks[1]);
return 0;
}
Loops are commonly used to process array elements.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 90, 85};
int i;
for (i = 0; i < 5; i++)
{
printf("%dn", marks[i]);
}
return 0;
}
75
82
68
90
85
#include <stdio.h>
int main()
{
int marks[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter marks %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("nStudent Marks:n");
for (i = 0; i < 5; i++)
{
printf("%dn", marks[i]);
}
return 0;
}
A two-dimensional array stores data in rows and columns.
It is useful for:
int marks[2][3] =
{
{70, 80, 90},
{65, 75, 85}
};
This represents:
| Student | Subject 1 | Subject 2 | Subject 3 |
|---|---|---|---|
| Student 1 | 70 | 80 | 90 |
| Student 2 | 65 | 75 | 85 |
A string is a sequence of characters.
C does not have a separate built-in string data type. Strings are stored using character arrays.
char name[20] = "Rahul";
A C string ends with a special null character:
''
The %s format specifier can be used to read a simple word.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter your name: ");
scanf("%29s", name);
printf("Hello %s", name);
return 0;
}
scanf("%s", ...) stops reading at whitespace, so it is not suitable for a full name containing spaces.
The <string.h> header provides many useful string functions.
| Function | Purpose |
|---|---|
strlen() |
Finds string length |
strcpy() |
Copies a string |
strcat() |
Concatenates strings |
strcmp() |
Compares strings |
#include <stdio.h>
#include <string.h>
int main()
{
char name[] = "Computer";
printf("Length = %zu", strlen(name));
return 0;
}
A function is a block of code designed to perform a specific task.
Functions help make programs:
#include <stdio.h>
void welcome()
{
printf("Welcome to C Programming");
}
int main()
{
welcome();
return 0;
}
A function generally involves three important parts.
int add(int, int);
int add(int a, int b)
{
return a + b;
}
result = add(10, 20);
#include <stdio.h>
void display(int number)
{
printf("Number = %d", number);
}
int main()
{
display(50);
return 0;
}
Here, number is a parameter of the function.
A function can return a value to the calling code.
#include <stdio.h>
int square(int number)
{
return number * number;
}
int main()
{
int result;
result = square(5);
printf("Square = %d", result);
return 0;
}
Square = 25
Recursion occurs when a function calls itself.
A recursive function should have a condition that stops further calls.
#include <stdio.h>
int factorial(int n)
{
if (n <= 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
printf("Factorial = %d", factorial(5));
return 0;
}
Factorial = 120
A pointer is a variable that stores the memory address of another variable.
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
printf("Value = %dn", number);
printf("Address = %pn", (void *)ptr);
return 0;
}
| Operator | Meaning |
|---|---|
& |
Address-of operator |
* |
Dereference operator |
Dereferencing means accessing the value stored at the address held by a pointer.
#include <stdio.h>
int main()
{
int number = 25;
int *ptr = &number;
printf("Value = %d", *ptr);
return 0;
}
Here, *ptr accesses the value of number.
Pointers can be used to allow a function to modify variables in the calling function.
#include <stdio.h>
void changeValue(int *x)
{
*x = 100;
}
int main()
{
int number = 20;
changeValue(&number);
printf("Number = %d", number);
return 0;
}
Number = 100
A structure allows different types of data to be grouped together under one name.
For example, student information may contain:
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student student = {101, "Rahul", 85.5};
printf("Roll No: %dn", student.rollNo);
printf("Name: %sn", student.name);
printf("Marks: %.2fn", student.marks);
return 0;
}
The dot (.) operator is used to access members of a structure.
student.rollNo
student.name
student.marks
printf("%d", student.rollNo);
An array of structures can store information about multiple records.
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student students[2] =
{
{101, "Rahul", 85.5},
{102, "Amit", 78.0}
};
int i;
for (i = 0; i < 2; i++)
{
printf("%d %s %.2fn",
students[i].rollNo,
students[i].name,
students[i].marks);
}
return 0;
}
This concept is useful for developing student management and record-management programs.
A union is similar to a structure, but its members share the same memory location.
union Data
{
int number;
float value;
char letter;
};
Unlike a structure, a union generally stores only one member’s value meaningfully at a time.
| Structure | Union |
|---|---|
| Each member has separate storage | Members share storage |
| Multiple members can hold values at the same time | Only one member should generally be treated as active at a time |
| Usually requires more memory | Can save memory |
An enumeration (enum) is a user-defined type containing named integer constants.
#include <stdio.h>
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY
};
int main()
{
enum Day today = WEDNESDAY;
printf("Day number = %d", today);
return 0;
}
By default, the first enumerator usually has the value 0, the next 1, and so on unless values are explicitly assigned.
File handling allows a C program to store and retrieve information from files.
Files can be used to store:
The <stdio.h> header provides functions for file operations.
The fopen() function is used to open a file.
FILE *fp;
fp = fopen("filename.txt", "mode");
| Mode | Purpose |
|---|---|
"r" |
Read |
"w" |
Write |
"a" |
Append |
"r+" |
Read and write |
"w+" |
Write and read |
"a+" |
Append and read |
Opening a file in
"w"mode can replace existing contents, so it should be used carefully.
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.");
return 1;
}
fprintf(fp, "ADCA Student Recordn");
fprintf(fp, "Roll No: 101n");
fprintf(fp, "Marks: 85n");
fclose(fp);
return 0;
}
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("Unable to open file.");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
return 0;
}
| Function | Purpose |
|---|---|
fopen() |
Opens a file |
fclose() |
Closes a file |
fprintf() |
Writes formatted data |
fscanf() |
Reads formatted data |
fgetc() |
Reads one character |
fputc() |
Writes one character |
fgets() |
Reads a line/string |
fputs() |
Writes a string |
Always check whether fopen() successfully opened the file before performing file operations.
Dynamic memory allocation allows programs to request memory during runtime.
Common functions are provided by <stdlib.h>:
malloc()calloc()realloc()free()#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = malloc(5 * sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed.");
return 1;
}
ptr[0] = 10;
ptr[1] = 20;
ptr[2] = 30;
printf("%d", ptr[1]);
free(ptr);
return 0;
}
Memory obtained dynamically with malloc(), calloc(), or realloc() should be released using free() when it is no longer needed.
Preprocessor directives begin with #.
#include <stdio.h>
#define PI 3.14159
#includeUsed to include header files.
#defineUsed to define macros.
Example:
#define MAX 100
Header files contain declarations and information needed by programs.
| Header | Common Purpose |
|---|---|
stdio.h |
Input/output |
stdlib.h |
General utilities and memory allocation |
string.h |
String operations |
math.h |
Mathematical functions |
ctype.h |
Character testing/conversion |
#include <stdio.h>
int main()
{
int numbers[5];
int i, largest;
printf("Enter 5 numbers:n");
for (i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
largest = numbers[0];
for (i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest number = %d", largest);
return 0;
}
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student s;
printf("Enter Roll Number: ");
scanf("%d", &s.rollNo);
printf("Enter Name: ");
scanf("%29s", s.name);
printf("Enter Marks: ");
scanf("%f", &s.marks);
printf("n--- Student Record ---n");
printf("Roll No: %dn", s.rollNo);
printf("Name: %sn", s.name);
printf("Marks: %.2fn", s.marks);
return 0;
}
Advanced C Programming introduces students to programming concepts that are used to create more powerful, organized, and efficient C programs. After learning variables, operators, conditions, and loops, students can now work with arrays, strings, functions, pointers, structures, unions, enumerations, and file handling.
These concepts help programmers develop programs that can manage larger amounts of data and divide complex tasks into smaller, reusable components.
An array is a collection of elements of the same data type stored under one variable name.
Instead of creating separate variables:
int mark1, mark2, mark3, mark4, mark5;
we can use:
int marks[5];
The elements of an array are accessed using an index.
C array indexing starts from
0.
For an array:
int marks[5];
the indexes are:
0 1 2 3 4
A one-dimensional array stores data in a single sequence.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 90, 85};
printf("First mark = %dn", marks[0]);
printf("Second mark = %dn", marks[1]);
return 0;
}
Loops are commonly used to process array elements.
#include <stdio.h>
int main()
{
int marks[5] = {75, 82, 68, 90, 85};
int i;
for (i = 0; i < 5; i++)
{
printf("%dn", marks[i]);
}
return 0;
}
75
82
68
90
85
#include <stdio.h>
int main()
{
int marks[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Enter marks %d: ", i + 1);
scanf("%d", &marks[i]);
}
printf("nStudent Marks:n");
for (i = 0; i < 5; i++)
{
printf("%dn", marks[i]);
}
return 0;
}
A two-dimensional array stores data in rows and columns.
It is useful for:
int marks[2][3] =
{
{70, 80, 90},
{65, 75, 85}
};
This represents:
| Student | Subject 1 | Subject 2 | Subject 3 |
|---|---|---|---|
| Student 1 | 70 | 80 | 90 |
| Student 2 | 65 | 75 | 85 |
A string is a sequence of characters.
C does not have a separate built-in string data type. Strings are stored using character arrays.
char name[20] = "Rahul";
A C string ends with a special null character:
''
The %s format specifier can be used to read a simple word.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter your name: ");
scanf("%29s", name);
printf("Hello %s", name);
return 0;
}
scanf("%s", ...) stops reading at whitespace, so it is not suitable for a full name containing spaces.
The <string.h> header provides many useful string functions.
| Function | Purpose |
|---|---|
strlen() |
Finds string length |
strcpy() |
Copies a string |
strcat() |
Concatenates strings |
strcmp() |
Compares strings |
#include <stdio.h>
#include <string.h>
int main()
{
char name[] = "Computer";
printf("Length = %zu", strlen(name));
return 0;
}
A function is a block of code designed to perform a specific task.
Functions help make programs:
#include <stdio.h>
void welcome()
{
printf("Welcome to C Programming");
}
int main()
{
welcome();
return 0;
}
A function generally involves three important parts.
int add(int, int);
int add(int a, int b)
{
return a + b;
}
result = add(10, 20);
#include <stdio.h>
void display(int number)
{
printf("Number = %d", number);
}
int main()
{
display(50);
return 0;
}
Here, number is a parameter of the function.
A function can return a value to the calling code.
#include <stdio.h>
int square(int number)
{
return number * number;
}
int main()
{
int result;
result = square(5);
printf("Square = %d", result);
return 0;
}
Square = 25
Recursion occurs when a function calls itself.
A recursive function should have a condition that stops further calls.
#include <stdio.h>
int factorial(int n)
{
if (n <= 1)
{
return 1;
}
return n * factorial(n - 1);
}
int main()
{
printf("Factorial = %d", factorial(5));
return 0;
}
Factorial = 120
A pointer is a variable that stores the memory address of another variable.
#include <stdio.h>
int main()
{
int number = 10;
int *ptr;
ptr = &number;
printf("Value = %dn", number);
printf("Address = %pn", (void *)ptr);
return 0;
}
| Operator | Meaning |
|---|---|
& |
Address-of operator |
* |
Dereference operator |
Dereferencing means accessing the value stored at the address held by a pointer.
#include <stdio.h>
int main()
{
int number = 25;
int *ptr = &number;
printf("Value = %d", *ptr);
return 0;
}
Here, *ptr accesses the value of number.
Pointers can be used to allow a function to modify variables in the calling function.
#include <stdio.h>
void changeValue(int *x)
{
*x = 100;
}
int main()
{
int number = 20;
changeValue(&number);
printf("Number = %d", number);
return 0;
}
Number = 100
A structure allows different types of data to be grouped together under one name.
For example, student information may contain:
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student student = {101, "Rahul", 85.5};
printf("Roll No: %dn", student.rollNo);
printf("Name: %sn", student.name);
printf("Marks: %.2fn", student.marks);
return 0;
}
The dot (.) operator is used to access members of a structure.
student.rollNo
student.name
student.marks
printf("%d", student.rollNo);
An array of structures can store information about multiple records.
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student students[2] =
{
{101, "Rahul", 85.5},
{102, "Amit", 78.0}
};
int i;
for (i = 0; i < 2; i++)
{
printf("%d %s %.2fn",
students[i].rollNo,
students[i].name,
students[i].marks);
}
return 0;
}
This concept is useful for developing student management and record-management programs.
A union is similar to a structure, but its members share the same memory location.
union Data
{
int number;
float value;
char letter;
};
Unlike a structure, a union generally stores only one member’s value meaningfully at a time.
| Structure | Union |
|---|---|
| Each member has separate storage | Members share storage |
| Multiple members can hold values at the same time | Only one member should generally be treated as active at a time |
| Usually requires more memory | Can save memory |
An enumeration (enum) is a user-defined type containing named integer constants.
#include <stdio.h>
enum Day
{
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY
};
int main()
{
enum Day today = WEDNESDAY;
printf("Day number = %d", today);
return 0;
}
By default, the first enumerator usually has the value 0, the next 1, and so on unless values are explicitly assigned.
File handling allows a C program to store and retrieve information from files.
Files can be used to store:
The <stdio.h> header provides functions for file operations.
The fopen() function is used to open a file.
FILE *fp;
fp = fopen("filename.txt", "mode");
| Mode | Purpose |
|---|---|
"r" |
Read |
"w" |
Write |
"a" |
Append |
"r+" |
Read and write |
"w+" |
Write and read |
"a+" |
Append and read |
Opening a file in
"w"mode can replace existing contents, so it should be used carefully.
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.");
return 1;
}
fprintf(fp, "ADCA Student Recordn");
fprintf(fp, "Roll No: 101n");
fprintf(fp, "Marks: 85n");
fclose(fp);
return 0;
}
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("Unable to open file.");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
return 0;
}
| Function | Purpose |
|---|---|
fopen() |
Opens a file |
fclose() |
Closes a file |
fprintf() |
Writes formatted data |
fscanf() |
Reads formatted data |
fgetc() |
Reads one character |
fputc() |
Writes one character |
fgets() |
Reads a line/string |
fputs() |
Writes a string |
Always check whether fopen() successfully opened the file before performing file operations.
Dynamic memory allocation allows programs to request memory during runtime.
Common functions are provided by <stdlib.h>:
malloc()calloc()realloc()free()#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr;
ptr = malloc(5 * sizeof(int));
if (ptr == NULL)
{
printf("Memory allocation failed.");
return 1;
}
ptr[0] = 10;
ptr[1] = 20;
ptr[2] = 30;
printf("%d", ptr[1]);
free(ptr);
return 0;
}
Memory obtained dynamically with malloc(), calloc(), or realloc() should be released using free() when it is no longer needed.
Preprocessor directives begin with #.
#include <stdio.h>
#define PI 3.14159
#includeUsed to include header files.
#defineUsed to define macros.
Example:
#define MAX 100
Header files contain declarations and information needed by programs.
| Header | Common Purpose |
|---|---|
stdio.h |
Input/output |
stdlib.h |
General utilities and memory allocation |
string.h |
String operations |
math.h |
Mathematical functions |
ctype.h |
Character testing/conversion |
#include <stdio.h>
int main()
{
int numbers[5];
int i, largest;
printf("Enter 5 numbers:n");
for (i = 0; i < 5; i++)
{
scanf("%d", &numbers[i]);
}
largest = numbers[0];
for (i = 1; i < 5; i++)
{
if (numbers[i] > largest)
{
largest = numbers[i];
}
}
printf("Largest number = %d", largest);
return 0;
}
#include <stdio.h>
struct Student
{
int rollNo;
char name[30];
float marks;
};
int main()
{
struct Student s;
printf("Enter Roll Number: ");
scanf("%d", &s.rollNo);
printf("Enter Name: ");
scanf("%29s", s.name);
printf("Enter Marks: ");
scanf("%f", &s.marks);
printf("n--- Student Record ---n");
printf("Roll No: %dn", s.rollNo);
printf("Name: %sn", s.name);
printf("Marks: %.2fn", s.marks);
return 0;
}
Leave a Reply