Arrays, Strings, and I/O Clinic
This practice page exercises Clusters 4 and 5. Keep the concept pages open for reference on the strlen/strcpy contract and array decay.
Compile every program with gcc -Wall -Wextra -std=c11 -o prog prog.c.
Retrieval Prompts
- State when an array expression does not decay to a pointer.
- State the contract of a C string.
- State why
strncpyis not a drop-in safe replacement forstrcpy. - State why
getswas removed in C11. - State the difference between
sizeof sandstrlen(s)forchar s[] = "abc";.
Compare and Distinguish
- array
int a[10]vs pointerint *p - string literal
"hi"vschar a[] = "hi"; strncpy,strlcpy,snprintf,memcpysizeof arrayinside the function where it was declared vs inside a function that received it as a parameterscanf("%d", &n)vsfgets+sscanf
Common Mistake Check
For each, identify the error:
char name[8]; strcpy(name, "Kernighan");char *s = "hi"; s[0] = 'H';char buf[100]; sprintf(buf, "%s %s", a, b);whereaandbare user inputchar buf[100]; fgets(buf, 100, stdin); buf[strlen(buf)-1] = '\0';int n; scanf("%d", n);printf(user_input);char dst[5]; strncpy(dst, "hello world", 5);thenputs(dst);void f(int a[10]) { size_t n = sizeof a / sizeof a[0]; /* loop over n */ }
Array Decay Drill
Predict, then verify with a compiled test:
int a[5];
printf("%zu %zu %zu\n", sizeof a, sizeof &a, sizeof &a[0]);
Then write void f(int a[5]) that prints sizeof a and explain why the answer differs from outside.
C String Reimplementations
Without looking them up, write and test:
size_t my_strlen(const char *s);int my_strcpy_safe(char *dst, size_t dst_size, const char *src);returning 0 on success, -1 on overflow.int my_strcat_safe(char *dst, size_t dst_size, const char *src);- same contract.char *my_strdup(const char *s);usingmalloc(you can forward-reference Module 2 on malloc).int my_strcmp(const char *a, const char *b);.
Test each with empty strings, exact-fit strings, and oversized inputs.
I/O Drill
Build a line-oriented tool:
- Read lines from stdin with
fgets(buf, sizeof buf, stdin)wherebufischar buf[1024]. - Strip trailing
\nif present. - Parse with
sscanforstrtokdepending on the task. - Write results to
stdout, errors tostderr. - Exit 0 at EOF, non-zero on
ferror(stdin).
Do all five for a program that reads word count per line and prints the total.
Struct and Modular Build Drill
Design a Buffer module that owns a growable byte buffer.
buffer.h: opaquetypedef struct buffer Buffer;plusbuffer_create,buffer_append,buffer_data,buffer_len,buffer_destroy.buffer.c: the struct definition and implementations.main.c: read lines from stdin and collect into a Buffer until EOF, then write once viafwrite(buffer_data(b), 1, buffer_len(b), stdout).Makefile: compilemain.oandbuffer.o, link toapp, supportmake clean.
Evidence Check
This page is complete only if you can:
- write all five C-string helpers from memory with passing tests
- explain sizeof inside and outside a function for the same array
- build the Buffer module with a clean
-Wall -Wextra -std=c11run and a working Makefile