Cs50 Flashcards
Cs50 library
Include < Cs50.h>
General C library
include
standard io(input/outputs functions)
Int
What is it?
How many bytes /bits of memory?
C data type/variable;
Used for variables that will store
4 Bytes/32 bits
Range from -2^31 to 2^31 minus 1 because we need as spot for 0
How can you double the value that an integer or other data type can take on?
Use “unsigned” qualifier;
Ex.) unsigned int → useful when you need a larger range and don’t need negative numbers;
Rangefor unsigned int = 0 to 2^32-1
Char
1 byte 18 bits; a single character;
Range from -2^7 to 2^7-1 (-128 to 127)
ASCII has single characters, or chars, mapped to numbers
Ex A =65; a=97
Floating point numbers (float)
Real numbers with decimals.floats have a precision problem because with only 32 bits
Double
Data type like a float but fit 8 bytes so double precision
What is type, but not a data type?
void
Can’t create a data type of “void” and assign a value to it
But it can be a return type in a function.
Printf→ doesn’t return a value
Bool
data type;only capable of 2 values true or false.
In C, every non-0 number is true, 0 is false
String
data type;
Text→ vanables that will store a series of characters (words,sentences, paragraphs, etc.)
Struts/typedefs
Structures and defined types
What 2 things do you need to create a variable?
① give it a type
② give it a name
int number;
char letter;
How do you create multiple variables at once for a given type?
int height, width;
float sqrt2,sqrt3, pi;
Initialization
When you declare a variable and assign it @ the same time.
int number =17;
Arithmetic operators
Add +
Subtract -
Multiply *
Divide /
Modulus operator %
Gives us the remainder
Ex. int m= 13 % 4 // m is now 1
Random number generator →
Equivalent syntax for
x = x * 5;
X *= 5;
X++;
X–;
Increase or decrease variable by 1
Logical operator
AND (&&)
x && y ;
Boolean expression where both operands are true, otherwise false;
OR (| |)
True if and only it @ least one of the operands is true
x || y
X can be true, Y can be true, X and y can be true, but both can’t be false
NOT (!) → inverts value of its operand, also called bang
If x is true, !x is false
If x is false, !x is true
Relational operators
x < y X <= y X > y X >= y X == y equal X !=y not equal
Conditional expressions
if else
If (boolean-expression)
{
}
else (boolean-expression)
{
}
How can you write a simple if else conditional on one line?
int x = (expr) ? 5 : 6;
Here, if expression is true, assign value of 5, else assign it a value of 6
Switch conditional
Use discrete cases to make decisions
While Loops
While (true)
{
}
This is a forever loop.
While (boolean-expression)
{
}
Do while loops
do
{
}
While (boolean-expression);
Do-white is guaranteed to run @ least once.
For loop
Used to repeat something a specific number of times
for (int i = ; i < 10; i++)
Linux command line
ls → list cd→ change directory pwd → present working directory mkdir → make directory cp → copy from a source to a destination rm mv → mv from a source to a destination