Chapter 1
a
#include
#include
#include
int main() {
write(1, “Hi! My name is “, strlen(“Hi! My name is “));
return 0;
}
b
#include
#include
void outputTriangle(int n) {
int i, j;
for(i = 1; i <= n; i++ ) {
for(j = 1; j <=i ; j++) {
write(2, "*", 1);
}
printf("\n");
}
}
int main() {
outputTriangle(3);
return 0;
}
c
/*Write your C code here*/
#include
#include
#include
#include
#include
int main() {
mode_t mode = S_IRUSR | S_IWUSR;
int fHandle = open(“test.txt”, O_CREAT | O_TRUNC | O_RDWR, mode);
write(fHandle, “Hi! My name is “, 16);
return 0;
}
d
/*Write your C code here*/
#include
#include
#include
#include
#include
int main() {
mode_t mode = S_IRUSR | S_IWUSR;
int fHandle = open(“test.txt”, O_CREAT | O_TRUNC | O_RDWR, mode);
printf(“Hi! My name is “);
return 0;
}
/*
1 write is a system call, printf is a C library function used to output data in specified format
2 write needs to specify length of data to be output, printf does not
3 With write, we can specify the output stream to be either standard output, standard error or a
file. printf output to standard output stream by default
*/
Chapter 2
Usually
a
1) 8 bits in a byte
2) 1 byte in a char
3) 4, 8, 4, 4, 8
b
1 0x7fbd9d50
2 *(data+3)
c
1 because “hello” is a constant string, it’s in a memory space where data is readonly
2 12
3 5
4 X = “ab”
5 Y = int * 2 (since int can be 2 or 4 bytes)
Chapter 3
a
1
sizeof(argv)/sizeof(char*)
argc
2
argv[0] is program file name
b
an array of pointers defined externally called environ
c
sizeof(ptr) = 8 because it is calculating size of a char pointer
sizeof(array) = 6 because it is calculating size of array(how many elements in the array)
d
stack
Chapter 4
a
1 heap, use heap allocation functions and keywords like malloc and new
2 free
b
1 the memory size requesting is too large
2 time() returns the seconds since 1970
ctime() returns current time in humanreadable form
3 it frees the same pointer second time
4 it uses the pointer after it has been freed
5 for the first mistake, make sure there is same number free() as number of malloc()
for the second mistake, after freeing the pointer, set it to NULL immediately
c
/*Write your C code here*/
#include
#include
#include
#include
typedef struct Person *PtrPerson;
typedef struct Person person;
struct Person {
char name[20];
int age;
PtrPerson friends[20];
};
void outputFriends(person p) {
puts((p.friends[0])>name);
}
int main() {
person one;
person two;
strcpy(one.name, “Agent Smith”);
one.age = 128;
one.friends[0] = &two;
strcpy(two.name, “Sonny Moore”);
two.age = 256;
two.friends[0] = &one;
outputFriends(one);
outputFriends(two);
return 0;
}
d
PtrPerson create(char * name, int age) {
PtrPerson ptr = (PtrPerson) malloc(sizeof(person));
strcpy(ptr>name, name);
ptr>age = age;
return ptr;
}
void destroy(PtrPerson p) {
int i;
for(i = 0; i < 20; i++) {
if (p>friends[i] != NULL) {
free(p>friends[i]);
}
}
free(p);
}
Chapter 5
a
1 gets, puts
2 it doesn’t check for the size of input, which may cause buffer overflow
b
1
#include
#include
#include
int main() {
char str1[20], str2[20];
int num;
sscanf(“Hello 5 World”, “%s %d %s”, str1, &num, str2);
printf(“%s %d %s”, str1, num, str2);
return 0;
}
c
1
a buffer
a integer to store length of data that’s read in
2
#define _GNU_SOURCE
#include
#include
int main() {
FILE * fHandle = fopen(“test.txt”, “r”);
int read;
size_t len = 0;
char *line = NULL;
if(fHandle != NULL) {
while((read = getline(&line, &len, fHandle)) != 1) {
printf(“%s”, line);
}
}
return 0;
}
Chapter 6
1 g
2 since make will only compile source files that have been modified, changing the gcc compile
options alone and execute make will not trigger a new build.
3 tab
4 heap memory is allocated dynamically, stack memory is allocated statically
scope of heap memory is between allocation and free, scope of stack memory is within the
function
heap memory is usually larger than stack memory
5 text segment for storing code, uninitialized and initialized data segment for storing things like
global variables and constants