Unit V: Input/Output and Low-Level Programming
1. Standard Input and Output
C provides a set of standard library functions for input and output operations, primarily defined in the stdio.h header file.
- Formatted Output (printf): Allows printing data to the standard output device with specific formatting like field width and precision.
- Formatted Input (scanf): Used to read formatted data from the standard input device (usually the keyboard).
- Variable Length Arguments: A mechanism (using stdarg.h) that allows functions to accept an undefined number of arguments, similar to how printf works.
2. Data Files and Processing
File handling allows programs to store data permanently on a disk.
- Opening and Closing: Files must be opened using fopen() before access and closed using fclose() to ensure data integrity.
- File Access Modes: Includes "r" (read), "w" (write), "a" (append), and "b" (binary modes).
- Unformatted Data Files: Files where data is stored in its raw binary form rather than human-readable text, often accessed via fread() and fwrite().
- Miscellaneous Functions: Includes functions like fseek() for random access, rewind(), and feof() to check for the end of a file.
3. Low-Level Programming
C allows programmers to perform tasks that are typically the domain of assembly language, providing close control over hardware.
- Register Variables: Using the register keyword suggests to the compiler that the variable be stored in a CPU register for faster access.
- Enumeration (enum): A user-defined data type consisting of a set of named integer constants, improving code readability.
- Library Functions: Standard C libraries provide low-level access to system resources and memory management.
4. Bitwise Operations and Fields
Bitwise operations allow for the manipulation of individual bits within a data type.
- Bit Fields: Allow the definition of structure members with a specific width in bits, saving memory in hardware-level structures.
5. Preprocessor and Macros
The C Preprocessor is a tool that examines the code before actual compilation begins.
Common Directives: #define, #include, #ifdef, #ifndef, #else, #endif.
- Macros: Short pieces of code given a name. Whenever the name is used, it is replaced by the contents of the macro.
- Header Files: Used to include external library definitions or user-defined common code.
6. Command Line Arguments
Command line arguments allow users to pass parameters to the main() function when the program is executed from the terminal.
- argc: Argument count (integer).
- argv: Argument vector (array of strings).
Exam Tips
- File EOF: Always check for the End-of-File (EOF) when reading files to prevent infinite loops.
- Bitwise shift: Remember that shifting left by 1 is equivalent to multiplying by 2, and shifting right by 1 is equivalent to dividing by 2 (integer division).
- Macro Safety: Always wrap macro arguments in parentheses to avoid unexpected operator precedence issues.
Frequently Asked Questions
Q: What is the difference between text and binary files?
Text files store data as sequences of characters (human-readable), while binary files store data exactly as it is represented in memory.
Q: Why use bit-fields?
They are used when memory is extremely limited or when you need to match a specific hardware register format.