Lecture 6 - Preprocessory Directives in C Flashcards

1
Q

What is stream-based I/O in C?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the purpose of the fopen function in C?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does the function fprintf do?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How does buffering work in C I/O?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the basic access modes for fopen?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the use of fflush in C?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the EOF constant used for in C?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the difference between printf and fprintf?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you open a file for both reading and writing in C?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What function is used to close a file in C, and why is it important?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the purpose of the C pre-processor?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does the #include directive do in C?

A

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").

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the purpose of the #define directive?

A

#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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are conditional directives in C?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the #pragma directive used for?

A

#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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How does #define work with parameters (macros)?

A

#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.

17
Q

What are some potential problems with using macros in C?

A

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.

18
Q

How can you avoid operator precedence errors in macros?

A

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.

19
Q

What are predefined macros in C, and give some examples?

A

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).

20
Q

How can #define be used to create inline functions in C?

A

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.

21
Q

What is #line directive in C used for?

A

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.