Week 4: Memory Maps / String Functions / Simple I/O Flashcards
A char takes how many bytes
1 byte
An int takes how many bytes
4 bytes
A float takes how many bytes?
4 bytes
Do different VARIABLES have to be contiguous?
NO
What is in the memory location when a variable is declared?
Whatever was there before
How many statements does the computer see in the following:
int height = 8;
Two, one for declaration and another for initialization
What is a “scalar” variable?
A variable capable of of holding a single data item
What is an “aggregate” variable?
A variable capable of holding a collection of values
arrays and structures
What are the two kinds of aggregate variables in C?
Arrays and structures
What is an array initializer?
The contents of an array enclosed in curly brackets
{1,2,3,4,0,0,0}
What does this do?
int a[10];
Declares an array of type int with 10 elements
What does this do?
int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
Declares an array of type int with 10 elements AND what the elements are
What does this do?
int a[10] = {1, 2, 3, 4, 5, 6};
/* initial value of a is {1, 2, 3, 4, 5, 6, 0, 0, 0, 0} */
The remaining values become 0s
What does this do?
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
If an initializer is present, the length of the array may be omitted:
In the case of no length given for an array, but an initializer is, what does the compiler do?
The compiler uses the length of the initializer
to determine how long the array is.
In C, arrays are stored in what?
C stores arrays in row-major order, with row 0 first, then row 1, and so forth.
(sort of …. )
What is the format of a 3-dimensional array and how are the elements accessed?
int b[2][3][4]; /* a 2 by 3 by 4 table */
2 layers
3 rows
4 columns
Can an array be declared with an expression as the size?
int a[n=1];
Yes
What is a string in C?
An array of characters ending with a special NULL character:
\0
How do you declare a string in C?
char d[8] = “Magic”;
What is the String library in C?
include string.h
What does strlen return with regards to a string?
The length of a NULL terminated string
What does
count = strlen(d);
return if d is “Magic”
5, even though there are 6 values (including the NULL character)
What does strcpy do? Format?
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
What does strcat do? Format?
Appends a copy of str2 to the end of str1
A pointer equal to str1 is returned
strcat(str1, str2);
Can strings be compared using equality operators?
Yes
What does strcmp do? Format?
What is returned if equal? If not equal?
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
What does strchr do? Format?
What is returned if found? Not found?
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);
The printf() function needs what when a variable is put in?
The type
What are the following formatting labels?
%d %u %ld %lu %f %lf %c %s
%d = signed integer %u = unsigned integer %ld = long signed integer %lu = long unsigned integer %f = float %lf = double float %c = character %s = string
What do the following mean?
\a \b \n \t \v \\
\a = audible alert \b = backspace \n = newline \t = tab \v = vertical tab \\ = backslash
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?
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);
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
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);
What is puts() ?
What is its formatting?
Another way of printing a string (simpler)
char sentence[] = “The quick brown fox”;
puts(sentence);
How do you left-justify vs. right justify?
Right justify happens automatically, to left justify, you must append a dash “-“ like:
printf(“%-s”, string);
What is the stdio.h function for taking in input? Format?
scanf(“%d”, &intVariableName);
What do %s and %ns do in scanf()?
%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
What is the difference between scanf() and gets()
gets() reads in a line
scanf() can read in multiple lines
Are ampersands included in formatting a scanf() for a character array?
No
scanf (“%s%s%s”, s1, s2, s3);
scanf (“%2s%2s%2s”, s1, s2, s3);