| Token | Type |
|---|---|
%d / %i | int |
%u | unsigned int |
%ld / %lu | long / unsigned long |
%lld | long long |
%zu | size_t |
%f / %g | double (float promotes) |
%c | int (a character) |
%s | NUL-terminated char * |
%p | void * |
%% | A percent sign |
fprintf(stderr, "usage: %s <file>\n", argv[0]);
printf("%zu\n", sizeof x);
printf("%p\n", (void *)p);
A conversion/argument mismatch is undefined, not a type error. printf("%s", NULL) is undefined. Bounded format into a buffer: snprintf(buf, sizeof buf, fmt, …).
| Call | Rule |
|---|---|
scanf("%d", &n) | Pass the address. Check the return is 1 before using n. |
scanf("%s", buf) | Array decays; do not pass &buf. Still needs a width. |
%127s into char buf[128] | Width is usable characters; leave room for NUL. |
if (scanf("%d", &n) != 1) {
/* failed — do not use n */
}
%d stays in stdin.fgets then parse for interactive lines.int c;
while ((c = getchar()) != EOF) {
putchar(c);
}
if (fgets(line, sizeof line, stdin) == NULL) {
/* EOF or error */
}
getchar returns int. Storing it in a char can confuse a byte with EOF.fgets(buf, sizeof buf, stdin) stores a NUL. It may keep the newline. Never gets.puts(s) adds a newline; fputs(s, stdout) does not.fflush(stdin) — undefined. Drain with a loop.stderr so they survive stdout redirection.%d is not size_t or long on LP64. Match the conversion to the type.~ Good luck with your revision! ~