Unit 5: Input/Output, Files, and Low-Level Programming
Table of Contents
5.1 Standard Input and Output
Refers to I/O from the default console (keyboard for input, screen for output).
Formatted Output (printf)
Provides fine-grained control over output format.
printf("Item: %-10s Price: %7.2f\n", "Apple", 1.2); // %-10s: Left-justify string in a 10-char-wide field // %7.2f: Right-justify float in a 7-char-wide field with 2 decimal places Formatted Input (scanf)
Reads formatted input. Its return value is the number of items successfully read.
scanf can be tricky. It often leaves the newline character (\n) in the input buffer, which can cause problems for subsequent scanf or gets calls.Variable Length Arguments
This is the mechanism that allows printf and scanf to take a variable number of arguments. It is an advanced feature implemented using the <stdarg.h> header.
5.2 Data Files
File I/O allows your program to read from and write to files on disk, making data persistent.
Opening and Closing a File
You use a FILE pointer. fopen() opens a file and fclose() closes it.
| Mode | Description |
|---|---|
"r" | Read: Open an existing file for reading. |
"w" | Write: Create a new file for writing. (Overwrites existing) |
"a" | Append: Open or create a file to write at the end. |
"r+", "w+", "a+" | Read and Write modes. |
FILE *fp; fp = fopen("data.txt", "w"); if (fp == NULL) { printf("Error opening file!\n"); return 1; // Exit } // ... do file operations ... fclose(fp); // Always close the fileProcessing a Data File (Formatted)
These functions work just like printf/scanf but for files.
fprintf(fp, "...", ...): Writes formatted text to a file.fscanf(fp, "...", ...): Reads formatted text from a file.
Unformatted Data Files (Binary)
Used for reading/writing raw binary data (like an array or a struct) directly to a file. Much faster and more efficient for non-text data.
fwrite(ptr, size, count, fp): Writescountitems ofsizefromptrtofp.fread(ptr, size, count, fp): Readscountitems ofsizefromfpintoptr.
Miscellaneous Functions
feof(fp): Returns true if the end-of-file has been reached.fseek(fp, offset, whence): Moves the file pointer to a specific location (for random file access).
5.3 Low-Level Programming
Register Variables
register int x;
This is a hint to the compiler to store this variable in a CPU register for faster access. Modern compilers are very good at optimization and often ignore this keyword.
Bitwise Operations
Operations that manipulate data at the individual bit level. Essential for hardware control, data compression, and encryption.
| Operator | Name | Example (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 (Depends on size) |
<< | Left Shift | 0101 << 1 | 1010 (10) |
>> | Right Shift | 0101 >> 1 | 0010 (2) |
Bit Fields
Allow you to pack data into a structure to save memory, specifying the exact number of bits for each member.
struct Flags { unsigned int isActive : 1; // Use only 1 bit unsigned int isDirty : 1; // Use only 1 bit unsigned int mode : 2; // Use only 2 bits };Enumeration (enum)
A user-defined type consisting of a set of named integer constants. Improves code readability.
enum Day { MON = 1, TUE, WED, THU, FRI, SAT, SUN }; enum Day today = WED; if (today == 3) { // true printf("It's Wednesday!\n"); }5.4 Advanced Topics (Macros, Library)
Command Line Arguments/Parameters
This topic is also listed in Unit 3
. It refers tomain(int argc, char *argv[]), allowing the program to accept parameters from the command line. Library Functions
The C Standard Library provides a rich set of functions, e.g.:
<string.h>:strcpy,strlen,strcmp<stdlib.h>:malloc,free,exit,rand<math.h>:sqrt,pow,sin
Macros (The C Preprocessor)
This topic is also listed in Unit 2
.#define is used to create constants and function-like macros. The preprocessor also supports conditional compilation: #define DEBUG ... #ifdef DEBUG printf("Debug message: x = %d\n", x); #endif