CS50 Week 1: C
On compiling C, data types and the C 'grammar'.
- π Course Website
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
.hrefer to some other set of code, like a library, that we can then use in our program. - this is how you
console.logsomething 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
boolfortrueorfalsecharsingle ASCI characters: take up 1 byte (8 bits)intnumber: always take up 4 bytes (32 bits) of memory (meaning that you can't count higher than ~ 4 billion)- unsigned
intis 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 likeshort,long(64 bits), andconstlongreally big number floatnumber with decimals, take up 4bytes (32 bits) of memory, caution: there's a limit to how precise we can be with floatsdoublenumber with many decimals, up to 8 bytes (64 bites) of memorystring(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?
voidis a type, but not a data type:- functions can have a
voidreturn type, which means they don't return a value (likeprintf) - functions with
voidas parameters simply take no parameter
Placeholders for data types permalink
%cchar (make sure to use single quotes forchar)%iint (integers are truncated floats! Everything after the period goes away.)%ffloat, double%lilong%sstring
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,elseswitchstatement- 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.β