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

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

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

what is a partial solution to string problems

A
  • Partial solution is to use “safe” functions
20
Q

what are safe string functions

A

strcpy() strcmp , strlen , strcat

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

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

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

25
Q

how to declare a pointer in C#

A

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

26
Q

does string exists in c#

A

no string does not exists we use char[] instead

27
Q
A