Professional Documents
Culture Documents
Table of Contents
2. In terminal window
- Compile code before running it, through a command:
clang -o hello hello.c: tell clang to use hello as the output filename and use hello.c
as the source code.
If use CS50’s library, via #include <cs50.h>, for the get_string function, add a
flag -l: clang -o hello hello.c -lcs50
- To run our program, type in another command, which is ./ + (folder name) (ex: ./hello)
3. Command line
- ls: short for “list”, this command will give you a readout of all the files and folders in current
directory
- cd <directory>
short for “change directory”, this command changes your current directory to
<directory>, which you specify, in your workspace or on your operating system
the shorthand name for the current directory is “.” and for the parent directory of the
current directory is “. .”
If ever curious about the name of the current directory, though the terminal prompt will
often tell you, you can type pwd (present working directory)
mkdir <directory>: make directory, create a new subdirectory called <directory> located
in the current directory
- cp <source> <destination>
short for “copy”, this command will allow you to create a duplicate of the file you specify
as <source>, which it will save in <destination>
cp -r: copy entire directories
- rm <file>
short for “remove”, this command will delete <file> after it asks you to confirm (y/n) you
want to delete it
rm -f: skip the confirmation
rm -r: delete entire directories
can also combine -r and -f flags into -rf
there is no undo
- mv <source> <destination>: short for “move”, this command will allow you to effectively
rename a file, moving it from <source> to <destination>
4. Printf
- printf (“hello, world\n”); \n: to get a new line
5. Data type
a. String
- Data type
- Store a series of characters – words, sentences, paragraphs and the like
- #include <cs50.h>
b. Int
- Data type
- Used for variables that will store integers
- Always take up 4 bytes of memory (32 bits)
- Unsigned int is a qualifier that can be applied to certain types (including int), which effectively
doubles the positive range of variables of that type, at the cost of disallowing any negative values
c. Long
- Integers with more bits, so they can count higher than an int
d. Float
- Data type
- To store floating-point values – real numbers
- Always take up 4 bytes of memory (32 bits)
- Data type
- To store floating- point values – real numbers
- Always take up 8 bytes of memory (64 bits)
- Double precision compared to float
f. Parity
g. Char
- Data type
- To store single character
- Always take up 1 byte of memory (8 bits)
h. Bool
- Data type
- Store a Boolean value – true/ false to compare values
- #include <cs50.h>
- In C, every nonzero value is equal to true, and zero is false
- Two main types of Boolean expressions
Logical operators:
Logical AND (&&) is true if and only if both operands are true, otherwise false
(ex: x && y is true if and only if x is true and y is true)
Logical OR (||) is true if and only if at least one operand is true, otherwise false
Logical NOT (!) inverts the value of its operand (ex: x is true so !x is false)
Relation operators
6. Void
- Type
- Function can have a void return type, which just means they don’t return a value
- The parameter list of a function can also be void. It simply means the function takes no
parameters
void cough (void) (custom function will be written below, just need first line here)
{
for (int i = 0, i < 3, i++)
{
cough ();
}
}
{
printf (“cough\n”);
}
cough (3);
}
7. Conditional statements
- If
{ {
} }
Else
{
}
- Switch statement: specify distinct case, instead of relying on Boolean expressions. It’s important
to “break ;” between each case, or you will fall through each case (unless that is desired
behaviour)
case 1: case 4:
printf (“One!\n”); printf (“Four, ”);
break;
case 3:
case 2: printf (“Three, ”);
printf (“Two!\n”);
break; case 2:
printf (“Two, ”);
case 3:
printf (“Three!\n”); case 1:
break; printf (“One, ”);
default: default:
printf (“Sorry!\n”); printf (“Blast off!\n”);
}
}
x x
3 3
Three! Three, two, one, blast off!
int x;
if (expr)
{
x = 5;
int x = (expr) ? 5 : 6; }
else
{
x = 6;
8. Arithmetic operators
- Can add (+), subtract (-), multiply (*), and divide (/)
- Modulus operator (%) – số dư
- Shortcut
x = x – 1 is the same as x - = 1 or x - -
9. Loop
- While: repeat an unknown number of times, and possibly not at all
Infinite loop
while (true)
If the Boolean-expr evaluates to true, all lines of code between the curly braces will
execute repeatedly, in order from top to bottom, until Boolean-expr evaluates to false.
while (boolean-expr)
do
while (boolean-expr)
- For: repeat a discrete number of times, though you may not know the number at the moment the
program is compiled.
Local variables: can only be accessed within the functions in which they are created
Global variables: can be accessed by any function in the program
- For the most part, local variables in C are passed by value in function calls
- When a variable is passed by value, the callee receives a copy of the passed variable, not the
variable itself.
- That means that the variable in the caller is unchanged unless overwritten.
- Things can get particularly insidious if the same variable names appear in multiple functions,
which is perfectly okay as long as the variables exist in different scopes
11. Arrays
- Arrays are a fundamental data structure
- We use arrays to hold values of the same type at contiguous memory locations.
- In C, the elements of an array are indexed starting from 0
- Array declarations: type name [size];
- When declaring and initializing an array simultaneously, there is a special syntax that may be
used to fill up the array with its starting values
- Arrays can consist of more than a single dimension (ex: bool battleship [10][10]).
- While we can treat individual elements of arrays as variables, we cannot treat entire arrays
themselves as variables.
- We cannot, for instance, assign one array to another using the assignment operator. Instead, we
must use a loop to copy over elements one at a time.
- Arrays are passed by reference. The callee receives the actual array
- argc (argument count): this integer-type variable will store number of command-line arguments
the user typed when the program was executed
- argv (argument vector): this array of strings stores, one string per element, the actual text the user
typed at the command-line when the program was executed. The first element of argv is always
found at argv[0]. And the last element is [argc – 1]
13. Struct
- If we wanted to implement a program that searches a phone book, we might want a data type for
a “person”, with their name and phone number.
- It turns out in C that we can define our own data type, or data structure, with a struct, which
could contain other data types inside it, in the following syntax:
Typedef struct
{
String name;
String number; (string if include symbols and formatting, like + or -)
}
Person;
- Without struct
Make sure that the first name in names matches the first number in numbers, and so on.
If the name at a certain index i in the names array matches, return the phone number in
the numbers array at the same index.
- With struct
Create an array of the person struct type, and name it “people”. Set the values for each
field, or variable, inside each person struct, using the dot operator, ..
In loop, more certain that the number corresponds to the name since they are from the
same person struct.
Improve the design of program with a constant (Const int NUMBER = 10;), and store
values not in code but in a separate file or even a database.