-
-
Notifications
You must be signed in to change notification settings - Fork 140
Simplify character input loop in loop.c #94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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 ||)
WalkthroughReplaced a guarded/flagged multi-branch loop with a single infinite loop using explicit break conditions; introduced local Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this 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: finalelseis redundantBecause both prior branches
break, theelsebefores[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
📒 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 beforegetchar, then break cleanly on newline/EOF.i >= MAXLINE - 1guard pluss[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.
|
@kkaname I'm not sure why you think this is better. Can you explain a bit more your thinking? |
|
@kkaname I've added CONTRIBUTIONS.md. Please take a look, as that will help managing these PR's easier for me. :D Thanks! |
|
@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 ||. |
|
@kkaname Thanks for explanation! If that's the case, merge the latest changes in my repo inside your fork. Then run |
|
@coderabbitai Can you add a comment here with |
|
Tip For best results, initiate chat on the files or code changes. 🧩 Analysis chain🏁 Script executed: Length of output: 812 🌐 Web query: 💡 Result: Exercise (K&R, ex. 2-2): rewrite the loop One correct approach is to use a simple for (or while) with explicit checks and breaks. Example: #include <stdio.h> int main(void) { } 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: @ohkimur Here's the reference and evaluation: K&R C Exercise 2-2 ReferenceExercise 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 Evaluation of PR ChangesThe solution in this PR successfully addresses the exercise requirements: ✅ No relational operators: The code avoids
✅ Proper string handling: Null-terminates the string with The approach using 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. |
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
Style
✏️ Tip: You can customize this high-level summary in your review settings.