Arrays, Strings, and PRNG in C Flashcards
What is an array in C?
An array is a contiguous block of memory containing elements of the same type. The size of the array is determined during initialization.
Does C perform bounds checking on arrays?
No, C does not perform bounds checking, which can lead to undefined behavior if an index is out of range.
How do you declare and initialize a 1D array in C?
int array[5] = {1, 2, 3, 4, 5};
How do you declare and initialize a 2D array in C?
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
What is a string in C?
A string is an array of characters, terminated by the null character (\0).
How do you initialize a string?
char str[] = “Hello”;
What happens if a string is missing a null terminator?
Functions like printf may continue reading beyond the allocated memory, causing undefined behavior.
What does strlen do?
Returns the number of characters in a string, excluding the null terminator.
How does strcpy work?
Copies the content of one string into another, including the null terminator.
strcpy(dest, src);
What does strcat do?
Appends one string to another, overwriting the null terminator of the first string.
How does strcmp compare two strings?
Returns 0 if strings are equal, a positive value if the first is greater, or a negative value if the second is greater.
What is a pseudo-random number?
A number generated by an algorithm that appears random but follows a deterministic sequence
How do you generate a random number in C?
Use the rand() function, which generates an integer between 0 and RAND_MAX.
How do you seed the random number generator in C?
Use the srand() function, typically with the current time:
srand(time(0));
What are some common random number distributions?
Uniform: All values in a range are equally likely.
Normal (Gaussian): Forms a bell curve.
Poisson: Counts events in a fixed interval.
Bernoulli: Two outcomes (0 or 1).
Why is C considered weakly typed?
It allows implicit type conversions, which can lead to unintended behavior.
What happens when you assign a float to an integer?
The decimal portion is truncated.
Name some special characters in C and their meanings.
\n: New line
\t: Tab
\: Backslash
': Single quote
": Double quote
What are the key topics covered in this slide deck?
Arrays and bounds checking.
Strings and their null-terminated nature.
Common string functions (strlen, strcpy, strcat, strcmp).
Pseudo-random numbers, seeding, and distributions.
Weak typing in C.