Final Review Flashcards
%f (default _ digits after decimal point)
6
displays 2 digits after the decimal point
%.2f
char: format specifier _, size: _ (normally)
%c 1
int: format specifier _, size: _ (normally)
%d 4
double: format specifier _, size: _ (normally)
%lf 8
character constants are enclosed
in single quotes or double quotes?
single
char ch; int i; i = 'a'; // what's i? ch = 65; // A ch = ch + 1; // what's ch?
i is 97, ch is C
range of char type
-128,127
double m = 5/6; /* int / int = int */
printf(“Result of 5/6 is %f\n”, m);
Result of 5/6 is 0.000000
double n = (double)5/6; /* double / int = double */
printf(“Result of (double)5/6 is %f\n”, n);
Result of (double)5/6 is 0.833333
double o = 5.0/6; /* double / int = double */
printf(“Result of 5.0/6 is %f\n”, o);
Result of 5.0/6 is 0.833333
int p = 5.0/6; /* double / int = double but then
converted to int */
printf(“Result of 5.0/6 is %d\n”, p);
Result of 5.0/6 is 0
Memory model:
Logical address: 0 -> 2^32 - 1
Code, Static Data, Dynamic Data(Heap)->, <-Stack
Memory model: int x = 10; int y; int f(int p, int q) { int j = 5; return p * q + j; } int main() { int i = x; y = f(i, i); return 0; }
w2
Initializing arrays
int a[10]
char letters[4] = {‘a’, ‘q’, ‘e’, ‘r’};
int i = 19; int *p; int *q; *p = i; // valid? q = &i // valid?
*p = i; /*error*/ q = &i /* valid */
What's c? char *cptr; char c = 'a'; cptr = &c; *cptr = 'b';
b
What does this do? int a[3] = {1, 3, 5}; int *p; p = a; *p = 10; p[1] = 4; *(p + 2) = 3;
a[0] = 10 a[1] = 4 a[2] = 3;
Passing Arrays as Parameters:
int i[3] = {10, 9, 8};
sum(i);
int sum( ? ) ;
int i[3] = {10, 9, 8}; sum(i); int sum(int *a, int size);
Multi-dimensional arrays int a[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}}; int *x = ?; // assign a to x. x[i][j] == ?
Arrays in C are stored in row-major order
int *x = (int *)a;
x[i][j] == *(x + i * n + j)
Passing by value / reference int a = 5; int *p = malloc(sizeof(int)); *p = 5; void increment (int* x) { (*x)++; } increment(?); // pass a increment(?); // pass p
increment(&a);
increment(p);
Dynamic allocation: Draw memory model int x = 2; int a[4]; int *b; int main() { b = malloc(4 * sizeof(int)); b[0] = 10; b[1] = 20; }
w3 p13
Dangling pointers int *a = malloc(10 * sizeof(int)); ... free(a); printf("%d\n", a[0]);
Dereferencing a pointer after the memory it
refers to has been freed is called a “dangling
pointer”.
void *memcpy(void *dest, const void *src, size_t n);
DESCRIPTION
The memcpy() function copies n bytes from
memory area src to memory area dest. The memory areas
must not overlap. Use memmove(3) if the memory areas do
overlap.