Lecture 6 - Preprocessory Directives in C Flashcards
What is stream-based I/O in C?
C provides an extensive library for stream-based I/O, which allows reading/writing from/to the console, file system, or memory buffers. It supports both text files (divided into lines) and binary files (sequences of bytes), with sequential or direct access.
What is the purpose of the fopen
function in C?
fopen
opens a file and returns a pointer to a file descriptor. If it fails, it returns NULL
. The first argument is the filename, and the second is the mode, which can be ‘r’ (read), ‘w’ (write), ‘a’ (append), etc.
What does the function fprintf
do?
fprintf
writes formatted output to a specified stream. It is similar to printf
but allows output to be directed to a specific file or stream, e.g., fprintf(stderr, "Error message");
.
How does buffering work in C I/O?
Streams are automatically buffered for efficiency. The buffer can be flushed using fflush
, and fclose
flushes the buffer before closing the stream. The buffering can be modified using setvbuf
.
What are the basic access modes for fopen
?
The common access modes are: ‘r’: Open an existing file for input. ‘w’: Create a new file or truncate an existing one for output. ‘a’: Append to an existing file or create a new file for output.
What is the use of fflush
in C?
fflush
flushes the output buffer of a stream, forcing the written data to be sent to the destination, which is useful for ensuring that all data is written to a file before the file is closed.
What is the EOF
constant used for in C?
EOF
(End of File) is a constant used to indicate the end of a file or an error when performing input/output operations, such as in a while
loop that reads characters until EOF
is reached.
What is the difference between printf
and fprintf
?
printf
outputs to the standard output (typically the console), while fprintf
can output to a specified file or stream, allowing formatted output to destinations other than the console.
How can you open a file for both reading and writing in C?
Use the mode ‘r+’ for opening an existing file for both reading and writing, or ‘w+’ for creating a new file or truncating an existing one for update.
What function is used to close a file in C, and why is it important?
fclose
is used to close a file, ensuring that any data still in the buffer is written to the file and that resources associated with the file are released.
What is the purpose of the C pre-processor?
The pre-processor performs textual processing of source code before compilation, handling directives like #include
, #define
, and #pragma
. It processes the source code to make necessary substitutions and inclusions before the actual compilation begins.
What does the #include
directive do in C?
The #include
directive is used to insert the contents of another file into the current source code file. It can include standard library headers (#include
) or user-defined files (#include "filename"
).
What is the purpose of the #define
directive?
#define
is used to create symbolic constants or macros in C. It defines a textual substitution that replaces the defined name with its associated value or expression throughout the code.
What are conditional directives in C?
Conditional directives, such as #ifdef
, #ifndef
, #if
, #else
, and #endif
, allow conditional compilation by including or excluding parts of the code based on whether a particular symbol is defined or an expression evaluates to true.
What is the #pragma
directive used for?
#pragma
provides special instructions to the compiler, often used to offer hints for optimization or to suppress warnings. It is compiler-specific and may vary between different compilers.
How does #define
work with parameters (macros)?
#define
can define macros with parameters, which act like functions but are expanded by textual substitution. For example, #define SQUARE(x) ((x)*(x))
expands into code that computes the square of a value, but does not provide type checking.
What are some potential problems with using macros in C?
Macros can cause errors if their parameters have side effects, or if the expansion does not properly account for operator precedence. This can lead to unexpected behavior, such as incorrect evaluation of expressions.
How can you avoid operator precedence errors in macros?
Enclose macro parameters in parentheses to ensure correct precedence when the macro is expanded. For example, #define SQUARE(x) ((x)*(x))
ensures that the expression SQUARE(a+1)
is evaluated correctly.
What are predefined macros in C, and give some examples?
Predefined macros provide information about the compilation process. Examples include \_\_FILE\_\_
(current file name), \_\_LINE\_\_
(current line number), \_\_DATE\_\_
(compilation date), and \_\_TIME\_\_
(compilation time).
How can #define
be used to create inline functions in C?
You can use #define
to create simple macros that mimic inline functions. For example, #define MAX(a, b) ((a) > (b) ? (a) : (b))
defines a macro that returns the maximum of two values. However, care must be taken to avoid side effects.
What is #line
directive in C used for?
The #line
directive is used to change the current line number and filename, primarily for error reporting and debugging. It can alter how the preprocessor perceives the file’s line number and file name.