Podcast
Questions and Answers
What is the primary function of the C preprocessor?
What is the primary function of the C preprocessor?
- To manage memory allocation during program runtime.
- To translate C code into assembly language.
- To execute before the compilation of a C program, handling directives such as file inclusion and macro definitions. (correct)
- To provide debugging tools for C programs.
Which preprocessor directive is used to include header files in a C program?
Which preprocessor directive is used to include header files in a C program?
- #include (correct)
- #header
- #insert
- #define
If a header file is included using angle brackets (e.g., #include <stdio.h>
), where does the preprocessor typically search for the file?
If a header file is included using angle brackets (e.g., #include <stdio.h>
), where does the preprocessor typically search for the file?
- Only in the current directory of the source file.
- In predesignated compiler and system directories. (correct)
- In a location specified by the user through environment variables.
- In the same directory as the compiler executable.
What is the purpose of the #define
preprocessor directive?
What is the purpose of the #define
preprocessor directive?
Consider the following preprocessor directive: #define PI 3.14159
. What happens during the preprocessing stage?
Consider the following preprocessor directive: #define PI 3.14159
. What happens during the preprocessing stage?
What is the role of the #undef
preprocessor directive?
What is the role of the #undef
preprocessor directive?
Which of the following preprocessor directives is used for conditional compilation?
Which of the following preprocessor directives is used for conditional compilation?
What is the purpose of conditional compilation directives like #ifdef
and #endif
?
What is the purpose of conditional compilation directives like #ifdef
and #endif
?
Why is it important to use header files and function prototypes in C programming?
Why is it important to use header files and function prototypes in C programming?
What is a function prototype in C?
What is a function prototype in C?
Which of the following best describes an array in C?
Which of the following best describes an array in C?
In C, how do you access the third element of an array named myArray
?
In C, how do you access the third element of an array named myArray
?
What happens if you try to access an element outside the bounds of an array in C?
What happens if you try to access an element outside the bounds of an array in C?
How can you determine the number of elements in an array in C?
How can you determine the number of elements in an array in C?
What will be the output of the following code snippet?
int arr[5] = {1};
printf("%d %d", arr[0], arr[4]);
What will be the output of the following code snippet?
int arr[5] = {1};
printf("%d %d", arr[0], arr[4]);
What is a typical use-case for size_t
in C?
What is a typical use-case for size_t
in C?
What is meant by 'arrays have no bounds checking' in C?
What is meant by 'arrays have no bounds checking' in C?
Consider the following code: char myString[] = "hello";
. How many elements are in myString
?
Consider the following code: char myString[] = "hello";
. How many elements are in myString
?
What is the primary difference between using scanf
and fgets
to read strings in C?
What is the primary difference between using scanf
and fgets
to read strings in C?
If you want to read a string containing spaces from the keyboard in C, which function is more suitable?
If you want to read a string containing spaces from the keyboard in C, which function is more suitable?
How are strings represented in C?
How are strings represented in C?
In C, what does the null character '\0' signify in a string?
In C, what does the null character '\0' signify in a string?
What is a multidimensional array in C?
What is a multidimensional array in C?
What will be the output of this C code snippet?
#include <stdio.h>
int main() {
int b[3][6] = {{1}, {3,4}, {5, 6}};
printf("%ld \n", sizeof(b) / sizeof(b[0]));
return 0;
}
What will be the output of this C code snippet?
#include <stdio.h>
int main() {
int b[3][6] = {{1}, {3,4}, {5, 6}};
printf("%ld \n", sizeof(b) / sizeof(b[0]));
return 0;
}
Given int b[2][3] = {{1, 2, 3}, {4, 5, 6}};
, how do you access the element with value 5
?
Given int b[2][3] = {{1, 2, 3}, {4, 5, 6}};
, how do you access the element with value 5
?
Flashcards
Function Implementation
Function Implementation
A function should be implemented or its prototype included before being called to ensure compilation.
Function Prototype
Function Prototype
A declaration of a function's name, return type, and parameters without the function body.
Header File
Header File
A separate file (with a .h extension) that contains function prototypes and other declarations.
#include Directive
#include Directive
Signup and view all the flashcards
C Preprocessor
C Preprocessor
Signup and view all the flashcards
#include Directive
#include Directive
Signup and view all the flashcards
#define Directive
#define Directive
Signup and view all the flashcards
Macros
Macros
Signup and view all the flashcards
#undef Directive
#undef Directive
Signup and view all the flashcards
Conditional Compilation
Conditional Compilation
Signup and view all the flashcards
Array
Array
Signup and view all the flashcards
Array Initialization
Array Initialization
Signup and view all the flashcards
_countof
_countof
Signup and view all the flashcards
Array Bounds Checking
Array Bounds Checking
Signup and view all the flashcards
String
String
Signup and view all the flashcards
fgets
fgets
Signup and view all the flashcards
Multidimensional Arrays
Multidimensional Arrays
Signup and view all the flashcards
Study Notes
Functions
- A function needs implementation before invocation
- A function prototype needs inclusion before calling the function as another option
- The function prototype represents the header, minus the body
- Function prototypes let the compiler validate function calls
- Prototypes of functions are placable in a separate header file with a .h extension
- The #include "file.h" directive includes a header file into a program
Squares Function Example
- The example includes the stdio.h and Math.h in this Driver.c example
- A function named squares uses a double type parameter
- Inside the main function, the program prompts for a number and calculates squares by calling the squares function in a loop
- The squares function returns the square of a given number using the pow function
Squares Header File Example
- Driver.c has standard library headers like stdio.h, Math.h, and Windows.h included
- Driver.c has a custom header file named "Squares.h" included
- Squares.h contains only the declaration of the squares function with the void return type, it serves as a function prototype
Preprocessor - #include
- The C preprocessor executes before compilation
- Actions performed include file inclusion, definition of constants and macros, conditional compilation, and conditional execution
- All preprocessor directives start with a #
Preprocessor #include directive
- The #include preprocessor directive puts a copy of a specified file into the directive's place of the directive
- The directive comes in two forms: #include
and #include "filename" - A filename in quotes searches in the same directory as the file, used for the programmer-defined headers
- Angle brackets around the filename (< and >) specify standard library headers searched through the designated system or compiler directories
Preprocessor - #define
- The #define directive creates symbolic constants
- #define identifier replacement_text replaces all identifier occurrences before compilation
- Macros, are a type of functions symbol that can be defined with or without arguments
- Arguments in a macro are substituted in the replacement text, which will replace the identifier and argument list in the program
Preprocessor - #define macros
- The code demonstrates macro definitions for PI and calculating the area of a circle
- In the past, macros with inline code avoided function calls, but now compilers inline function calls or c inline keywords
- Another code snipper defines a MAX macro that returns the greater of two arguments
#include <stdio.h>
#define MAX(a,b) ((a)>(b)?(a):(b))
void main() {
printf("Maximum between 10 and 20 is: %d\n", MAX(10,20));
}
Preprocessor - #undef
- The #undef preprocessor directive is used for undefining a macro
- The syntax is "#undef MACRO_NAME" where MACRO_NAME is the symbolic constant or macro that has to be undefined
Preprocessor - Conditional Compilation
- Conditional compilation directives determine whether a constant is defined already, such as #if !defined(PI)
- These directives use #define to define a constant if it is not already defined
- #ifdef checks if a constant is defined, while #ifndef checks if a constant is not defined
Arrays
- Arrays represent a data structure with containing data items of the same data type
- Arrays are static entities, that maintain the same size throughout program execution
- Arrays are a contiguous memory location
- The syntax array[index] accesses array element
- Example code shows array and element assignment, such as int c[] = {1, 5, 3, 78};
- Arrays are not automatically initialized to zero
- Initializing the array's first element ensures zero initialization for all remaining elements
Arrays Example
- The example initializes one element and ensures zero initialization for all remaining elements
- The code displays garbage values due to the array initialization state
- The size_t type helps represent the difference in RAM locations
- size_t does not rely on data structure size
Arrays Syntax
- Macros, such as _countof(array), get the number of elements inside an array
- It returns sizeof(array) / sizeof(array[0])
Arrays – Security Issue
- C lacks array bounds checking
- Executing programs may "walk off" an array without warning
- All array references should stay within array bounds
Strings
- Strings are character arrays
- char string[] = "object"; is an example of initializing string
- The array contains an extra element \0 for a null terminator called the String termination character
- scanf reads characters until a whitespace like space, tab, or newline is reached
- The statement fgets (string2, 10, stdin) will read until new line
Strings example
#include <stdio.h>
int main() {
char a[10];
fgets(a, 10, stdin);
for (int i = 0; i < 10 && a[i] != '\0'; i++)
printf("%C - ", a[i]);
}
Multidimensional Arrays
- Arrays in C allow multiple indices
- The syntax int b[2][3] identifies 2D array by row and column indices
- Example values are int b[2][3] = {{1, 2, 3}, {4, 5, 6}};
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.