Show Menu
Cheatography

CS50 Cheat Sheet by

Cheat Sheet for the Harvard CS50 course

Project base

// include Standard Input and Output
#include <stdio.h>
// include CS 50 helper
#include <cs50.h>

int main(void)
{
    // Asks for the user name
    string name = get_string("What is your name? ");
    // Print the message
    printf("hello, %s.\n", name);
}

Declaring variables

// Variables declaration
// The structure to declare variables is:
// type variable_name;

int my_integer;
char my_single_char;
bool true_or_false;
string a_long_string;
float price;

// you can also declare multiple vars of the same type in one line
string first, last, full;
int total, a, b;


// assign "=" values to a variable

my_integer = 10;
my_single_char = 'A';
true_or_false = true;
a_long_string = "A really long text";
price = 1.25;
// note: When working with strings the value needs to be under double quotes "
// When working with chars the value needs to be under single quotes '

// intialization
int my_age = 30;
float product_price = 123.45;
string message = "thank you";
char letter = 'A';

Condit­ional statements

if (boolean-expression)
{
    // code if true
}

// ------

if (boolean-expression)
{
    // code if true
}
else
{
    //code if false
}

// ------
if (boolean-expression)
{
    // code if true
}
else if (another-boolean-expression)
{
    //code if first is false but second is true
}
else
{
    //code if all are false
}

//-- 
// Example ok ?: use
int x;
if (boolean-expression)
{
    x = 5;
}
else
{
    x = 10;
}
// this is a short form of the same code above
int x = boolean-expression ? 5 : 10;

//--------

int my_number = 10;
switch(my_number)
{
    case 1:
        printf("Number is 1");
        break;
    case 10:
        printf("Number is 10");
        break;
    default:
        printf("Number is invalid");
}

Loops

while (boolean-expression)
{
    // code will be executed while the boolean-expression is true
}


do
{
    // do-while will always execute at least once.
    // code will be executed while the boolean-expression is true
}
while (boolean-expression);


for (initilization ; boolean-expression ; increment)
{
    
}

for (int index = 0 ; index < 20 ; index ++)
{
    
}
 

Data Types

int
Integers - 4 bytes (32 bits)
char
Single characters - 1 byte (8 bits)
float
Real numbers - 4 bytes (32 bits)
double
Double precision for floats
void
Is a type but not a Data Type. Used in functions to say that params or returns are not required
CS50 ones
bool
Boolean value (true or false)
string
Serie of characters (words, sentences, etc)

Arithmetic Operators

+
Sum (add)
-
Subtra­ction
*
Multip­lic­ation
/
Division
%
Module, gets the remainder (15 % 7 == 1)
x++
x = x + 1
x--
x = x - 1
x += 5
x = x + 5
x *=5
x = x * 5

Boolean expres­sions

Boolean expression is used in C to compare values.
There are just two possible values for it true or false.
On boolean expres­sions, anything other than false, 0 or NULL is considered as true.

Logical operators:
&& - AND is true only if both operands are true, otherwise false
|| - OR is false only if both operands are false, otherwise true
! - NOT inverts the value of its operand.

Relational operators
== - EQUAL is true only if both operands are equal.
!= - NOT EQUAL is true if the operands are not equal.
< - LESS THAN is true if the left operand is less than the one in the right.
> - GREATER THAN is true if the left operand is greater than the one in the right.
>= - GREATER THAN OR EQUAL TO is true if the left operand is greater than or equal to the one in the right.
<= - LESS THAN OR EQUAL TO is true if the left operand is less or equal than the one in the right.
 

cs50.h Functions

get_ch­ar(­"­MES­SAG­E")
prompts user for a char
get_do­ubl­e("M­ESS­AGE­")
prompts user for a double
get_fl­oat­("ME­SSA­GE")
prompts user for a float
get_in­t("M­ESS­AGE­")
prompts user for an int
get_lo­ng(­"­MES­SAG­E")
prompts user for an long
get_st­rin­g("M­ESS­AGE­")
prompts user for a string

Truth table

 
AND &&
x
y
(x && y)
true
true
true
true
false
false
false
true
false
false
false
false
 
OR ||
x
y
(x || y)
true
true
true
true
false
true
false
true
true
false
false
false

Linux commands

make file_name
Complile the code
./file­_name
Execute a compiled file
cd folder­_name
Change directory
cd
Go back to the home folder (~)
cp origin­al_file copied­_file
Copy file
ls
List files
mkdir name_o­f_t­he_­folder
Create a folder
pwd
show the current path
rm file_name
Remove a file
rm -R folder
Remove a folder recurs­ively
touch file_name
Create a file
mv origin­al_file rename­d_file
Rename or move a file

String scapes

Format String

%c
Char
%s
String
%d || %i
Int
%f
Double or float
%.#f
Float (limit the output to # decimal places)
%%
%
   
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Algorithms (CS50) Cheat Sheet