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.
common errors:
– forgetting to allocate memory when a pointer is
declared
– dereferencing a pointer after it’s been free’d
– losing track of a memory block without free’ing
(memory leak)!
w3
Struct
struct student {
char *name;
int age;
};
File Operations:
fopen, fread, fwrite, fprintf, fgets, fscanf, fclose.
Remember to use fclose to close each file
when done.
char *fgets(char *s, int size, FILE *stream); reads the next line from a file pointer – It reads at most _?_ characters – Reading stops after a -?- – eliminate the ‘\n’ or not? – The end of the string?
reads the next line from a file pointer – It reads at most size -1 characters – Reading stops after a newline or EOF – Does not eliminate the ‘\n’ ! – Appends a ‘\0’ character at the end of the string.
FILE* f = fopen(…);
fprintf(f, “My name is %s\n”, myname);
w3
Open a file: FILE *fopen(const char *filename, const char *mode); Close: void fclose(FILE *stream);
FILE *fp;
fp = fopen(argv[1], “r”);
“r” Open for reading
“w” Open for writing (file need not exist)
“a” Open for appending (file need not exist)
“r+” Open for reading and writing, starting at beginning
“w+” Open for reading and writing (truncate if file exists)
“a+” Open for reading and writing (append if file exists)
strchr();
strrchr();
#include char *strchr(const char *s, int c); The strchr() function returns a pointer to the first occurrence of the character c in the string s.
char *strrchr(const char *s, int c); The strrchr() function returns a pointer to the last occurrence of the character c in the string s.
Compare:
fgrets, fscanf;
fread, fwrite;
Recall that fgets, fscanf will read
characters.
• By contrast, fread and fwrite operate on
bytes.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
read nmemb * size bytes into memory at ptr
write nmemb * size bytes from ptr to the file
pointer stream
n = fwrite(&num, sizeof(num), 1, fp);
Moving around a file int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream); void rewind(FILE *stream);
• set the file position for stream • add offset bytes to the position specified by whence • whence can be – SEEK_SET - beginning of file – SEEK_CUR - current position in file – SEEK_END - end of file