C Programming - Preprocessors, Structures, and File Handling
Preprocessors
- Programs that process the source code before compilation.
- Not part of compilation, but a separate process for code modification.
- The first process in converting C source code to an executable file.
Preprocessor Directives
#define: Defines a macro.#undef: Undefines a macro.#include: Includes a file in the source code.#if: Checks for a specified condition.#else: Alternate code that executes when#iffails.#endif: Marks the end of#ifor#ifdef.#pragma: Provides specific instructions to the compiler (e.g., disable warnings, set alignment).#pragma startup: Specifies functions to run beforemain().#pragma exit: Specifies functions to run just before exitingmain().
File Inclusion
- Allows including external files (header files, libraries) using the
#includedirective. - Syntax:
#include <file_name>: Searches for the file in the standard directory.#include "filename": Searches for the header file in the source file's directory.
Macros
- A symbolic name or constant representing a value, expression, or code snippet.
- Defined using
#define; the preprocessor substitutes it with its content.
typedef vs #define
- Both create aliases, but differ significantly.
typedef:- A keyword to create type aliases, checked by the compiler.
#define:- A preprocessor directive for textual substitution without type checking.
Structures
Groups related variables into one place.
Each variable is a "member" of the structure.
Can contain different data types (int, float, char, etc.).
Example:
#include <stdio.h> // Defining a structure struct A { int x; }; int main() { // Creating a structure variable struct A a;
}// Initializing member a.x = 11; printf("%d", a.x); return 0;Explanation: Structure
Aholds an integerx. A variableaof typestruct Ais created, anda.xis initialized to 11.
Structure Definition
Defined using the
structkeyword, followed by the structure name and its members.Also called a structure template or prototype.
No memory is allocated during the declaration.
Syntax:
struct structure_name { data_type1 member1; data_type2 member2; ... };
Copying Structures
- As simple as copying other variables using the assignment operator.
- Example:
s2 = s1;
Nested Structures
A structure within another structure.
Allows creation of complex data types.
Syntax:
struct name_1 { member1; member2; ... struct name_2 { member_1; member_2; ... } var1; } var2;Example:
#include <stdio.h> #include <string.h> // Declaration of Outer structure struct College { char college_name[20]; int ranking;
}; int main() { struct College c1 = {"ICCS", 7, {111, "abc", 278}};// Declaration of Inner structure struct Student { int student_id; char name[20]; int roll_no; } student1;
}printf("College name : %s\n", c1.college_name); printf("Ranking : %d\n", c1.ranking); printf("Student id : %d\n", c1.student1.student_id); printf("Student name : %s\n", c1.student1.name); printf("Roll no : %d\n", c1.student1.roll_no); return 0;
File Handling
- Process of creating, opening, reading, writing, and closing files.
- C provides functions like
fopen(),fclose(),fprintf(), etc.
C File Operations
- Creating a new file.
- Opening an existing file.
- Reading from a file.
- Writing to a file.
- Moving to a specific location in a file.
- Closing a file.
Types of Files
- Text Files:
- Contains data in ASCII characters; stores a stream of characters.
- Each line ends with a newline character (
'\n'). - Readable and writable by text editors.
- Stored with
.txtextension. - Can store source code.
- Binary Files:
- Contains data in binary form (0s and 1s) instead of ASCII.
- Stored similarly to main memory.
- Created and read only by programs.
- More secure as they are not easily readable.
- Stored with
.binextension.
File Pointer
- A reference to a position in the opened file.
- Used for file operations: read, write, close, etc.
- Declared using the
FILEmacro (defined in<stdio.h>). - Syntax:
FILE* pointer_name; - Used in almost all file operations.
Opening Modes
R: Read mode.Fp = fopen(filename, "r");
W: Write mode.Fp = fopen(filename, "w");
A: Append mode.Fp = fopen(filename, "a");
Built-in Functions
fopen(): Creates a new file or opens an existing one.fclose(): Closes a file.getc(): Reads a character from a file.putc(): Writes a character to a file.fscanf(): Reads a set of data from a file.fprintf(): Writes a set of data to a file.