C Flashcards
include
Library then add name
int main (void) { }
beginning of program
get_string()
- ask for a string
printf()
print string
printf(“hello,%s\n”, answer)
print variable
%c
char placeholder
%f
float, double placeholder
%i
int placeholder
%li
long placeholder
%s
string placeholder
\n
new line
return-type name (argument_list)
- make a new function
- function is referred to on top, but body is below code
- first void is type of output
- second void is type of input
- when a function has no arguments it is void
int add_two_ints (int a, int b); \function declaration
int main (void) { int x = 8; int y = 9;
printf(“%i”, add_two_ints(x,y));
}
int add_two_ints (int a, int b) { return a + b; }
int
- 4 bytes (32 bit)
~- 2 bill to ~+2 bill
unsigned int
- no negatives
- 2x positive number ~+4 bill
chars
- store single characters
- 1 byte (8 bits)
- 0 to 127
float
- real numbers (decimal)
- 4 byte (32 bits)
- precision problem
double
- real numbers (decimal)
- 8 byte (64 bits)
more precise
void
- type, not datatype
- function with void does not return value
- parameter with void does not take parameter
- placeholder meaning ‘nothing’
bool
- include library cs50
- boolean value
- true or false
create variable [type] [name]
int number;
char letter;
int height, width;
initializing
int number = 17;
char letter = “15”;
Operators
- +, -, /, *, %
- % remainder
x *= 5
x = x *5
x++
increment by 1
x–
decrease by 1
&&
- and
- only if both operand are true
||
- or
- only if operand or both are true
!
- not
- inverts the value of the operand
- true becomes false
==
- equals to
!=
- not equal to
IF
if (boolean)
{
}
- if boolean true executes {}
- false will not execute
IF ELSE
if (boolean) { } else { }
- if true will execute first {}
- if false will execute else
ELSE IF
if (boolean) { } else if (boolean_2) { } else if (boolean_3) { } else { } - mutually exclusive branches
SWITCH
int x = GetInt(); switch(x) { case 1: printf("one!\n"); break; case 2: printf("two!\n"); break; case 3: printf("three!\n"); break; }
- specific cases
- have to put break between if you want to stop
- if no break, it will fall through
? :
int x; if(bool_exp) { x=5 } else { x=6 }
instead can write
int x = (bool_exp) ?5:6;
- This is a short conditional branch
- Use only for simple branches
WHILE
while (true)
{
}
- Infinite loop
WHILE (bool_expr)
while (bool_expr)
{
}
- repeat until bool_expr becomes false
DO WHILE
do
{
}
while (bool_expr)
- execute until bool_expr false
- will run at least one time
- unknown # of times will run, but at least once
FOR
for (start; expr; increment)
{
}
ex.
for (int i = 0; i < 0, i++)
{
}
- do loop a certain number of times
1. start - set counter
2. expr - bool_expr (if true, execute loop)
3. increment - increase/decrease counter
Compiling
- preprocessing: get relevant code from the library to be used in code
- compiling: turns it into assembly code
- assembling: converts assembly to binary
- linking: brings different files together
Casting
Converts one data type to another
- char c1 = ‘H’;
- printf(“%i”, (int) c1);
72
get_int()
get integar
include string.h
string library
strlens()
string length
Array
- it is a data structure
- must be the same type
- each block is an element
- access by index #
- index starts at 0 and last is n-1 (ex. 50 elements, 1st: 0, last: 49)
type name[size];
int student_grades[40];
double menu_prices[8];
- type: variable (int char, etc.)
- name: what you are calling it
- size: how many elements
type name[size] = {}
int student_grades[4] = {92, 78, 36, 80};
or
int student_grades[] = {92, 78, 36, 80};
- fill an array
- do not need to declare the number before declaration
type name[size][index]
bool battleship[10][10]
- 10 by 10 grid
Copying an array from one variable to another variable
int foo[5]
int bar[5]
for(int j = 0; j < 5; j++)
{
bar[j] = foo[j];
}
DO NOT bar = foo because it does not copy in C
int main(int argc, string argv[]) { }
- collect data at the command line
- argc: argument count
1. how man arguments the user typed (ex. ./greed is 1 argument, and ./greedy 1024 cs50 is 3 arguments) - argv: argument vector
1. stores strings only, one string per element (ex. ./greedy 1024 cs 50 has 3 strings [0] “./greedy” [1] “1024” [2] “cs50”
Local Variable
- can only be accessed within the function (including main)
- passed by variable (1) callee recieves a copy of the variable, not the variable itself (2) variable in the caller remains unchanged unless overwritten
Global Variables
- can be accessed by any function in the program
- declare it outside of all functions
Recursion
Function that calls it self
factorial(n!)
fact(n) = n * fact(n-1)
TypeDef
Create your own data structure
typedef struct { string name; string number; } person; //name of typedef
int main (void) {
person people[4]; //type and name of array
people[0].name = "EMMA"; people[0].number = "928930"; people[1].name = "Brian"; people[1].number = "23948939";
}