Skip to content

CS50 Week 1: C

On compiling C, data types and the C 'grammar'.

Written by Eva Dee on (about a 4 minute read).

On design permalink

  • design: a qualitative, subjective measure describing how well-written your code is.
  • we strive to write no only correct code, but well-designed code
  • the style, is the aesthetic that makes your code much more readable

On compiling permalink

  • A program called a compiler will take source code as input and produce machine code as output.
  • source code (C language) -> algorithm (compiler - clang) -> machine code (binary)
  • clang -o hello hello.c -lcs50 -> make hello
  • Header files that end with .h refer to some other set of code, like a library, that we can then use in our program.
  • this is how you console.log something in C:
// a library that allows you to get user input
#include <stdio.h>

int main(void)
{
printf("hello, %s\n", answer );
printf("%i\n", x + y);
}

Data types permalink

  • bool for true or false
  • char single ASCI characters: take up 1 byte (8 bits)
  • int number: always take up 4 bytes (32 bits) of memory (meaning that you can't count higher than ~ 4 billion)
  • unsigned int is a qualifier - it doubles the positive range of values that an integer can take on (but doesn't allow for negative values). There are also other qualifiers like short, long (64 bits), and const long really big number
  • float number with decimals, take up 4bytes (32 bits) of memory, caution: there's a limit to how precise we can be with floats
  • double number with many decimals, up to 8 bytes (64 bites) of memory
  • string (must #include <cs50.h>)

What will happen on January 19, 2038, 3:14:08?? permalink

In computing, the world was created on January 1, 1970. Computers have been counting time in seconds since the. On the specified date, the seconds will overflow the 32-bit integer (after 4 billion seconds have elapsed) and the computers will start counting in negative. In short, the Year 2038 problem is caused by an insufficient number of bits chosen to represent time.

More complex types permalink

  • structs (structures)
  • typedefs (defined types)

πŸ€” How about void?

  • void is a type, but not a data type:
  • functions can have a void return type, which means they don't return a value (like printf)
  • functions with void as parameters simply take no parameter

Placeholders for data types permalink

  • %c char (make sure to use single quotes for char)
  • %i int (integers are truncated floats! Everything after the period goes away.)
  • %f float, double
  • %li long
  • %s string

Casting permalink

  • you can cast or typecast one data type into another:
// converting one of the ints is enough
float z = float(x) / y;

Operators permalink

  • arithmetic operators
  • boolean expressions (logical operators: &&, ||, !, relational operators: >, >=, <, <=, ==, !=)

Conditional statements permalink

  • if, else if, else
  • switch statement
  • ternary operators

Loops permalink

  • while, do while, for

Misc permalink

  • type of output -> void main(void) <- type of input
  • All variables (when you declare them) need 2 things: type and name
  • declaration vs assignment vs initialization (declaring + assigning at the same time)
  • for a custom function, the convention in C is to write the function prototype, then use the function, then add the function definition at the bottom. This is the function prototype:
void meow(void);

Command line permalink

  • ls, cd, pwd, mkdir, cp, rm, mv

πŸ€” What is a greedy algorithm?

  • A greedy algorithm is one β€œthat always takes the best immediate, or local, solution while finding an answer. Greedy algorithms find the overall, or globally, an optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.”