Skip to content

Conversation

@kkaname
Copy link

@kkaname kkaname commented Dec 6, 2025

since they have asked in the question to not to use the relational operators, I have written the code for the loop without the relational operators(&& and ||)

Summary by CodeRabbit

  • Refactor

    • Streamlined loop logic for simpler, more reliable input handling.
  • Style

    • Output now consistently ends with a newline for cleaner display.

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

since they have asked in the question to not to use the relational operators, I have written the code for the loop without the relational operators(&& and ||)
@coderabbitai
Copy link

coderabbitai bot commented Dec 6, 2025

Walkthrough

Replaced a guarded/flagged multi-branch loop with a single infinite loop using explicit break conditions; introduced local int c, i = 0;, explicitly null-terminated the buffer with s[i] = '\0', and changed printing to printf("%s\n", s).

Changes

Cohort / File(s) Change Summary
Loop refactor & I/O
chapter_2/exercise_2_02/loop.c
Rewrote loop as a single infinite loop with break on MAXLINE-1, newline, or EOF; added local int c, i = 0;; explicitly set s[i] = '\0'; changed output from printing raw buffer to printf("%s\n", s); removed prior guarded/flagged control flow and related commented code.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Check loop break conditions and off-by-one handling for MAXLINE
  • Verify correct handling of EOF and newline cases
  • Confirm s[i] = '\0' placement is safe for empty and full inputs

Poem

A rabbit nibbles lines of code,
Tightens loops upon the road.
One break, one terminator placed just right,
The buffer sleeps — the output bright. 🐇✨

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
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.
✅ Passed checks (2 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: simplifying the character input loop in loop.c by replacing a guarded/flagged loop with a single infinite loop using breaks.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2f1fed8 and cafa649.

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

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)

18-21: Minor style nit: final else is redundant

Because both prior branches break, the else before s[i++] = c; is not needed. Dropping it slightly flattens the control flow:

-        if (c == '\n')
-            break;
-        else if (c == EOF)
-            break;
-        else 
-            s[i++] = c;
+        if (c == '\n')
+            break;
+        else if (c == EOF)
+            break;
+
+        s[i++] = c;

Purely optional; no behavior change.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cafa649 and d37125c.

📒 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)

8-25: Loop logic, bounds, and termination all look correct

  • The rewritten loop avoids &&/|| as required while preserving the original behavior: you guard the buffer bound before getchar, then break cleanly on newline/EOF.
  • i >= MAXLINE - 1 guard plus s[i] = '\0'; ensures no out‑of‑bounds writes and always produces a properly null‑terminated string, fixing the previous undefined‑behavior concern.

This is good to ship as‑is.

@ohkimur
Copy link
Owner

ohkimur commented Dec 9, 2025

@kkaname I'm not sure why you think this is better. Can you explain a bit more your thinking?

@ohkimur
Copy link
Owner

ohkimur commented Dec 9, 2025

@kkaname I've added CONTRIBUTIONS.md. Please take a look, as that will help managing these PR's easier for me. :D Thanks!

@kkaname
Copy link
Author

kkaname commented Dec 9, 2025

@ohkimur I mean to say that since the question asked us to solve it without using any relational operator, I have solved the question without the relational operator like && and ||.

@ohkimur
Copy link
Owner

ohkimur commented Dec 9, 2025

@kkaname Thanks for explanation! If that's the case, merge the latest changes in my repo inside your fork. Then run make format and make lint. If there are any errors fix them. After that we can merge your changes. But please include some test cases, with input and outputs, with my original code and your code, so we know the functionality is the same.

@ohkimur
Copy link
Owner

ohkimur commented Dec 9, 2025

@coderabbitai Can you add a comment here with k&r c exercise 2-2 so we have a reference to it? Evaluate the PR changes and analyze how well they fit with the exercise.

@coderabbitai
Copy link

coderabbitai bot commented Dec 9, 2025

Tip

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

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Get the actual file content to see the changes
cat chapter_2/exercise_2_02/loop.c

# Also check if there's any README or documentation about the exercise
fd -t f README chapter_2/exercise_2_02/

Length of output: 812


🌐 Web query:

K&R C programming language exercise 2-2

💡 Result:

Exercise (K&R, ex. 2-2): rewrite the loop
for (i = 0; i < lim-1 && (c = getchar()) != '\n' && c != EOF; ++i)
s[i] = c;
without using && or ||.

One correct approach is to use a simple for (or while) with explicit checks and breaks. Example:

#include <stdio.h>

int main(void) {
int c, i, lim = 1000;
char s[1000];

for (i = 0; i < lim-1; ++i) {
    c = getchar();
    if (c == '\n')
        break;
    if (c == EOF)
        break;
    s[i] = c;
}
s[i] = '\0';
/* use s... */
return 0;

}

This preserves the original semantics (calls getchar() at most once per iteration and stops on newline, EOF, or when i reaches lim-1). See discussion and example solutions. [1][2]

Sources:
[1] Stack Overflow discussion of K&R exercise 2-2.
[2] Another Stack Overflow thread reviewing solutions and pitfalls (evaluation-order/undefined-behavior).


@ohkimur Here's the reference and evaluation:

K&R C Exercise 2-2 Reference

Exercise 2-2 from "The C Programming Language" (2nd Edition) asks to rewrite the following loop:

for (i = 0; i < lim-1 && (c = getchar()) != '\n' && c != EOF; ++i)
    s[i] = c;

without using the && or || operators.

Evaluation of PR Changes

The solution in this PR successfully addresses the exercise requirements:

No relational operators: The code avoids && and || operators entirely
Correct logic flow: Uses if statements with break to achieve the same behavior
Proper termination conditions:

  • Stops when buffer limit is reached (i >= MAXLINE - 1)
  • Stops on newline (c == '\n')
  • Stops on EOF (c == EOF)

Proper string handling: Null-terminates the string with s[i] = '\0'
Single getchar() per iteration: Calls getchar() exactly once per loop iteration

The approach using while(1) with explicit break statements is a standard and correct solution to this exercise. It maintains the same semantics as the original loop while avoiding the logical operators.

Minor observation: The comment at the bottom about multiplication operations appears to be from a different approach and may not be relevant to the current implementation.

@kkaname kkaname closed this by deleting the head repository Dec 9, 2025
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