Knowlet

Unit 5: Input/Output, Files, and Low-Level Programming

1. Standard Input and Output

This refers to I/O operations from the console (keyboard and screen), which are represented by stdin and stdout.

Formatted Output (printf)

printf ("print formatted") is used to print data to the screen. It uses format specifiers (like %d) to indicate how to print a variable.

  • %d or %i: Integer
  • %f: Float
  • %c: Character
  • %s: String (char array)
  • %p: Pointer (address)

printf is a variable length argument function, meaning it can take a different number of arguments each time it's called. This is made possible by the ... (ellipsis) syntax, handled by the library.

Formatted Input (scanf)

scanf ("scan formatted") is used to read data from the keyboard. It uses the same format specifiers as printf.

You must pass the address (using &) of the variable you want to fill.
int age; scanf("%d", &age); // Correct scanf("%d", age); // WRONG: Will crash
The only exception is for strings (char arrays), which do not need & because the array name *is* an address.
char name[50]; scanf("%s", name); // Correct

2. Data Files

File handling allows your program to read from and write to files on the disk, making data persistent. All file operations use a special pointer: FILE *fp;.

Opening and Closing Files (fopen, fclose)

You must open a file before you can use it. fopen() returns a FILE pointer, or NULL if it fails.

 FILE *fp = NULL; // Open "data.txt" for writing ("w") fp = fopen("data.txt", "w"); if (fp == NULL) { printf("Error opening file!\n"); return 1; // Exit } // ... do file operations ... // Always close the file when done fclose(fp); 

Common File Modes:

  • "r": Read (file must exist)
  • "w": Write (creates file; truncates/overwrites if it exists)
  • "a": Append (creates file; adds to the end if it exists)
  • "rb", "wb", "ab": Same, but for binary files.

Processing Files (Formatted vs. Unformatted)

  • Formatted (Text) Files: Use fscanf() and fprintf(). These are just like scanf/printf, but the first argument is the FILE pointer.
    fprintf(fp, "Name: %s, Age: %d\n", "Ravi", 25); int age; fscanf(fp, "%d", &age);
  • Unformatted (Binary) Files: Use fread() and fwrite(). These read/write raw bytes of data (like a struct) directly. They are much faster and more precise.
     struct Student s1; // Write the s1 object to the file fwrite(&s1, sizeof(struct Student), 1, fp); // Read one Student object into s2 struct Student s2; fread(&s2, sizeof(struct Student), 1, fp);

Miscellaneous Functions

  • fseek(fp, offset, whence): Moves the file pointer to a specific location.
  • ftell(fp): Returns the current position of the file pointer.
  • rewind(fp): Moves the file pointer back to the beginning of the file.

3. Low-Level Programming

This involves operations that are closer to the hardware, often manipulating individual bits.

Register Variables

This is a repeat from Unit 2. The register keyword suggests to the compiler that a variable should be stored in a CPU register for faster access. It's often used for loop counters, but modern compilers are smart enough to do this automatically.

Bitwise Operations

These operators work on the binary (bit) representation of integers.

OperatorNameExample (a=5, b=3)Result
& Bitwise AND 0101 & 0011 0001 (1)
| Bitwise OR 0101 | 0011 0111 (7)
^ Bitwise XOR 0101 ^ 0011 0110 (6)
~ Bitwise NOT ~0101 1010 (-6)
<< Left Shift 0101 << 1 1010 (10)
>> Right Shift 0101 >> 1 0010 (2)
Uses: Setting/checking a specific bit (e.g., flags), high-speed multiplication/division by 2, encryption.

Bit Fields

Bit fields allow you to define structure members that are only a specific number of bits wide. This is used to pack data tightly, often for hardware programming.

 // This structure packs a date into 16 bits (2 bytes) struct Date { unsigned int day : 5; // 5 bits (can hold 0-31) unsigned int month : 4; // 4 bits (can hold 0-15) unsigned int year : 7; // 7 bits (can hold 0-127) }; 

Enumeration (enum)

An enumeration is a user-defined type that consists of a set of named integer constants. It makes code more readable.

 // Define an enum for days of the week enum Weekday { MON, TUE, WED, THU, FRI, SAT, SUN }; int main() { // By default, MON=0, TUE=1, WED=2, ... enum Weekday today = WED; if (today == 2) { // Works, but less readable //... } if (today == WED) { // Much better //... } return 0; } 

4. Other Topics

This unit re-lists several topics from previous units, likely for reinforcement.

Command Line Arguments

See Unit 3 notes. This refers to int main(int argc, char *argv[]) for receiving arguments when the program is executed.

Library Functions & Preprocessor

  • Library Functions: These are pre-written functions (like printf, strcpy, fopen) available in C's standard library.
  • Macros / The C Preprocessor: See Unit 2 notes. This refers to #include and #define.

Did this resource help you study?

Share feedback or report issues to help improve this resource.