// 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);
}
// 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';
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");
}
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 ++)
{
}
|
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)
|
|
+
|
Sum (add)
|
|
-
|
Subtraction
|
|
*
|
Multiplication
|
|
/
|
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
|
|
get_char("MESSAGE")
|
prompts user for a char
|
|
get_double("MESSAGE")
|
prompts user for a double
|
|
get_float("MESSAGE")
|
prompts user for a float
|
|
get_int("MESSAGE")
|
prompts user for an int
|
|
get_long("MESSAGE")
|
prompts user for an long
|
|
get_string("MESSAGE")
|
prompts user for a string
|
|
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
|
|
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 original_file copied_file
|
Copy file
|
|
ls
|
List files
|
|
mkdir name_of_the_folder
|
Create a folder
|
|
pwd
|
show the current path
|
|
rm file_name
|
Remove a file
|
|
rm -R folder
|
Remove a folder recursively
|
|
touch file_name
|
Create a file
|
|
mv original_file renamed_file
|
Rename or move a file
|
|
\n
|
New line
|
|
\r
|
Return
|
|
\t
|
Tab
|
|
\"
|
Double quote
|
|
\\
|
Backslash
|
|
%c
|
Char
|
|
%s
|
String
|
|
%d || %i
|
Int
|
|
%f
|
Double or float
|
|
%.#f
|
Float (limit the output to # decimal places)
|
|
%%
|
%
|
No comments yet.
Cheatography n. The art of mapping complex subjects into clear, instantly navigable reference guides.