C Flashcards

1
Q

include

A

Library then add name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
int main (void)
{
}
A

beginning of program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

get_string()

A
  • ask for a string
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

printf()

A

print string

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

printf(“hello,%s\n”, answer)

A

print variable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

%c

A

char placeholder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

%f

A

float, double placeholder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

%i

A

int placeholder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

%li

A

long placeholder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

%s

A

string placeholder

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

\n

A

new line

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

int

A
  • 4 bytes (32 bit)

~- 2 bill to ~+2 bill

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

unsigned int

A
  • no negatives

- 2x positive number ~+4 bill

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

chars

A
  • store single characters
  • 1 byte (8 bits)
  • 0 to 127
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

float

A
  • real numbers (decimal)
  • 4 byte (32 bits)
  • precision problem
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

double

A
  • real numbers (decimal)
  • 8 byte (64 bits)
    more precise
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

void

A
  • type, not datatype
  • function with void does not return value
  • parameter with void does not take parameter
  • placeholder meaning ‘nothing’
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

bool

A
  • include library cs50
  • boolean value
  • true or false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

create variable [type] [name]

A

int number;
char letter;
int height, width;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

initializing

A

int number = 17;

char letter = “15”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Operators

A
  • +, -, /, *, %

- % remainder

23
Q

x *= 5

A

x = x *5

24
Q

x++

A

increment by 1

25
Q

x–

A

decrease by 1

26
Q

&&

A
  • and

- only if both operand are true

27
Q

||

A
  • or

- only if operand or both are true

28
Q

!

A
  • not
  • inverts the value of the operand
  • true becomes false
29
Q

==

A
  • equals to
30
Q

!=

A
  • not equal to
31
Q

IF

A

if (boolean)
{
}

  • if boolean true executes {}
  • false will not execute
32
Q

IF ELSE

A
if (boolean)
{
}
else
{
}
  • if true will execute first {}
  • if false will execute else
33
Q

ELSE IF

A
if (boolean)
{
}
else if (boolean_2)
{
}
else if (boolean_3)
{
}
else
{
}
- mutually exclusive branches
34
Q

SWITCH

A
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
Q

? :

A
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
Q

WHILE

A

while (true)
{
}

  • Infinite loop
37
Q

WHILE (bool_expr)

A

while (bool_expr)
{
}

  • repeat until bool_expr becomes false
38
Q

DO WHILE

A

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
Q

FOR

A

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
Q

Compiling

A
  • 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
Q

Casting

A

Converts one data type to another

  • char c1 = ‘H’;
  • printf(“%i”, (int) c1);
    72
42
Q

get_int()

A

get integar

43
Q

include string.h

A

string library

44
Q

strlens()

A

string length

45
Q

Array

A
  • 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
Q

type name[size];

int student_grades[40];
double menu_prices[8];

A
  • type: variable (int char, etc.)
  • name: what you are calling it
  • size: how many elements
47
Q

type name[size] = {}

int student_grades[4] = {92, 78, 36, 80};
or
int student_grades[] = {92, 78, 36, 80};

A
  • fill an array

- do not need to declare the number before declaration

48
Q

type name[size][index]

bool battleship[10][10]

A
  • 10 by 10 grid
49
Q

Copying an array from one variable to another variable

A

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
Q
int main(int argc, string argv[])
{
}
A
  • 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
Q

Local Variable

A
  • 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
Q

Global Variables

A
  • can be accessed by any function in the program

- declare it outside of all functions

53
Q

Recursion

A

Function that calls it self

factorial(n!)

fact(n) = n * fact(n-1)

54
Q

TypeDef

A

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";

}