C Flashcards
(54 cards)
1
Q
include
A
Library then add name
2
Q
int main (void) { }
A
beginning of program
3
Q
get_string()
A
- ask for a string
4
Q
printf()
A
print string
5
Q
printf(“hello,%s\n”, answer)
A
print variable
6
Q
%c
A
char placeholder
7
Q
%f
A
float, double placeholder
8
Q
%i
A
int placeholder
9
Q
%li
A
long placeholder
10
Q
%s
A
string placeholder
11
Q
\n
A
new line
12
Q
return-type name (argument_list)
A
- 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; }
13
Q
int
A
- 4 bytes (32 bit)
~- 2 bill to ~+2 bill
14
Q
unsigned int
A
- no negatives
- 2x positive number ~+4 bill
15
Q
chars
A
- store single characters
- 1 byte (8 bits)
- 0 to 127
16
Q
float
A
- real numbers (decimal)
- 4 byte (32 bits)
- precision problem
17
Q
double
A
- real numbers (decimal)
- 8 byte (64 bits)
more precise
18
Q
void
A
- type, not datatype
- function with void does not return value
- parameter with void does not take parameter
- placeholder meaning ‘nothing’
19
Q
bool
A
- include library cs50
- boolean value
- true or false
20
Q
create variable [type] [name]
A
int number;
char letter;
int height, width;
21
Q
initializing
A
int number = 17;
char letter = “15”;
22
Q
Operators
A
- +, -, /, *, %
- % remainder
23
Q
x *= 5
A
x = x *5
24
Q
x++
A
increment by 1
25
x--
decrease by 1
26
&&
- and
| - only if both operand are true
27
||
- or
| - only if operand or both are true
28
!
- not
- inverts the value of the operand
- true becomes false
29
==
- equals to
30
!=
- not equal to
31
IF
if (boolean)
{
}
- if boolean true executes {}
- false will not execute
32
IF ELSE
```
if (boolean)
{
}
else
{
}
```
- if true will execute first {}
- if false will execute else
33
ELSE IF
```
if (boolean)
{
}
else if (boolean_2)
{
}
else if (boolean_3)
{
}
else
{
}
- mutually exclusive branches
```
34
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
35
? :
```
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
36
WHILE
while (true)
{
}
- Infinite loop
37
WHILE (bool_expr)
while (bool_expr)
{
}
- repeat until bool_expr becomes false
38
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
39
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
40
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
41
Casting
Converts one data type to another
- char c1 = 'H';
- printf("%i", (int) c1);
72
42
get_int()
get integar
43
#include string.h
string library
44
strlens()
string length
45
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)
46
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
47
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
48
type name[size][index]
bool battleship[10][10]
- 10 by 10 grid
49
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
50
```
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"
51
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
52
Global Variables
- can be accessed by any function in the program
| - declare it outside of all functions
53
Recursion
Function that calls it self
factorial(n!)
fact(n) = n * fact(n-1)
54
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";
```
}