Skip to content

Conversation

@kkaname
Copy link
Contributor

@kkaname kkaname commented Dec 10, 2025

I have implemented the code without using any relational operators(&& and ||) and using only while loop and break statement.

Summary by CodeRabbit

  • Bug Fixes

    • Improved input handling to reliably stop on newline/EOF and prevent buffer overruns for long lines.
    • Fixed output to always include a trailing newline.
  • Refactor

    • Internal loop logic simplified for clearer, more predictable input processing.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

Walkthrough

Refactors the line-reading loop in chapter_2/exercise_2_02/loop.c: moves int c and int i = 0 outside the loop, replaces a guarded while with while (1) and break-based termination, relocates getchar() inside the loop, adjusts newline/EOF handling, and changes final printf to print a trailing newline.

Changes

Cohort / File(s) Summary
Loop Control Flow Refactoring
chapter_2/exercise_2_02/loop.c
Declares c and i before the main loop; replaces the previous guarded while with while (1) and explicit break conditions; moves getchar() into the loop body; breaks on newline or EOF (printing a trailing newline on EOF path), appends other characters to s while enforcing i < MAXLINE-1; ensures s[i] = '\0' and uses printf("%s\n", s); for output.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Verify EOF vs newline handling and that printing a newline on EOF matches intended behavior.
  • Confirm buffer bounds check i < MAXLINE-1 prevents overflow in all paths.
  • Check that s[i] = '\0' and printf("%s\n", s); produce the expected output for empty and non-empty inputs.

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: implementing code using a while loop without relational operators, which matches the raw summary's description of replacing a guarded loop with while (1) and explicit break-based termination.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cfd1076 and fab0bb2.

📒 Files selected for processing (1)
  • chapter_2/exercise_2_02/loop.c (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • chapter_2/exercise_2_02/loop.c

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@kkaname
Copy link
Contributor Author

kkaname commented Dec 10, 2025

@ohkimur I am really sorry for the hassle, cause this is my first PR and i am trying to learn.
so please find it to excuse me for this hassle.
But I dont get why the bot is showing an error this time since its the same code.

@ohkimur
Copy link
Owner

ohkimur commented Dec 10, 2025

@coderabbitai generate

@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

@ohkimur: I'll review the changes in this PR now.

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@ohkimur
Copy link
Owner

ohkimur commented Dec 10, 2025

@coderabbitai what's up with this finding? It seems to me irrelevant to this particular context.

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.

@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

Tip

For best results, initiate chat on the files or code changes.

@ohkimur: You're absolutely right! The docstring coverage check is irrelevant for this C codebase.

Docstring coverage is primarily designed for Python (and some other languages like Java/Kotlin for KDoc) where functions have structured documentation strings. C doesn't have docstrings—it uses regular comments instead. This check shouldn't be running on your C repository.

To resolve this:

You can disable the docstring coverage check for this repository by configuring CodeRabbit's settings. Here are your options:

  1. Via the CodeRabbit UI: Go to your repository settings in the CodeRabbit dashboard and disable the "Docstring Coverage" check.

  2. Via .coderabbit.yaml configuration: Add this to your repository's .coderabbit.yaml file:

reviews:
  tools:
    biome:
      enabled: false
    ruff:
      enabled: false

Since this is a C repository, you might also want to configure path-based instructions or disable certain automated checks that are language-specific and don't apply to C.

Would you like me to help you configure the appropriate settings for this repository?

@ohkimur
Copy link
Owner

ohkimur commented Dec 10, 2025

@coderabbitai done! ✅

@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

Tip

For best results, initiate chat on the files or code changes.

@ohkimur: Great! 🎉 The docstring coverage check should no longer appear for this C repository. Thanks for taking care of that!

If you have any other questions or need help with anything else related to this PR or CodeRabbit configuration, feel free to ask.


If you found this review helpful, would you consider giving us a shout-out on X?

Thank you for using CodeRabbit!

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
chapter_2/exercise_2_02/loop.c (1)

21-35: Correct implementation that meets the exercise requirements.

The logic correctly replicates the original for loop's behavior without using && or || operators. The implementation properly:

  • Checks buffer bounds before reading
  • Handles newline and EOF termination
  • Maintains buffer safety

Optional simplification: Since each condition breaks anyway, the else if and else can be flattened:

     c = getchar();
     
     if (c == '\n') {
         break;
-    } else if (c == EOF) {
+    }
+    if (c == EOF) {
         break;
-    } else {
-        s[i++] = c;
     }
+    s[i++] = c;
 }
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c8cbf8a and cfd1076.

📒 Files selected for processing (1)
  • chapter_2/exercise_2_02/loop.c (1 hunks)
🔇 Additional comments (4)
chapter_2/exercise_2_02/loop.c (4)

5-14: Excellent documentation of the exercise requirements.

The comment clearly explains the K&R exercise and provides helpful context about why certain approaches (like using * instead of &&) would be unsafe due to lack of short-circuit evaluation.


18-19: LGTM!

Variable declarations are correctly placed outside the loop for proper scope and reuse.


37-37: LGTM!

String is properly null-terminated within buffer bounds.


39-39: LGTM!

Output statement correctly prints the null-terminated string.

- Added comments to clarify the approach and potential pitfalls of using multiplication instead of logical operators.
@ohkimur
Copy link
Owner

ohkimur commented Dec 10, 2025

@kkaname Thanks a lot for your contribution! I've added some changes myself to the code so it's a bit more explicit now. Also, I fixed the double line printing. Feel welcome to contribute more in the future with other solutions.

NOTE: Considering that both you and I forgot to run make format, I'll see if we can with minimal hustle add a commit hook so everybody that contribute in the future automatically will run make format and make lint before committing. 😅

@ohkimur ohkimur merged commit 05f1382 into ohkimur:main Dec 10, 2025
2 checks passed
@kkaname
Copy link
Contributor Author

kkaname commented Dec 10, 2025

@ohkimur Hope ill be of more use and try to contribute more and in meaningfull way. Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants