Lecture 2 Flashcards

User Defined Functions, Stack Frames, C Struct, and More I/O

1
Q

basic function syntax

A

return_type function_name (argument list) {
//body of function
return statement;
}
ex) int add (int a, int b) {
return a + b;
}

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

basic function with no return syntax

A

void function_name (argument list) {
//body of function
}
*no return statement because return_type is void

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

function prototype syntax

A

return_type function_name (arg list);
ex) int add(int a, int b);

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

what is a function prototype?

A

definition of the function without initializing it

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

can you call a function prototype in main( ) and then initialize it after main( )?

A

yes, you can
the function prototype defines the function (no implementation yet) but the system recognizes it from the prototype
after running through main( ), the system is able to execute the function call since the implementation happens after main( )

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

can you call a function in main( ) and then define/implement it after main()?

A

no, the system will stop at the function call because it wont recognize the function call (not defined before called) and wont know what to do for it (not implemented before or after call)

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

what is the most basic linear data structure in c?

A

c structures (aka structs)

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

struct basic syntax

A

struct name {
field 1;
field 2;
field 3;
};
ex) struct student {
unsigned int pid;
char name[50];
}

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

can you initialize the fields when you define the struct?

A

NO, cannot initialize fields within the definition of the struct (syntax)

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

how do you assign values to the fields of your struct?

A

using the “.” operator (dot)
ex) student.pid = 12;
*the dot operator goes to that field in memory and assigns the int pid to 12

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

can you use the dot operator for strings/chars?

A

NOOO

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

how do you assign values to string/char fields of your struct?

A

strcpy( struct.field, “string”);
string function, copies the string then puts into the character array at the allocated indexes for that field

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

what is the stack frame’s lifetime?

A

1) created when the function is called
- stores variables local to that
function in the stack frame
- push functions onto stack
2) destroyed when the function returns
- all local variables go out of scope
- pop functions off of the stack

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

what are the stack’s operations to add/remove functions in the stack?

A

push –> pushes functions onto the stack when called
pop –> “pops” functions off the stack after they return

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

what sequence does the stack adhere to?

A

LIFO (last in first out)

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

do push and pop remove the functions/variables from memory?

A

no, they only update the memory register
**not creating or destroying memory just changing the value stored there

17
Q

if you have a function pushed on the stack and the local variables aren’t initialized, are they equal to 0?

A

no, whatever is left in memory at those addresses in the stack will be assigned to those variables
- leftover from what was on the stack from before
**important to initialize upon creation

18
Q

if you have a function popped off the stack and the local variables aren’t initialized, are they set to 0?

A

no, there is nothing initializing the memory back to zero
**important to initialize upon creation

19
Q

where is the stack located?

A

in main memory (RAM)

20
Q

what is the stack frame created for?

A

each function call has it’s own stack frame where the variables local to that function are stored while its pushed onto the stack until it gets popped off the stack

21
Q

int foo(int a, int b) {
return a + bar(a + b);
}
int bar (int z, int w) {
int x = z + w;
z = square(z);
return x + z;
}
int square (int x) {
return x * x;
}
how many stack frames are created?

A

3 for each function called
(foo, foo calls bar, bar calls square)

22
Q

what kind of function is puts( )?

A

puts is an output function included in stdio.h extension

23
Q

what does puts( ) do?

A

function that only takes character array/strings that is formatted by coder, then prints it to the console
*similar to printf but only accepts chars
- writes one character @ a time

24
Q

what kind of function is gets()?

A

gets is an input function included in stdio.h extension

25
Q

what does gets() do?

A
  • waits for the coder/user to hit the input something into the terminal and hit the return key
  • takes the characters you put in and puts them into the character array
  • reads input (stdin) one character @ a time
26
Q

how do puts and gets differ from printf and scanf?

A
  • puts and gets only accept characters/strings
  • printf and scanf can convert to integers etc
27
Q

what if the user entered 10 when prompted by gets() and printed by puts()?

A

accepted as the string ‘10’ not the integer, prints ‘10’ as string not integer

28
Q

what should you do if you want convert a string to an integer?

A

atoi()
changes ASCII (characters) to integers
“a to i”

29
Q

what is a different way to create a struct?

A

using typedef

30
Q

what is typedef?

A

definition type, allows us to create own datatypes without using struct

31
Q

what is the implementation of typedef?

A

typedef struct {
//fields
} name;

32
Q

if you use struct only, how do you initialize that data type?

A

struct name_of_datatype variable_name;

33
Q

if you use typedef, how do you initialize that data type?

A

name_of_datatype variable_name;
**don’t have to say struct if you use typedef