Structure Definition
Structure Definition
A structure in C is a user-defined data type that allows grouping of variables of different data
types under a single name. It is defined using the struct keyword.
Example:
struct Student {
int id;
char name[50];
float grade;
};
In this example, Student is a structure containing an integer (id), a character array (name),
and a float (grade).
2. Structure Initialization
Structures can be initialized in two ways: when defining variables or by assigning values to
members later.
Example:
Here, s1 is initialized with values at the time of declaration, and then values are updated
individually.
3. Accessing Structures
Structure members are accessed using the dot (.) operator. If you have a pointer to a
structure, the arrow (->) operator is used.
Example:
4. Nested Structures
Example:
struct Date {
int day, month, year;
};
struct Student {
int id;
char name[50];
struct Date dob; // Nested structure
};
5. Arrays of Structures
You can create an array of structures to store data for multiple entities.
Example:
This example shows an array of Student structures to store information about multiple
students.
Example:
displayStudent(s1);
updateGrade(&s1, 95.0);
7. Self-Referential Structures
A self-referential structure contains a pointer to a structure of the same type, which is
commonly used in linked lists.
Example:
struct Node {
int data;
struct Node *next; // Self-referential
};
8. Unions
A union is similar to a structure but only allows one member to hold a value at any given
time, saving memory.
Example:
union Data {
int i;
float f;
char str[20];
};
union Data d;
d.i = 10;
printf("i: %d\n", d.i);
d.f = 220.5;
printf("f: %.2f\n", d.f); // i is now undefined
9. Typedef
typedef allows creating an alias for a data type, making code easier to read and manage.
Example:
typedef struct {
int id;
char name[50];
} Student;
Student s3;
s3.id = 3;
strcpy(s3.name, "David");
This way, Student can be used directly as a type without using struct.
10. Enumerations
An enumeration defines a list of named integer constants, often used to represent states or
options.
Example:
FILE HANDLING
1. Command Line Arguments
Command line arguments are used to pass arguments to a program from the command line. In
C, these arguments are accessed through main() function parameters int argc (argument
count) and char *argv[] (argument vector).
Example:
#include <stdio.h>
Usage:
Output:
Number of arguments: 3
Argument 0: ./program
Argument 1: arg1
Argument 2: arg2
2. File Modes
File modes determine how a file is opened, such as for reading, writing, or appending.
Common file modes in C include:
Example:
In C, file operations are performed using standard functions like fopen, fclose, fprintf,
fscanf, fgets, fputs, etc.
Writing to a File
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("file.txt", "w");
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
fprintf(file, "Hello, World!\n");
fclose(file);
return 0;
}
This opens file.txt in write mode and writes "Hello, World!" to it.
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("file.txt", "r");
char buffer[100];
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
fclose(file);
return 0;
}
This reads lines from file.txt and prints them to the console.
Appending to a File
Example:
#include <stdio.h>
int main() {
FILE *file = fopen("file.txt", "a");
if (file == NULL) {
printf("File could not be opened.\n");
return 1;
}
fprintf(file, "This is an appended line.\n");
fclose(file);
return 0;
}
Example:
#include <stdio.h>
void function() {
int local_var = 20; // Local scope
static int static_var = 30; // Retains value between calls
printf("Global: %d, Local: %d, Static: %d\n", global_var, local_var,
static_var);
static_var++;
}
int main() {
function();
function();
return 0;
}
Output:
5. Multi-File Programming
In multi-file programs, code is split across multiple files for better organization and
modularity.
For example, consider a program split into two files: main.c and helper.c. A header file
(helper.h) is also created to share function declarations.
helper.h:
#ifndef HELPER_H
#define HELPER_H
void displayMessage();
#endif
helper.c:
#include <stdio.h>
#include "helper.h"
void displayMessage() {
printf("Hello from helper.c!\n");
}
main.c:
#include "helper.h"
int main() {
displayMessage();
return 0;
}
Compilation:
gcc main.c helper.c -o program
./program
Output: