Final Review Flashcards

1
Q

%f (default _ digits after decimal point)

A

6

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

displays 2 digits after the decimal point

A

%.2f

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

char: format specifier _, size: _ (normally)

A

%c 1

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

int: format specifier _, size: _ (normally)

A

%d 4

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

double: format specifier _, size: _ (normally)

A

%lf 8

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

character constants are enclosed

in single quotes or double quotes?

A

single

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
char ch;
int i;
i = 'a'; // what's i?
ch = 65; // A
ch = ch + 1; // what's ch?
A

i is 97, ch is C

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

range of char type

A

-128,127

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

double m = 5/6; /* int / int = int */

printf(“Result of 5/6 is %f\n”, m);

A

Result of 5/6 is 0.000000

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

double n = (double)5/6; /* double / int = double */

printf(“Result of (double)5/6 is %f\n”, n);

A

Result of (double)5/6 is 0.833333

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

double o = 5.0/6; /* double / int = double */

printf(“Result of 5.0/6 is %f\n”, o);

A

Result of 5.0/6 is 0.833333

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

int p = 5.0/6; /* double / int = double but then
converted to int */
printf(“Result of 5.0/6 is %d\n”, p);

A

Result of 5.0/6 is 0

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

Memory model:

Logical address: 0 -> 2^32 - 1

A

Code, Static Data, Dynamic Data(Heap)->, <-Stack

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
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;
}
A

w2

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

Initializing arrays

A

int a[10]

char letters[4] = {‘a’, ‘q’, ‘e’, ‘r’};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q
int i = 19;
int *p;
int *q;
*p = i;  // valid?
q = &i  // valid?
A
*p = i; /*error*/
q = &i /* valid */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
What's c?
char *cptr;
char c = 'a';
cptr = &c;
*cptr = 'b';
A

b

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q
What does this do?
int a[3] = {1, 3, 5};
int *p;
p = a; 
*p = 10;
p[1] = 4;
*(p + 2) = 3;
A
a[0] = 10
a[1] = 4
a[2] = 3;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Passing Arrays as Parameters:
int i[3] = {10, 9, 8};
sum(i);
int sum( ? ) ;

A
int i[3] = {10, 9, 8};
sum(i);
int sum(int *a, int size);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
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] == ?
A

Arrays in C are stored in row-major order
int *x = (int *)a;
x[i][j] == *(x + i * n + j)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
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
A

increment(&a);

increment(p);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q
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;
}
A

w3 p13

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q
Dangling pointers
int *a = malloc(10 * sizeof(int));
...
free(a);
printf("%d\n", a[0]);
A

Dereferencing a pointer after the memory it
refers to has been freed is called a “dangling
pointer”.

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

void *memcpy(void *dest, const void *src, size_t n);

A

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.

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

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

A

w3

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

Struct

A

struct student {
char *name;
int age;
};

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

File Operations:

fopen, fread, fwrite, fprintf, fgets, fscanf, fclose.

A

Remember to use fclose to close each file

when done.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q
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?
A
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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

FILE* f = fopen(…);

fprintf(f, “My name is %s\n”, myname);

A

w3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q
Open a file:
FILE *fopen(const char *filename,
const char *mode);
Close:
void fclose(FILE *stream);
A

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)

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

strchr();

strrchr();

A
#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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Compare:
fgrets, fscanf;
fread, fwrite;

A

Recall that fgets, fscanf will read
characters.
• By contrast, fread and fwrite operate on
bytes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q
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);
A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q
Moving around a file
int fseek(FILE *stream, long
offset, int whence);
long ftell(FILE *stream);
void rewind(FILE *stream);
A
• 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

DIR *opendir(const char *filename);
struct dirent *readdir(DIR *dirp);
closedir(dp);

A

w3

36
Q

Command-Line Arguments

argc, argv

A

argc (“argument count”) is the number of
command-line arguments.
argv[0] points to the name of the program, while
argv[1] through argv[argc-1] point to the
remaining command-line arguments.
• argv[argc] is always a null pointer - a special
pointer that points to nothing.

37
Q

char **p;
for (p = &argv[1]; *p != NULL; p++)
printf(“%s\n”, *p);

A

w3

38
Q

– length is the number of non-null characters

currently present in the string

A

size is the amount of memory allocated for

storing the string

39
Q

Duplicating a string

A

strdup allocates memory automatically

char * str2 = strdup(str1);

40
Q

Concatenating strings

A

char *strncat(char *s1, const char *s2,
size_t n);
– appends the contents of string s2 to the end of s1, and
returns s1.

41
Q

Comparing strings

A
int strcmp(const char *s1,
const char *s2)
Careful: if (str1 == str2) - compares the pointers
(the addresses), NOT the string contents!!
42
Q

Pointers to Functions

A
int cube(int x) {
return x*x*x;
}
int (*f)(int);
f = cube; 
printf ("%d\n", (*f)(5));
43
Q
void map(int *nums, int size, int (*f)(int)) {
int i;
for(i = 0; i < size; i++)
nums[i] = (*f)(nums[i]);
}
A

map(a, 8, square);

44
Q
Changing the pointer
void increment (int **x) {
int * p = malloc (sizeof(int));
*p = 13; // assign a value in the location where p points to
*x = p; // make *x point to the same addr where p points
// no return, void
}

*a = 5;
increment(&a);

A

int* increment (int *x) {
int * p = malloc (sizeof(int));
*p = 13; // assign a value in the location where p points to
x = p; // now make x point to the same addr where p points
return x;
}

*a = 5;
a = increment(a);
45
Q

extern:

A
informs the compiler that 
i
is an 
int
variable, but doesn't cause it to allocate 
space.
46
Q

header

A

include “list.h”

47
Q
Makefiles
target
first prerequistite
all out of date prerequisites
all prerequisites
A

$@
$?
$^

48
Q

-c flag

A

-c flag in gcc does compilation of file

without linking.

49
Q

Compile the object and lin

A

gcc –c main.c
gcc –c list.c
gcc -o program main.o list.o

50
Q

Process:
fork()
getpid()
getppid()

A

Return:
-1: error
0: in child process
PID: in parent process, pid is child’s.

51
Q

Differences between parent and child

A

pid, ppid
pending alarms cleared for child
pending signals are cleared for child
address space

52
Q

A terminating process sends its parent a ?

signal

A

SIGCHILD

53
Q

Orphan

process:

A

is a process whose parent died

is adopted by the init process (PID 1)

54
Q

wait()

waitpid(-1, &status, 0);

A

pid_t wait(int *status)

55
Q

WIFEXITED(status)

A

if(
WIFEXITED
( status ) )
printf(“normal termination\n”);

56
Q

pid_t waitpid(pid_t pid, int *status, int options);

A

pid = -1: wait for any child
option == WNOHANG: non-blocking
option == 0: blocking

57
Q

orphan process

zombie process

A

A child process whose parent process terminates before it does
becomes an
orphan process.
A child process that terminates but is never waited on by its
parent becomes a
zombie process.

58
Q

exec()

A

On success, exec never returns

On failure, returns -1

59
Q

system call to convert a FILE object to a fd:

A

int fileno(FILE *fp)

60
Q

Assign a stream interface to a file descriptor

A

FILE *fdopen(int fd, const char *mode);

61
Q
Often we want the 
stdout
of one process to be 
connected to the 
stdin
of another process
A

Set one FD to the value of another.
returnCode = dup2(oldFD, newFD);

int fd[2], pid;
int filedes = 
open
("file1", O_RDONLY);
dup2
(filedes, fileno(stdin));
close
(filedes);
pipe
(fd);
62
Q

pipe()/dup2()
Example
p33

A
if((pid = 
fork
()) == 0) {/* child */
dup2
(fd[1], fileno(stdout));
close
(fd[0]); 
close
(fd[1]);
execl
("/usr/bin/sort", "sort", (char *) 0);
} else if(pid > 0){      /* parent */
dup2
(fd[0], fileno(stdin));
close
(fd[1]); 
close
(fd[0]);
execl
("/usr/bin/uniq", "uniq", (char *) 0);
} else { 
perror("fork"); exit(1); 
}
63
Q

popen()

simplifies the sequence of:

A

generating a pipe

forking a child process

duplicating file descriptors

passing command execution via an 
exec()
64
Q

FILE *pipeFP;

pipeFP = popen(“/usr/bin/ls *.c”, “r”)

A

“w” – write data as input (stdin) to command

“r” – read input from command’s stdout

65
Q

TCP/IP

Transmission Control Protocol

A

soucre address, dest. address
bytes, ack, port
data

66
Q

sin_addr can be set to

A

INADDR_ANY

67
Q
Create a listen socket
Create an sockaddr_in address
bind
listen
accept
A

int listenfd;
struct aockaddr_in addr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(PORT);
bind(listenfd, (struct sockaddr *)&addr, sizeof(addr);
listen(listenfd, 1);
struct sockaddr_in q;
accept(listenfd, (struct sockaddr *)&q, &len);

68
Q

Select():

A
fd_set allset, readset;
struct timeval timeout;
timeout.tv_set = 0;
timeout.tv_usec = 0;
FD_ZERO(&allset);
FD_SET(listenfd, &allset);
readset = allset;
select(maxfd+1, &readset, NULL, NULL, &timeout);
if(FD_ISSET(listenfd, &readset))
69
Q
int select(int maxfdp1,
fd_set *readset,
fd_set *writeset,
fd_set *exceptset,
const struct timeval *timeout);

struct timeval;

A

NULL, wait forever

0, test and return immediately;

70
Q

Single quotation marks, as in the preceding example, will always get you exactly what’s inside the quotation marks–any characters that might otherwise have special meaning to the shell (like the dollar sign or the backslash) are treated literally.
Use double quotation marks when you want to assign a string that contains special characters the shell should act on.
The backslash is used to escape (treat literally) a single character (such as $ or *) that might otherwise be treated as a special character by the shell.

A

howdy=’Good Morning $USER !’
echo $howdy
Good Morning $USER !

howdy=”Good Morning $USER !”
echo $howdy
Good Morning hermie !

costmsg=”Price is $5.00”
echo $costmsg
Actual result: Price is .00

$ costmsg=”Price is $5.00”
$ echo $costmsg
Actual result: Price is $5.00

71
Q

iters=”1 2 3 4”
for i in $iters; do
echo $i
done

A

for i in 1 2 3 4; do
echo $i
done

72
Q
• Double quotes inhibit wildcard
replacement only.
• Single quotes inhibit wildcard
replacement, variable substitution and
command substitution.
• Back quotes cause command
substitution.
A

$ echo “Today is date
• Today is Thu Sep 19 12:28:55 EST 2002

  • $ echo ’Today is date
  • Today is date
73
Q
if test ! -d notes
then 
echo not found
else
echo found
fi
A

if [ ! -d notes ]
then …

-z: true is empty string
str1 = str2
int1 -eq int2
-ne, -gt, lt, le
-a: and
-o: or
74
Q

If statements just check the return value of

the command.

A

test is just a command that returns a value.

75
Q

Command line arguments

A

chmod u+x myscript
$0, $1, $2
$* == $1, $2

76
Q

Positional Parameters

$0, $#, $, $@, “$”, “$@”,

A
$0 Name of the script
$# Number of positional parameters
$* Lists all positional parameters
$@ Same as $* except when in quotes
“$*” Expands to a single argument (“$1 $2 $3”)
“$@” Expands to separate arguments (“$1” “$2” “$3”)
$1 .. $9 First 9 positional parameters
${10} 10th positional parameter
77
Q

set and shift

A

set – assigns positional parameters to its arguments.
– $ set date
– $ echo “The date today is $2 $3, $6”
– The date today is May 25, 2006

78
Q

shift – change the meaning of the positional

parameters

A
#!/bin/sh
while test "$1"
do
echo $1
shift
done
$ giant2 fee fie fo fum
fee
fie
fo
fum
giant2
1
79
Q

expr

A

x=1
x=expr $x + 1
y=expr 3 \* 5

y = $((3*5))

80
Q

expr $string : $substring

A

Returns the length of matching

substring at beginning of string.

81
Q
read
#!/bin/sh
echo "Enter your name:"
read fName lName
echo "First: $fName"
echo "Last: $lName"
A
$ name
Enter your name:
Alexander Graham
Bell
First: Alexander
Last: Graham Bell
82
Q

Reading from a file

A

while read line
do
echo $line
done < $file

83
Q

You can create your own functions or

subroutines:

A
myfunc() {
arg1=$1
arg2=$2
echo $arg1 $globalvar
return 0
}
84
Q
int pthread_create(pthread_t *tid,
pthread_attr_t *attr,
void *(*func)(void*), void *arg);
A

pthread_t thread_ID; int fd, result;
fd = open(“afile”, “r”);
result = pthread_create(&thread_ID, NULL,
myThreadFcn, (void *)&fd);

void *myThreadFcn(void *p) {
tdata *mydata = (tdata *) p;
write(mydata->fd, mydata->name, 7);
close(mydata->fd);
free(mydata);
return NULL;
}
85
Q
int pthread_join(pthread_t tid,
void **status)
A

join