Week 4: Memory Maps / String Functions / Simple I/O Flashcards

1
Q

A char takes how many bytes

A

1 byte

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

An int takes how many bytes

A

4 bytes

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

A float takes how many bytes?

A

4 bytes

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

Do different VARIABLES have to be contiguous?

A

NO

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

What is in the memory location when a variable is declared?

A

Whatever was there before

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

How many statements does the computer see in the following:

int height = 8;

A

Two, one for declaration and another for initialization

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

What is a “scalar” variable?

A

A variable capable of of holding a single data item

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

What is an “aggregate” variable?

A

A variable capable of holding a collection of values

arrays and structures

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

What are the two kinds of aggregate variables in C?

A

Arrays and structures

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

What is an array initializer?

A

The contents of an array enclosed in curly brackets

{1,2,3,4,0,0,0}

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

What does this do?

int a[10];

A

Declares an array of type int with 10 elements

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

What does this do?

int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

A

Declares an array of type int with 10 elements AND what the elements are

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

What does this do?

int a[10] = {1, 2, 3, 4, 5, 6};

A
/* initial value of a is
 {1, 2, 3, 4, 5, 6, 0, 0, 0, 0} */

The remaining values become 0s

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

What does this do?

int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

A

If an initializer is present, the length of the array may be omitted:

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

In the case of no length given for an array, but an initializer is, what does the compiler do?

A

The compiler uses the length of the initializer

to determine how long the array is.

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

In C, arrays are stored in what?

A

C stores arrays in row-major order, with row 0 first, then row 1, and so forth.
(sort of …. )

17
Q

What is the format of a 3-dimensional array and how are the elements accessed?

A
int b[2][3][4];
/* a 2 by 3 by 4 table */

2 layers
3 rows
4 columns

18
Q

Can an array be declared with an expression as the size?

int a[n=1];

A

Yes

19
Q

What is a string in C?

A

An array of characters ending with a special NULL character:

\0

20
Q

How do you declare a string in C?

A

char d[8] = “Magic”;

21
Q

What is the String library in C?

A

include string.h

22
Q

What does strlen return with regards to a string?

A

The length of a NULL terminated string

23
Q

What does
count = strlen(d);
return if d is “Magic”

A

5, even though there are 6 values (including the NULL character)

24
Q

What does strcpy do? Format?

A

It copies a character string into another string

source should be NULL terminated
destination should have enough room

strcpy(str2, str1);

Whatever was in str2 is replaced with whatever is in str1

25
Q

What does strcat do? Format?

A

Appends a copy of str2 to the end of str1

A pointer equal to str1 is returned

strcat(str1, str2);

26
Q

Can strings be compared using equality operators?

A

Yes

27
Q

What does strcmp do? Format?

What is returned if equal? If not equal?

A

Does an ASCII comparison one char at a time until a difference is found
between two chars in the same position.

int strcmp (str1, str2);

Returns 0 if equal
Returns anything BUT 0 if not equal

Note: spaces AND different capitalizations result in a false comparison

Ex:
DOG and DOG return 0
DOG and Dog return not 0
DOG and _Dog return not 0

28
Q

What does strchr do? Format?

What is returned if found? Not found?

A

strchr search str until ch is found or NULL character is found instead.
If found, a (non-NULL) pointer to ch is returned. Otherwise, NULL is returned instead.

char * strchr (char * str, int ch);

29
Q

The printf() function needs what when a variable is put in?

A

The type

30
Q

What are the following formatting labels?

%d
%u
%ld
%lu
%f
%lf
%c
%s
A
%d = signed integer
%u = unsigned integer
%ld = long signed integer
%lu = long unsigned integer
%f = float
%lf = double float
%c = character
%s = string
31
Q

What do the following mean?

\a
\b
\n
\t
\v
\\
A
\a = audible alert
\b = backspace
\n = newline
\t = tab
\v = vertical tab
\\ = backslash
32
Q

How do you print an integer with the following:

At least 5 wide

At least 5 wide, left-justified

At least 5 wide, zero-filled

At least 5 wide, with a plus-sign

At least 5 wide, plus sign, left-aligned?

A

At least 5 wide
printf(“‘%5d’”, 10);

At least 5 wide, left-justified
printf(“’%-5d’”, 10);

At least 5 wide, zero-filled
printf(“‘%05d’”, 10);

At least 5 wide, with a plus-sign
printf(“’%+5d’”, 10);

At least 5 wide, plus sign, left-aligned?
printf(“’%-+5d’”, 10);

33
Q

How do you print a float with the following formatting:

Print one position after the
decimal

Two positions after the decimal

Eight-wide, two positions after the decimal

Eight-wide, four positions after the decimal

Eight-wide, two positions after the decimal, zero-filled

Eight-wide, two positions after the decimal, left-justified

Printing a much larger number
with that same format

A
Print one position after the
decimal
printf("'%.1f'", 10.3456);

Two positions after the decimal
printf(“’%.2f’”, 10.3456);

Eight-wide, two positions after the decimal
printf(“‘%8.2f’”, 10.3456);

Eight-wide, four positions after the decimal
printf(“‘%8.4f’”, 10.3456);

Eight-wide, two positions after the decimal, zero-filled
printf(“‘%08.2f’”, 10.3456);

Eight-wide, two positions after the decimal, left-justified
printf(“’%-8.2f’”, 10.3456);

Printing a much larger number with that same format
printf(“’%-8.2f’“,101234567.3456);

34
Q

What is puts() ?

What is its formatting?

A

Another way of printing a string (simpler)

char sentence[] = “The quick brown fox”;
puts(sentence);

35
Q

How do you left-justify vs. right justify?

A

Right justify happens automatically, to left justify, you must append a dash “-“ like:

printf(“%-s”, string);

36
Q

What is the stdio.h function for taking in input? Format?

A

scanf(“%d”, &intVariableName);

37
Q

What do %s and %ns do in scanf()?

A

%s scans up to but not including the “next” white space character

%ns scans the next n characters or up to the next white space character,
whichever comes first

38
Q

What is the difference between scanf() and gets()

A

gets() reads in a line

scanf() can read in multiple lines

39
Q

Are ampersands included in formatting a scanf() for a character array?

A

No

scanf (“%s%s%s”, s1, s2, s3);
scanf (“%2s%2s%2s”, s1, s2, s3);