Skip to content

Commit

Permalink
More versions of printing out argv.
Browse files Browse the repository at this point in the history
  • Loading branch information
janpgit committed Apr 21, 2024
1 parent df4305c commit d6c8632
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions modules/program-arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ Some `printf()` implementations barf on a null pointer when printing via the
`%s` format string.

Code:
- #solution argv-do-while.c
- #solution argv-while.c
- #solution argv-for.c
- #solution argv-for-v2.c
- #solution argv-nodash.c
- #solution print-argv-recursively.c

Expand Down
9 changes: 9 additions & 0 deletions src/argv-do-while.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

int
main(int argc, char *argv[])
{
do {
printf("%p '%s'\n", *argv, *argv);
} while (*++argv != NULL);
}
9 changes: 9 additions & 0 deletions src/argv-for-v2.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <stdio.h>

int
main(int argc, char **argv)
{
/* Not a nice way of doing things but shown as possible. */
for ( ; *argv != NULL; printf("'%s'\n", *argv++))
;
}
10 changes: 4 additions & 6 deletions src/argv-while.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include <stdio.h>

int
main(int argc, char *argv[])
main(int argc, char **argv)
{
char **p = argv;

do {
printf("%p '%s'\n", *p, *p);
} while (*++p != NULL);
while (*argv++ != NULL) {
printf("'%s'\n", *(argv - 1));
}
}

0 comments on commit d6c8632

Please sign in to comment.