Skip to content

Conversation

@kkaname
Copy link

@kkaname kkaname commented Dec 9, 2025

…e relational operators.

As per asked in the excercise question, I have implemented the loop using while loop and break statement without using any relational operator(&& and ||).

Summary by CodeRabbit

  • Refactor
    • Simplified input handling for clearer, more reliable line reading
    • Ensures a consistent trailing newline at end-of-input
    • Streamlined character accumulation and output for improved readability and correctness

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

…e relational operators.

As per asked in the excercise question, I have implemented the loop using while loop and break statement without using any relational operator(&& and ||).
@coderabbitai
Copy link

coderabbitai bot commented Dec 9, 2025

Walkthrough

Replaced guarded while loop with an infinite while(1) loop in one exercise file, moved getchar() into the loop, introduced local c and i, added explicit break conditions for MAXLINE-1, newline, and EOF, and adjusted accumulation and output to print the collected line (newline on EOF).

Changes

Cohort / File(s) Summary
Loop structure refactoring
chapter_2/exercise_2_02/loop.c
Replaced prior guarded loop with while(1) and explicit breaks for MAXLINE-1, newline, and EOF. Introduced local c and i (initialized), assigned getchar() to c inside the loop, used if/else if branching to handle newline/EOF/char accumulation, and changed output to print the accumulated string followed by a newline (prints newline on EOF).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Single-file, localized change with straightforward control-flow adjustments.
  • Pay attention to:
    • Correct boundary check for MAXLINE-1 and null-termination of the buffer.
    • Proper handling of EOF vs newline to avoid off-by-one or missing characters.
    • Ensure i cannot overflow and that output formatting matches previous behavior.

Poem

I nibble bytes at break of line,
While loops now let logic shine,
EOF bows, the buffer sings,
A rabbit hops on tiny springs,
New code hops home with joy and dine 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning, 1 inconclusive)
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.
Title check ❓ Inconclusive The title is truncated and incomplete, ending with 'th…' which obscures the full intended message about implementing a loop for reading input without using relational operators. Complete the title clearly. For example: 'Implement loop for reading input into string without relational operators' to fully convey the exercise requirement.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ 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 603ddad and 54e8e54.

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

7-8: Variable declarations now follow requested style

Splitting int c; and int i = 0; onto separate lines matches the repository’s “one variable per line” convention and clears the earlier style concern. No functional issues here.


17-32: Loop correctly mirrors original for semantics without &&/||

The while (1) loop with:

  • pre-check on i >= MAXLINE - 1,
  • c = getchar() only after bounds are confirmed,
  • early breaks on '\n' and EOF,
  • and s[i++] = c; in the else branch

correctly enforces the MAXLINE - 1 limit, handles newline/EOF as intended, and keeps EOF detection safe by using int c. This is a faithful translation of the original for condition while respecting the “no &&/|| in the loop condition” requirement.

Also applies to: 35-37


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.

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: 2

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

9-13: Optional: Consider removing commented-out code.

The commented code from the previous implementation provides useful context but could be removed for cleaner code. The NOTE at lines 40-45 already explains the multiplication approach.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 396a2cd and fb49ce7.

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

15-31: Implementation successfully avoids relational operators, but needs fixes.

The while(1) loop with separate break conditions successfully avoids using && and || operators as required by the exercise. The buffer overflow protection logic (lines 16-18) is correct.

However, the critical bug on line 20 must be fixed before this implementation is correct.

Comment on lines +25 to +28
else if (c == EOF){
printf("\n");
break;
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Questionable: Extra newline printed on EOF.

Lines 25-28 print a newline when EOF is encountered, but not for other exit conditions (newline or buffer full). This creates inconsistent output behavior.

Consider whether this extra newline is necessary or if EOF should be handled like the newline case (just break).

🤖 Prompt for AI Agents
In chapter_2/exercise_2_02/loop.c around lines 25 to 28, the code prints an
extra newline when c == EOF which causes inconsistent output compared to other
exit conditions; remove the printf("\n") so EOF is handled the same as the
newline/buffer-full cases (just break), or restructure the branch to break
immediately on EOF without emitting any characters.

@kkaname
Copy link
Author

kkaname commented Dec 9, 2025

@ohkimur So i have implemented the new changes, and as per you said earlier you can merge the file.
Also i have tested the code, it runs as intened, so no issues with it.

@ohkimur
Copy link
Owner

ohkimur commented Dec 10, 2025

Awesome job @kkaname! The PR is almost ready to merge. A couple of nitpicks for the consistency of the repo. Let’s define the variables one per line explicitly as you saw in my code. Also adress the finding be @coderabbitai and we’re good to go.

NOTE: in case you want to contribute more it’ll be amazing if we enforce single line variable definition in the linting/formatting so other contributors know about this in advance.

@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

Tip

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get the current file content from the PR branch
cat chapter_2/exercise_2_02/loop.c

# Get review comments by coderabbitai on this PR
gh pr view 95 --json comments --jq '.comments[] | select(.author.login == "coderabbitai") | {author: .author.login, body: .body, createdAt: .createdAt}'

Length of output: 11766


@ohkimur Thank you for the clarification! Just to note, my previous finding was actually positive - I confirmed that the critical EOF detection issue was fixed and the code is working correctly now.

The only remaining item for consistency is the variable declaration style. Currently on line 7:

int c, i = 0;

Should be split into:

int c;
int i = 0;

This matches the repository's coding style of defining variables one per line.

@kkaname - this is a simple change needed for consistency with the rest of the codebase. Just split the variable declarations on line 7 into separate lines as shown above.

Regarding the linting/formatting note: For C projects, tools like clang-format with appropriate style configuration could enforce this automatically. You could set up a .clang-format file with AllowShortBlocksOnASingleLine: Never and similar rules, or integrate linters like cpplint into the CI pipeline to catch these style issues early.

@ohkimur
Copy link
Owner

ohkimur commented Dec 10, 2025

Awesome job @kkaname! The PR is almost ready to merge. A couple of nitpicks for the consistency of the repo. Let’s define the variables one per line explicitly as you saw in my code. Also adress the finding be @coderabbitai and we’re good to go.

NOTE: in case you want to contribute more it’ll be amazing if we enforce single line variable definition in the linting/formatting so other contributors know about this in advance.

@coderabbitai Maybe you can take care of the single line variable definition and the finding you found so we make @kkaname life easier. You can update the PR and I’ll review the changes.

@coderabbitai
Copy link

coderabbitai bot commented Dec 10, 2025

Tip

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Read the current file content to confirm the exact state
cat chapter_2/exercise_2_02/loop.c

Length of output: 1030


@ohkimur I appreciate the trust, but I'm unable to commit code directly to the repository. However, I can make this very easy for either you or @kkaname!

The change needed is simple - just one line needs to be modified:

Current code (line 7):

int c, i = 0;

Should be changed to:

int c;
int i = 0;

That's the only change needed for the variable declaration style consistency. To clarify my previous finding: I was actually confirming that the code is working correctly - the EOF handling issue has been properly fixed. So there's no functional issue to address, just this style consistency change.

Options to apply this change:

  1. @kkaname can make this quick edit and push it to the PR branch
  2. @ohkimur - you could make the edit directly through GitHub's web interface:
    • Go to the Files changed tab
    • Click the "..." menu on the file
    • Select "Edit file"
    • Make the change
    • Commit directly to the patch-1 branch

Either way, this is the final change needed before merge! 🐰

Refactor variable declarations for clarity.
@kkaname
Copy link
Author

kkaname commented Dec 10, 2025

@ohkimur Done!
Done ✅

  • Updated variable declarations to single-line
  • Addressed all coderabbitai findings

It was a great to contribute to this repository as this is my first ever contribution to public repo, 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