CS50 — Week Two Notes

CS50 — Week Two Notes


Pset Grading <Perhaps class-specific, but not without value in a general sense.>

  • scope – right piece of the problem bitten off
  • correctness – program works as it’s supposed to?
  • design – subjective, will take the longest to learn… how well written is the code? how efficient?
  • style – how pretty is the code? well-named, indentations, readability
  • 1-5 (3 = good)


  • sending secret messages
  • inputs: the secret key + “plaintext”
  • outputs: ciphertext (to de-encrypt, just plug in ciphertext + key to get the original plaintext)
  • there’s no such thing as a string in C, just a series of characters stored in memory (RAM) back-to-back


  • allows you to switch from one data type to another
  • remember, it’s all 1s and 0s
  • for example, ASCII maps letters to numbers (capital “A” = 65, etc.)
  • after establishing some int i, can change types by putting in parenthesis the desired type and the current type next to it:
    • (char) i

Strings in C

  • when storing strings, C delineates the end with a \0 (8 0 bits in a row) that serves as a line in the sand of sorts marking out the end
  • strings by design have a dynamic number of characters, so there’s no way for the computer to know in advance how many character slots it’ll need

Command-line Arguments

  • int main(void)
    • no input taken in from the user taken at the command line
  • int main(int argc, string argv[])
    • array = similar data stored contiguously together in memory (generally, the same type/related data (string string string; int int int; etc.)
      • the string “Netanya” is technically just an array of chars
      • argv = an array of strings
  • how about the output/the return? it’s always int
    • in general, if nothing has gone wrong in the program, the convention is that 0 is returned
    • alternatively, if the program returns a negative value (or anything but 0), it generally signifies that something has gone wrong
      • exit codes” that are generally only useful/meaningful to the programmer or to the company shipping the software (I may see error messages with the numbers displayed, but as an end user they’re pretty meaningless to me)
    • if unspecified, main will return 0
  • to check return values, use echo $? in the terminal command line (useful for debugging)


  • when building bigger programs with 1,000s of lines of code, it’s not a good idea to keep everything in main(), b/c it’ll get harder and harder to find things in your code
  • also called: procedures, methods (esp. in object-oriented programming languages), subroutines
  • what is it?
    • a “black box” with a set of 0+ inputs and 1 output
    • “black box” b/c unless we write the function ourselves, we don’t really need to know the underlying implementation as long as the output is what we expect
      • important that when we write functions, they have clear names and documentation (for others’ ease of use)
  • why use?
    • organization – break up complicated problem into smaller parts
    • simplification – easier to write + debug small pieces of a program at a time
    • reusability – recycle + reuse elsewhere
  • step 1 = declaration
    • gives compiler a heads-up that the function is going to appear in code
    • should be before main()
    • return-type name(argument-list)
      • return-type is what kind of variable the function will output
      • name should be meaningful and point to usage
      • argument-list is what kind of input
  • step 2 = definition
    • write instructions for what to do in the “black box”
  • step 3 = call
    • use in the program where you need it
  • no input? void no output? void
Practice Problem! valid_triangle()

Variables and Scope

  • scope = variable characteristic defining from which functions that variable may be accessed
  • 2 primary scopes in C:
    • local variables
      • can only be accessed within the functions in which they are created
    • global variables
      • declared outside all functions (including main())
      • can be accessed by any function in the program
      • flexible, yet could have dangerous consequences if one function changes the variable’s value before you expected it to be changed
  • local variables in C are passed by value in function calls
    • the callee (function taking the variable as input) receives a copy of the passed variable, not the variable itself
    • the variable can’t be changed (really, doesn’t have an ability to have its value varied anymore, being just a copy of a value) unless overwritten in the caller (function that is making the function call – or, in other words, the function that the called function’s inside of)
  • scope isn’t such a problem if all variables are named distinctly, but can get nasty if same variable name appears in multiple functions
    • happens a lot in the real world when a team is working on a project together
    • esp. common xy, etc.
What’s printed?


  • a fundamental data structure to hold a bunch of integers, or a bunch of strings, without having to give each its own variable name (which could become cumbersome)
  • partitioned into identically-sized blocks of memory space called elements
  • each spot in an array can store a certain amount of data
  • elements can be accessed directly by an index
  • in C, elements are indexed starting from 0
    • ex: in an index of n elements, the first element located at index 0, and the last at index (n-1)
    • C won’t prevent me from going “out of bounds” of my array, but it’ll cause major problems, so I have to watch out
  • declarations
    • type name[size];
    • type = what data type the elements will be
    • name = what I want to call my array
    • size = how many elements I want my element to contain
  • if declaring and initializing an array simultaneously, use instantiation syntax or a loop
    • instantiation syntax:
      • bool truthtable[] = { false, true, true };
      • *notice that this syntax does not require me to specify number of elements in [] because the compiler is smart enough to count how many elements are listed in {}
  • can create as many size specifiers as desired, and can think of them as 2- or 3-dimensional, though in the computer’s memory, it will always be a one-dimensional array
    • example:
      • bool battleship[10][10];
      • in my mind: a 10×10 grid of cells on which to play battleship
      • useful abstraction to help the human mind perceive
  • in C, can’t treat entire arrays themselves as variables – can’t assign an array to another using the assignment operator (=)
    • instead, would have to copy over each individual element using a loop
    • other more modern languages do allow you to do this
  • unlike (local) variables, arrays in C are passed by reference in function calls (the callee receives the actual array and not a copy, so all changes made to the array within that function are effective everywhere else in the program that the array is)
  • ** Note to self: in the printf() function, the modifiers %d and %i have the same result – printing an integer (d is for “decimal”)

Command Line Arguments

  • if we want the user to provide data to our program at run time (instead of while the program is running), we want another form for main()
  • int main(int argc, string argv[])
    • these 2 special arguments allow me to know how much and what data the user provided at the command line
    • argc stands for “argument count” and is a conventional name (I could name it anything I want)
      • number of command-line arguments is counted by white spaces between them
    • argv[] stands for “argument vector” (another conventional name)
      • vector is another name for array
      • argv[0] will always be the first string the user typed (aka the program name)
      • argv[argc - 1] holds the last element of argv[]
  • it’s good to remember that whatever data is collected in argv[] is stored under the string data type

Magic Numbers

  • directly writing constants into our code is sometimes referred to as using magic numbers
  • it’s a bad habit and should be generally avoided
  • we want to give symbolic meaning to any constants
  • C provides a preprocessor directive (also called a macro) for creating symbolic constants:
    • #define NAME REPLACEMENT (** no semicolon!! *capitalization is convention and not requirement, but is good practice for clarity purposes)
    • other macros we’ve been using: #include <blahblahblah.h>
    • if #include is like copy/paste, then #define is like find/replace
    • at time of compilation, #define goes through code and replaces NAME with REPLACEMENT
    • #define PI 3.14159265 goes thru and replaces every pi in code with 3.14159265
    • #define COURSE "CS50" goes thru and replaces course with cs50

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s