From 62724ef6bbd66bd78615463898ac14f5174fe3c8 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Tue, 12 Apr 2016 12:01:27 -0400 Subject: [PATCH] ex9 extra credit --- lcthw-remnants-2/.gitignore | 1 + lcthw-remnants-2/ex9-ec.c | 55 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 lcthw-remnants-2/ex9-ec.c diff --git a/lcthw-remnants-2/.gitignore b/lcthw-remnants-2/.gitignore index f65443b..7564254 100644 --- a/lcthw-remnants-2/.gitignore +++ b/lcthw-remnants-2/.gitignore @@ -6,3 +6,4 @@ ex6 ex7 ex8 ex9 +ex9-ec diff --git a/lcthw-remnants-2/ex9-ec.c b/lcthw-remnants-2/ex9-ec.c new file mode 100644 index 0000000..7376ba7 --- /dev/null +++ b/lcthw-remnants-2/ex9-ec.c @@ -0,0 +1,55 @@ +#include + +int main(int argc, char *argv[]) +{ + int numbers[4] = {0}; + char name[4] = {'a'}; + + // first, print them out raw + printf("numbers: %d %d %d %d\n", + numbers[0], numbers[1], + numbers[2], numbers[3]); + + printf("name each: %c %c %c %c\n", + name[0], name[1], + name[2], name[3]); + + printf("name: %s\n", name); + + // setup the numbers + numbers[0] = 1; + numbers[1] = 2; + numbers[2] = 3; + numbers[3] = 4; + + // setup the name + name[0] = 'Z'; + name[1] = 'e'; + name[2] = 'd'; + name[3] = '\0'; + + // then print them out initialized + printf("numbers: %d %d %d %d\n", + numbers[0], numbers[1], + numbers[2], numbers[3]); + + printf("name each: %c %c %c %c\n", + name[0], name[1], + name[2], name[3]); + + // print the name like a string + printf("name: %s\n", name); + + // another way to use name; + char *another = "Zed"; + + printf("another: %s\n", another); + printf("another each: %c %c %c %c\n", + another[0], another[1], + another[2], another[3]); + + printf("name length: %ld\n", sizeof(name)); + printf("name as int: %d\n", (int)(*name)); + + return 0; +}