Reading Someone Else’s Code

Recall that functions like get_char and get_string from the CS50 Library are declared in a file called cs50.h that you can #include in your own programs. Those functions are actually implemented in a file called cs50.c, which you can see at https://gist.github.com/dmalan/522bc25f1f4c362525beca2be4ec016e.

  • Review the implementation of get_char in cs50.c. Note that CHAR_MAX is a constant defined in limits.h that specifies the maximum value that a char can hold.

  • Read up on sscanf, as via man or Google.

  • Review the implementation of get_string and teardown in cs50.c. Note that the destructor attribute causes teardown to be called automatically after main has completed or exit has been called in a program.

  • Read up on static, as via Google.

Answer the below in reading.txt.

Questions

  1. (2 points.) Why do we return CHAR_MAX on failure in get_char (when its call to get_string returns NULL) rather than NULL?

  2. (2 points.) If get_char, per its own name, is meant to get just one char from a user, why do we try to read two via sscanf?

  3. (2 points.) Why do we declare allocations and strings as static? What might happen if we didn’t?

  4. (2 points.) How does the library ensure that every string allocated by get_string is eventually freed?

Debrief

  1. Which resources, if any, did you find helpful in answering this problem’s questions?

  2. About how long did you spend on this problem’s questions?