Term
What happens here?
char str1[] = "Hello\0everyone!"; char str2[] = "Happy Friday!";
printf(str1); |
|
Definition
|
|
Term
What is wrong with this code? (4)
char str1[] = "Hello\0everyone!"; printf(%s, str1[6]; |
|
Definition
1. %s wants a pointer to a character, but str1[6] gives it an integer 2. (characters are upgraded to integers!). 3. To get a character pointer, we need to do &str1[6]. 4. Or str1 + 6. |
|
|
Term
Difference between sizeof and strlen |
|
Definition
Sizeof gets the size in memory, which includes \0. Strlen does not include \0. |
|
|
Term
Difference between char str[] = "Hello World!" and char *str = "Hello World!" |
|
Definition
The first is mutable, the second is an immutable string literal |
|
|
Term
What is the difference between strcmp and strncmp? Strcopy and strncopy? |
|
Definition
Strncmp and strncopy specify a fixed length |
|
|
Term
What is wrong with this code?
char str1[] = "Hello World!"; char str2[] = str1; |
|
Definition
str2[] is initialized as a constant pointer. That means it can't point at anything else. |
|
|
Term
What is wrong with this code?
char str1[] = "Hello World!"; char str2[12]; strcpy(str2, str1); |
|
Definition
Char str2[12] specifies the length of the array, not the size. Str1 has a size of 13 because of the null character, so when it gets copied until str2, the null character is not copied. That means the null character is placed outside the bounds of str2. |
|
|
Term
Why doesn't this code work?
char *amazing_str = "Gotta get down on Friday!"; strcpy(amazing_str, "Matthew"); |
|
Definition
Amazing_str has been declared as an immutable string literal |
|
|
Term
|
Definition
You can put in a string with size less than int, but not greater than int |
|
|
Term
When to use: char *my_string char other_str [] char amazing_str[128] |
|
Definition
Use * when you don't need to change anything. Use [] when you might want to change the characters in place, but don't anticipate having to put new things in. Use [128] (or some other large number) when you anticipate having to add new things in. |
|
|
Term
Difference between strcpy and strcat |
|
Definition
Strcpy overrides what is in the destination string, strcat just attaches to the end of it |
|
|
Term
Program to get age input from the user |
|
Definition
printf("Please enter your age: "); int age; scanf("%d", &age); |
|
|
Term
What does this mean? ls -l / > list_of_files.txt |
|
Definition
|
|
Term
Why is stderr slower than stdout? |
|
Definition
Stderr is not buffered, it is flushed to the terminal immediately |
|
|
Term
|
Definition
Output redirection -- sends stdout to a file instead of the terminal |
|
|
Term
|
Definition
Output redirection, but appending to the file instead of overwriting |
|
|
Term
|
Definition
Input redirection, reads from file instead of stdin |
|
|
Term
Why can printing to the terminal be misleading sometimes? |
|
Definition
There is no guarantee that what you did in your code will match what's displayed on the screen because standard input is getting buffered. Printf() might execute, but the program might crash before any output is displayed. |
|
|
Term
How does buffered I/O speed up the I/O process? |
|
Definition
It collects multiple I/O operations, combines them, and then executes them as one big operation |
|
|
Term
Tradeoff between flushing every output versus using buffering |
|
Definition
Flushing: sometimes when printing, your program crashes before the butter gets cleared, and data is lost
Buffering increases speed. Performing printf takes several steps: get permission from OS, OS goes to device, device gets your display involved |
|
|