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 #if fails.
  • #endif: Marks the end of #if or #ifdef.
  • #pragma: Provides specific instructions to the compiler (e.g., disable warnings, set alignment).
    • #pragma startup: Specifies functions to run before main().
    • #pragma exit: Specifies functions to run just before exiting main().

File Inclusion

  • Allows including external files (header files, libraries) using the #include directive.
  • 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 A holds an integer x. A variable a of type struct A is created, and a.x is initialized to 11.

Structure Definition

  • Defined using the struct keyword, 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;
    // Declaration of Inner structure
    struct Student {
        int student_id;
        char name[20];
        int roll_no;
    } student1;
    
    }; int main() { struct College c1 = {"ICCS", 7, {111, "abc", 278}};
    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

  1. Creating a new file.
  2. Opening an existing file.
  3. Reading from a file.
  4. Writing to a file.
  5. Moving to a specific location in a file.
  6. Closing a file.

Types of Files

  1. 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 .txt extension.
    • Can store source code.
  2. 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 .bin extension.

File Pointer

  • A reference to a position in the opened file.
  • Used for file operations: read, write, close, etc.
  • Declared using the FILE macro (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.