From 0a7af7311a2080f20274b5318c8aa437fb467b7e Mon Sep 17 00:00:00 2001 From: kdeng00 Date: Tue, 13 Oct 2020 20:51:41 -0400 Subject: [PATCH] Finished 2.6. Start 2.7 Type Conversions --- ch_02/exercise_02-02/main.c | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 ch_02/exercise_02-02/main.c diff --git a/ch_02/exercise_02-02/main.c b/ch_02/exercise_02-02/main.c new file mode 100644 index 0000000..ed573b5 --- /dev/null +++ b/ch_02/exercise_02-02/main.c @@ -0,0 +1,68 @@ +/* + * + * + * for (i=0; i < lim-1 && (c=getchar()) != '\n' && c ~= EOF; ++i) + * s[i] = c; + * + * Exercise 2-1. Write a loop equivalent to the for loop above without using && or || + * + * Author: Kun Deng + */ + +#include + + + +const int LIM = 10; + +int is_i_less_than_lim(int i) +{ + // Results in 1 if true and 0 if not + int result = i < LIM -1; + + // printf("is_i_less_than_lim(%d, %d) function result is %d\n", i, LIM, result); + + return result; +} + +int is_c_not_new_line(char *c) +{ + *c = getchar(); + // Results in 1 if true and 0 if not + int result = (*c != '\n'); + + // printf("is_c_not_new_line(%c) function result is %d\n", *c, result); + + return result; +} + +int is_c_end_of_file(char c) +{ + // Results in 1 if true and 0 if not + int result = c != EOF; + + // printf("is_c_end_of_file(%c) function result is %d\n", c, result); + + return result; +} + +int main() +{ + char c; + char s[LIM]; + int i; + + printf("Enter characters\n\n"); + + // The result of the functions are compared to each other. If any are not true then the loop will + // be terminated + for (i = 0; is_i_less_than_lim(i) == is_c_not_new_line(&c) == is_c_end_of_file(c); ++i) + s[i] = c; + + s[i] = '\0'; + + printf("s is %s\n", s); + + + return 0; +}