From 7c1a3cf9afef705c63528ae74fe0e0f9e138d057 Mon Sep 17 00:00:00 2001 From: ayush <152896667+kkaname@users.noreply.github.com> Date: Mon, 15 Dec 2025 11:10:40 +0530 Subject: [PATCH 1/3] Correct escape sequence in printf statement it is given that we have to check what happens when we put a incorrect escape sequence like \c given in the question. Previous code had "hello, world\\c" has the printf argument which just prints 'hello world\c' instead of checking what error does it show for entering incorrect escape sequence. I have also commented the error produced as per the GCC compiler --- chapter_1/exercise_1_02/printf_argument.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter_1/exercise_1_02/printf_argument.c b/chapter_1/exercise_1_02/printf_argument.c index 4477442..8ad0136 100644 --- a/chapter_1/exercise_1_02/printf_argument.c +++ b/chapter_1/exercise_1_02/printf_argument.c @@ -1,6 +1,6 @@ #include int main(void) { - printf("hello, world\\c"); + printf("hello, world\c"); /*prints warning: unknown escape sequence '\c' */ return 0; } From a01ba79b83c1e3d5a27aaf8501747422f8cb43a5 Mon Sep 17 00:00:00 2001 From: ayush <152896667+kkaname@users.noreply.github.com> Date: Mon, 15 Dec 2025 11:18:42 +0530 Subject: [PATCH 2/3] Fix printf argument to avoid warning Removed warning comment about unknown escape sequence. --- chapter_1/exercise_1_02/printf_argument.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/chapter_1/exercise_1_02/printf_argument.c b/chapter_1/exercise_1_02/printf_argument.c index 8ad0136..cf2e130 100644 --- a/chapter_1/exercise_1_02/printf_argument.c +++ b/chapter_1/exercise_1_02/printf_argument.c @@ -1,6 +1,8 @@ #include int main(void) { - printf("hello, world\c"); /*prints warning: unknown escape sequence '\c' */ + printf("hello, world\c"); return 0; } + +// prints warning: unknown escape sequence '\c' From e4b721e3564642a453fb4a560cf42b14d1365abe Mon Sep 17 00:00:00 2001 From: Ayush Hegde Date: Mon, 15 Dec 2025 16:32:00 +0530 Subject: [PATCH 3/3] Implemented code for 32 bit for more flexibility --- chapter_2/exercise_2_08/rightrot.c | 65 +++++++++++++++--------------- 1 file changed, 32 insertions(+), 33 deletions(-) diff --git a/chapter_2/exercise_2_08/rightrot.c b/chapter_2/exercise_2_08/rightrot.c index 5e7c1b5..f0bd851 100644 --- a/chapter_2/exercise_2_08/rightrot.c +++ b/chapter_2/exercise_2_08/rightrot.c @@ -1,48 +1,47 @@ -#include +#include #include -void printbin(unsigned int x); -unsigned int rightrot(unsigned int x, unsigned int n); +/* Exercise 2.8: Write a function rightrot(x, n) that returns the value + * of the integer x rotated to the right by n bit positions. + */ -int main(void) { - // Using hexadecimal instead of binary literals (0b...) for C89/C99 - // compatibility 0xF5 = 0b11110101 - unsigned int x = 0xF5; +void to_binary(unsigned short n) { + unsigned long i = 0; + int binary[32]; + printf("0b"); - printbin(x); - printbin(rightrot(x, 5)); + if (n == 0) { + printf("0\n"); + return; + } - return 0; + while (i < sizeof(unsigned short) * CHAR_BIT) { + binary[i++] = n % 2; + n = n / 2; + } + for (int j = i - 1; j >= 0; j--) { + printf("%d", binary[j]); + } + printf("\n"); } -void printbin(unsigned int x) { - unsigned int n = sizeof(unsigned int); +unsigned short rightrot(unsigned short x, int n) { + unsigned w = sizeof(unsigned short) * CHAR_BIT; + n %= w; - printf("0b"); + return (x >> n) | (x << (w - n)); +} - int i; - for (i = n * 8 - 1; i >= 0; --i) { - (x & (unsigned int)pow(2, i)) ? putchar('1') : putchar('0'); - } +int main(void) { + unsigned short x; + x = 55; - putchar('\n'); -} + printf("x = %d -->", x); + to_binary(x); -unsigned int rightrot(unsigned int x, unsigned int n) { - unsigned int msb_1 = ~(~(unsigned)0 >> 1); - - // Using unsigned int for i to match the unsigned n parameter - // This prevents sign comparison warnings - unsigned int i; - for (i = 0; i < n; ++i) { - if (x & 1) { - x = (x >> 1) | msb_1; - } else { - x = (x >> 1); - } - } + to_binary(rightrot(x, 3)); - return x; + return 0; } // NOTE: The rightrot function rotate the entire unsigned int var and if we