๐ŸŒ CS50x: Introduction to Computer Science ยท subject

CS50x: Introduction to Computer Science Programming in C Syllabus

The Programming in C syllabus for CS50x: Introduction to Computer Science in full: 6 chapters and 21 topics, every one listed below, plus 50 Programming in C flashcards with answers.

6Chapters
21Topics
50Flashcards

Programming in C syllabus: all 6 chapters

Expand any chapter to see its topics and sub-topics.

  1. C Fundamentals

    4 topics
    • Source Code, Machine Code, and Compilers
    • Syntax and Escape Sequences
    • Header Files and Libraries
    • Variables and Constants
  2. Data Types and Operators

    4 topics
    • Data Types
    • Operators
    • Integer Overflow
    • Floating-Point Imprecision
  3. Control Flow

    3 topics
    • Conditional Statements
    • Loops
    • Functions and Pseudocode
  4. Command Line and Development Environment

    3 topics
    • Visual Studio Code and the CLI
    • Linux Commands
    • Manual Pages
  5. Arrays and Strings

    5 topics
    • Compilation Pipeline
    • Arrays
    • Strings as Arrays of Characters
    • Command-Line Arguments
    • Exit Status
  6. Cryptography

    2 topics
    • Encryption and Ciphers
    • Encoding and Decoding

CS50x: Introduction to Computer Science Programming in C flashcards โ€” 50 questions with answers

  1. What is source code?

    The human-readable set of instructions a programmer writes in a programming language such as C. It must be translated into machine code before a computer can execute it.

  2. What is machine code?

    The binary sequence of $0$s and $1$s that a computer's CPU can execute directly. It is the output of compiling source code.

  3. What is a compiler?

    A program that translates source code written in a language like C into machine code (binary) that the computer can run. CS50 uses the compiler clang.

  4. What command compiles hello.c with clang and names the output program 'hello'?

    clang -o hello hello.c โ€” the -o flag specifies the output file name. Without it, clang produces a default file named a.out.

  5. What does the command 'make hello' do in CS50?

    make is a build utility that automatically runs the compiler (clang) with the correct flags on hello.c, producing an executable named hello, so you don't have to type the full clang command yourself.

  6. What punctuation must end most statements in C, and what happens if it is missing?

    A semicolon ( ; ). Omitting it is a syntax error, and the compiler will refuse to compile the program until it is fixed.

  7. What is an escape sequence in C, and which one prints a new line?

    An escape sequence is a backslash followed by a character that represents a special character in a string. \n moves the cursor to a new line, e.g., printf("hello, world\n");

  8. What do the escape sequences \" , \\ , and \0 represent in C?

    \" prints a literal double quote inside a string; \\ prints a literal backslash; \0 is the NUL character (a byte of all zeros) that terminates strings.

  9. Which printf format specifiers are used for a string, an int, a float/double, a char, and a long?

    %s for a string, %i (or %d) for an int, %f for a float or double, %c for a char, and %li for a long.

  10. What does the line #include <stdio.h> do in a C program?

    It tells the preprocessor to copy the contents of the header file stdio.h (the standard input/output header) into the program, declaring functions like printf so the compiler recognizes them.

  11. What is a header file in C?

    A file ending in .h that contains declarations (prototypes) of functions and other symbols provided by a library, e.g., stdio.h declares printf. Including it lets your code call those library functions.

  12. What does the CS50 library (cs50.h) provide?

    Convenience functions for safely getting user input โ€” such as get_int, get_float, get_char, get_long, and get_string โ€” plus the string type. Include it with #include <cs50.h>.

  13. What is a library in programming?

    A collection of pre-written code (functions) that programmers can reuse in their own programs, accessed in C by including the library's header file and linking its compiled code.

  14. How do you declare and initialize an integer variable named counter with value 0 in C, and why must you state the type?

    int counter = 0; C is a statically (strongly) typed language, so every variable must be declared with its data type, which tells the compiler how much memory to allocate and how to interpret it.

  15. How do you declare a constant in C, and what naming convention is used?

    Use the const keyword, e.g., const int TOTAL = 10; The value can never be changed afterward, and by convention constant names are written in all uppercase letters.

  16. In C, what is the difference between = and == ?

    = is the assignment operator (stores a value in a variable); == is the equality comparison operator (checks whether two values are equal, returning true or false). Using = inside a condition is a classic bug.

  17. How many bytes does an int use in C, and what range of values can it store?

    An int uses 4 bytes ($32$ bits) and stores values from $-2^{31}$ to $2^{31}-1$, i.e., roughly $-2{,}147{,}483{,}648$ to $2{,}147{,}483{,}647$.

  18. How many bytes does a char use in C, and what does it store?

    A char uses 1 byte ($8$ bits) and stores a single character encoded as a number (its ASCII value), covering $2^{8} = 256$ possible values.

  19. Compare float and double in C.

    Both store real (floating-point) numbers. A float uses 4 bytes ($32$ bits); a double uses 8 bytes ($64$ bits), giving roughly twice the precision (more digits after the decimal point). Doubles reduce, but do not eliminate, floating-point imprecision.

  20. How many bytes does a long use in C, and why would you use it instead of int?

    A long uses 8 bytes ($64$ bits), storing values from $-2^{63}$ to $2^{63}-1$. Use it when a value might exceed the int maximum of $2^{31}-1$, at the cost of more memory.

Show the other 30 Programming in C flashcards
  1. What is the bool data type in C and what values can it hold?

    A Boolean type holding only true or false. It is not built into old C; it comes from cs50.h (or the standard header stdbool.h).

  2. What is the 'string' type used in CS50, and what is it really in standard C?

    string is a type defined by cs50.h for a sequence of characters. Standard C has no string type โ€” it is actually char *, a pointer to an array of characters ending with the NUL byte \0.

  3. What does the modulo operator % do in C? Give an example.

    It yields the remainder after integer division. Example: $52 \bmod 10 = 2$, so in C, 52 % 10 evaluates to 2. It is commonly used to check even/odd: n % 2 == 0 means n is even.

  4. What are three equivalent ways to increase an int variable counter by 1 in C?

    counter = counter + 1; counter += 1; and counter++; (similarly counter-- decreases it by 1).

  5. What does integer division do in C, and how do you get a true decimal result from dividing two ints x and y?

    Integer division truncates (discards) everything after the decimal point: 5 / 3 evaluates to 1, not $\frac{5}{3} \approx 1.67$. Cast an operand to a float/double first: (float) x / y.

  6. What are the three logical operators in C?

    && (logical AND โ€” true only if both operands are true), || (logical OR โ€” true if at least one is true), and ! (logical NOT โ€” inverts a Boolean value).

  7. What is integer overflow?

    The error that occurs when a calculation produces a value larger than the highest value the integer type can store; with a fixed number of bits there is no place for the extra carry bit, so the value wraps around (e.g., to a negative or zero value).

  8. What is the maximum value of a signed 32-bit int, and why?

    $2^{31} - 1 = 2{,}147{,}483{,}647$. Of the $32$ bits, one is used for the sign, leaving $31$ bits for the magnitude, and one value pattern is used for zero.

  9. Give a real-world consequence of integer overflow.

    The Boeing 787 had to be rebooted periodically because an internal counter of hundredths of a second overflowed after about 248 days ($2^{31}$ hundredths of a second), which could cut electrical power. The Y2K problem (storing years as 2 digits) is a related overflow-style error.

  10. What is floating-point imprecision, and why does it happen?

    The inability of computers to represent all real numbers exactly, because a float has only a finite number of bits. For example, $\frac{1}{10}$ has no exact binary representation, so 0.1 is stored as something like $0.10000000149\ldots$

  11. How can a programmer reduce floating-point imprecision in C, and can it be eliminated?

    Use double (8 bytes) instead of float (4 bytes) to gain more bits of precision. It cannot be fully eliminated โ€” any finite number of bits still cannot represent infinitely many real numbers exactly.

  12. What is the structure of an if / else if / else construct in C, and how many of its branches execute?

    if (condition) { ... } else if (condition) { ... } else { ... } โ€” conditions are checked top to bottom and exactly one branch (the first whose condition is true, or the final else) executes.

  13. Why is 'if (x > 0) ... if (x < 0) ... else ...' worse than using else if for a three-way comparison?

    Separate if statements are each evaluated unconditionally, wasting comparisons and risking logic errors (the else binds only to the nearest if). Chained if / else if / else evaluates mutually exclusive branches efficiently and correctly.

  14. What does the ternary (conditional) operator ? : do in C?

    It is a compact one-line if/else that yields one of two values: condition ? value_if_true : value_if_false. Example: int max = (a > b) ? a : b;

  15. How does a while loop work in C, and how do you write an intentional infinite loop?

    while (condition) { body } re-checks the condition before each iteration and repeats the body as long as it is true (possibly zero times). An intentional infinite loop is while (true) { ... }, exited with break or return.

  16. What are the three parts of a C for loop header, e.g., for (int i = 0; i < 3; i++)?

    1) Initialization (int i = 0) โ€” runs once at the start; 2) Condition (i < 3) โ€” checked before every iteration, loop runs while true; 3) Update (i++) โ€” runs after each iteration.

  17. How does a do-while loop differ from a while loop, and what is it commonly used for?

    A do-while executes its body first and checks the condition after, so the body always runs at least once. It is ideal for validating user input: do { n = get_int(...); } while (n < 1);

  18. What are the three parts of a C function's definition header, as in 'int add(int a, int b)'?

    1) The return type (int) โ€” the type of value the function gives back; 2) the function name (add); 3) the parameter list (int a, int b) โ€” the typed inputs (arguments) it accepts.

  19. What is a function prototype in C, and why is one needed?

    A declaration of a function's return type, name, and parameters placed before main (ending with a semicolon), e.g., void meow(int n); It is needed because C is read top to bottom โ€” the compiler must know a function exists before it is called, even if it is defined later.

  20. What does the void keyword mean in a C function signature?

    As a return type, void means the function returns no value (e.g., void meow(void)). As a parameter list, void means the function takes no arguments.

  21. What is pseudocode and why is it useful?

    An informal, human-language description of an algorithm's steps, written before actual code. It helps a programmer plan logic (conditions, loops, functions) without worrying about syntax.

  22. What is a command-line interface (CLI), and how does it differ from a GUI?

    A CLI is a text-based way to interact with a computer by typing commands into a terminal, versus a GUI (graphical user interface) with windows, icons, and mouse clicks. Programmers favor the CLI for speed and automation; VS Code includes a built-in terminal for this.

  23. In VS Code as used in CS50, what are the two main areas you work in?

    The text editor (top region) where you write source code files like hello.c, and the integrated terminal (bottom region) where you run CLI commands to compile (make) and execute (./hello) programs.

  24. What do the Linux commands ls, cd, and pwd do?

    ls lists the files and folders in the current directory; cd changes the current directory (cd .. moves up one level, cd alone returns to the home directory); pwd prints the path of the current working directory.

  25. What do the Linux commands mkdir, cp, mv, and rm do?

    mkdir creates a new directory; cp copies a file (cp source dest); mv moves or renames a file; rm removes (deletes) a file โ€” with rm -r it removes a directory and its contents recursively.

  26. What are manual pages (man pages) and how do you use them?

    Built-in Unix/Linux documentation for commands and C library functions. Run man followed by the name (e.g., man printf) to see its usage, parameters, and options; CS50 also offers simplified pages at manual.cs50.io.

  27. Name, in order, the four steps of the C compilation pipeline that 'make' hides.

    1) Preprocessing โ€” #include and other # directives are expanded (header contents copied in); 2) Compiling โ€” C source is translated to assembly code; 3) Assembling โ€” assembly is translated to machine code (object code); 4) Linking โ€” your object code is combined with the machine code of libraries (e.g., cs50, stdio) into one executable.

  28. In the compilation pipeline, what exactly happens during preprocessing and during linking?

    Preprocessing: lines beginning with #, like #include <cs50.h>, are replaced with the actual contents (prototypes) of those header files. Linking: the compiled machine code of your file is merged with the precompiled machine code of the libraries you used, producing the final single executable.

  29. What is an array in C, and how do you declare an array of 3 ints named scores? How are its elements accessed?

    An array is a sequence of values of the same type stored back-to-back (contiguously) in memory. Declare it as int scores[3]; Elements are accessed by zero-based index: scores[0], scores[1], scores[2] โ€” C does no bounds checking, so accessing scores[3] is a bug.

  30. How are strings represented in memory in C, and what is stored in argc and argv when main is declared as int main(int argc, string argv[])?

    A string is an array of chars terminated by the NUL byte \0, so "HI!" occupies 4 bytes; individual characters are just small numbers ('A' is 65 in ASCII) accessible by index like s[0], and string.h's strlen counts characters up to \0. For command-line arguments, argc (argument count) holds how many words were typed at the prompt and argv (argument vector) is an array of those strings, with argv[0] being the program's own name and argv[1] the first argument; main's return value is the program's exit status โ€” 0 for success, nonzero for error (viewable with echo $?).

CS50x: Introduction to Computer Science Programming in C FAQ

What is in the CS50x: Introduction to Computer Science Programming in C syllabus?

CS50x: Introduction to Computer Science Programming in C is split into 6 chapters: C Fundamentals, Data Types and Operators, Control Flow, Command Line and Development Environment, Arrays and Strings and Cryptography. Together they contain 21 topics.

How many chapters are in CS50x: Introduction to Computer Science Programming in C?

6 chapters, containing 21 topics. The largest chapter is Arrays and Strings, with 5 topics.

Are there flashcards for CS50x: Introduction to Computer Science Programming in C?

Yes โ€” a 50-card Programming in C deck, printed in full in the flashcards section of this page and available in the Examius app with spaced repetition. The free plan covers one exam of your choice, including its flashcards; Examius Pro, a one-time purchase, unlocks every other exam and deck.