week 2 c programming code Flashcards

to learn more about c programming

1
Q

what is a string in c#

A

string of characters is a 1D array of characters

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

how much memory does ASCII store

A

(1 byte) of each character element is stored in
consecutive memory locations

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

what is string terminated by

A

String is terminated by the null character ‘\0’ (ASCII value 0)

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

what is the length of the null string

A

The null string (length zero) is the null character only

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

what is use to take input from user in c#

A

scanf()

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

what is used to output info to user

A

printf()

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

what is the format for string in c

A

%s

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

what is the 4 safe string functions

A

strcpy()
strcat()
strlen()
strcmp

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

what is strcpy()

A

copies the string from one destination to another

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

what do you need to make sure that you do in strcpy()

A

need to allocate memory to the destination where you want to put your string
e.g
char[10] destination; -> where i will put the string in;

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

what does strlen do

A

gets the length of the string;

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

does strlen take into account the null value

A

no

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

what does strcat do

A

concatenates 2 strings together

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

in strcat(str1,str2) which string is modified and which stays the same

A

str2 stays the same and str1 changes as str2 is appended to teh end of str1

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

what does strcmp do

A

compares 2 strings and see if they are the same

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

if strcmp conpares 2 strings and finds them to be equal then what does it return

A

it returns 0

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

if strcmp conpares 2 strings and finds them to not be equal then what does it return

A

returns the numeric difference between the first
non matching characters
or -1 i need to check

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

what are string problems

A

Need to ensure that sufficient memory is allocated at run-
time for storing the string - not automatically done by the
compiler

  • No checks at run-time (and compile-time), hence common
    source of errors
  • Such “buffer overflows” have been exploited to achieve
    execution of code supplied by attackers
  • Partial solution is to use “safe” functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

what is a partial solution to string problems

A
  • Partial solution is to use “safe” functions
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

what are safe string functions

A

strcpy() strcmp , strlen , strcat

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

what is wrong with this
strncpy(char *dst, char * src, size_t num)

A

Copies the first num characters of src to dst. If the end of src
(signaled by a null char) is found before num characters have been
copied, dst is padded with zeros until a total of num characters
have been written to it.

No null-character is implicitly appended at the end of dst if src is
longer than num. Thus, in this case, dst shall not be considered a
null terminated C string

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

what is the definition of a pointer

A

A pointer is a variable that contains the address of a
variable
pointer is also stored in memory

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

what is the unary operator*

A
  • is also calld indirection or dereferencing operator
    applied to a pointer to accesses the object the pointer points to
    e.g c = 5 , *p = &c , d = p now d = 5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what is unary operator &

A

‘address-of’ operator
points to address of object
p =&c
now p now has address of c so p points to c

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

how to declare a pointer in C#

A

T *p
T is any data type like int or char

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

does string exists in c#

A

no string does not exists we use char[] instead

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

do & and * have a higher precedence then arithmetic operations

A

yes

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

use of pointers in expressions (1)

A
  • Pointer variables can appear in expressions
  • If ‘p’ points to the object ‘x’, then *p can occur in any context
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Use of pointer in expressions (3)

A

A pointer variable can be assigned to another pointer variable of the same type

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

scale factors in point expressions

A

If p is a pointer then p++ or p+1
➢ points to the next object of the same type. So,
➢ value of p increments by 4 when p is an int pointer
➢ value of p increments by 4 when p is a float pointer
➢ value of p increments by 1 when p is a char pointe

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

what is the syntax to finding out the scale factor in C#

A

sizeof(data_type)

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

what is the scale factor in C

A

number of bytes used by a data-type

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

what can i use to find scale factor in C

34
Q

how many bytes in
int
char
float
*int
*char
*float

A

int - 4
char - 1
float - 4
*int - 8
*char - 8
*float -8

35
Q

what is an array in C

A

sequential collection of elements of the same type

36
Q

how does pointer work in an array in C

A

int a[10];
int *p = &a[0];
p is a pointer to the first element of array a
int b = *p;
will copy value of a[0] into b

37
Q

accessing array elements using pointers

A

If p points to the first element a[0], then
* p+1 points to a[1]
* p+i points to a[i]
So, *(p+i) refers to the content of a[i]

38
Q

what does *(p + i) refer to

A

refers to the content of a[i] in array

39
Q

write for loop to compute sum of array of integers using pointers

A

int *p = &a[0];
int sum=0, i;
for(i=0; i<5; i++)
sum = sum + *(p+i);

40
Q

what is array - name

A

synonym for the location of the initial element

41
Q

what can int *p = &a[0]; also be written as using array name

A

int *p = a;

42
Q

write for loop for sum of array of integers using array name and pointers

A

int sum = 0;
for (int i = 0 ; i < a.length ; i ++) {
sum = sum + *(a + i);
}

43
Q

What is the memory layout of a C program

A

1) Text or Code segment
2) Data segment
3) stack segment
4) heap segment

44
Q

what is the Text / code segment

A

Text segment contains the the program
i.e. the executable instructions

45
Q

what is the data segment

A

Two data segments contain initialized and
uninitialized global and static variables
respectively

46
Q

what is the stack segment

A

Stack segment is used to store all local or
automatic variables. When we pass
arguments to a function, they are kept in
stack

47
Q

what is the heap segment

A

Heap segment is used to store dynamically
allocated variables are stored

48
Q

what is a static variable

A
  • Static variables are stored in the data segment, not in the stack.
  • The data segment is active during the entire life-time of program
  • So, static variables preserve their values even after they are out of
    their scope.
49
Q

what is a global variable

A

A global variable is declared outside all functions.
* It can be read or updated by all functions.
Careful: Do not name any local var in the name of a global var

50
Q

why is using an array not good for a large volume of data

A

insufficient
wasteful
tiny computers can not hold large amounts of data

51
Q

what is the solution for dynamci memory allocation

A

allocate memory at runtime as per demand

52
Q

why is allocating memory at runtime as per demand a good solution

A

optimal usage of memory depending on current case of applications

53
Q

how can we acces memory aloocation in C

54
Q

what syntax can we use for memory allocation in C

55
Q

what is malloc(c)

A

allocates requested amount of bytes of contiguous memory from heap
returns a pointer of first byte in allocated space or returns null pointer if memory fails

56
Q

what is free(p)

A

releases dynamically allocated blocks of memory
block of memory returns to heap

57
Q

what is something you make sure you do with free() when doing a function

A

use free() before finishing requests
all malloc() need to use free()

58
Q

how to avoid memory leaks in C

A

always use a free() after you have used a malloc()

59
Q

what happens when there is a memory leak

A

blocks of memory not returned to heap so program grows larger over time

60
Q

what are the consequences of a memory leak in C

A
  • leaks slow down system preformance as they reduce the amount of available memory
  • modern applications ystems -> releases memory used by applications when application terminates
  • memory leaks causes serious issues on resource constrained embedded devices
61
Q

what happens when free() is called twice for the same address and what is the consequence.

A

if called twice for same address program’s memory management becomes corrupt
con: prog malfunctions including security vuneralbilities

62
Q

what is a linked list in C

A
  • collection of data elements called nodes,
    -> search nodes points to next nodes in a list
    -> not stored in contiguous locations like arrays
    -> linked using pointers
63
Q

implementations of singly linked list in C

A

1) append elements at end of the list
2) search for elements in list
3) print , gives start of list
4) free memory is occupied by list

64
Q

what do nodes consist of in C

A

data
reference to the next node ( pointer -> )

65
Q

what data type is node in C

A

composite data type

66
Q

what is Null in a linked list

A

end of a list

67
Q

instead of class , what do we use in c

A

we use structure in C

68
Q

what is Structure in C

A

User defined composite data type in C
Use to deinfed items of possibly different data type into one single data type
no member functions

69
Q

what is syntax of structure

A

struct{
T a;
T b;
}

70
Q

can you write struct{} in code

A

struct{
int x;
int y;
}
int main() {
struct point p1;
}

71
Q

what is an alternative version to using struct{}

A

typedef struct var{
T a;
T b;
} v;
v -> variable name that we call in other functions

72
Q

how are structure members accessed in different functions

A

using the dot (.) operator
e.g
Point p1;
p1.x = 1;

73
Q

give example of using typedef for co - ordinates x and Y

A

typedef struct Point {
int x;
int y;
} point;

74
Q

how are pointers created in structure members

A

using pointer ( -> ) operator

75
Q

what is “pass by value”

A

pass data objects to functions as arguments

76
Q

what is “pass by reference “

A

pass pointers to a function as arguments (no side effects)

77
Q

consequences of using pass - by - value for functions

A

function gets a local copy of variable
fumction only happens within the function and will not be printed out in the main or another function

78
Q

consequences of using pass - by - reference for functions

A

function gets a local copy of pointer ehich has address of variable
pointer update so you get results of the function thst was executed
memory locstion is where the variable is stored
both function and main can see the result of the variable

79
Q

in a function can pointers be returned

A

yes they can pointers of the same data type can be returned

80
Q

what one thing you should not return pointers to

A

do not return pointers to local var
do not return address of a variable too