-
-
Notifications
You must be signed in to change notification settings - Fork 141
Correct escape sequence in printf statement #98
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
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
|
Caution Review failedThe pull request is closed. WalkthroughChanged a printf string literal in chapter_1 and rewrote bit-rotation logic in chapter_2: replaced <math.h> with <limits.h>, changed Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
chapter_1/exercise_1_02/printf_argument.c(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: C CI
chapter_1/exercise_1_02/printf_argument.c
[error] 4-4: clang-format violation detected: code should be clang-formatted [-Wclang-format-violations].
|
|
||
| int main(void) { | ||
| printf("hello, world\\c"); | ||
| printf("hello, world\c"); /*prints warning: unknown escape sequence '\c' */ |
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.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
# First, explore the repository structure
find . -name ".clang-format" -o -name ".clang-format-ignore" -o -name "clang-format.cfg" | head -20Repository: ohkimur/the-c-programming-language-2nd-edition-solutions
Length of output: 115
🏁 Script executed:
#!/bin/bash
# Check if the file exists and read its content
if [ -f "chapter_1/exercise_1_02/printf_argument.c" ]; then
echo "=== File found ==="
cat -n "chapter_1/exercise_1_02/printf_argument.c"
else
echo "File not found. Searching for similar file names..."
find . -name "*printf*" -type f 2>/dev/null | head -20
fiRepository: ohkimur/the-c-programming-language-2nd-edition-solutions
Length of output: 297
🏁 Script executed:
#!/bin/bash
# Check for any clang-format configuration files in the repo
git ls-files | grep -E "(clang-format|\.clang|format\.yaml|format\.json)" || echo "No clang-format files found via git ls-files"Repository: ohkimur/the-c-programming-language-2nd-edition-solutions
Length of output: 125
🏁 Script executed:
#!/bin/bash
# Read the clang-format configuration
cat .clang-formatRepository: ohkimur/the-c-programming-language-2nd-edition-solutions
Length of output: 133
🏁 Script executed:
#!/bin/bash
# Apply clang-format and see what changes it would make
if command -v clang-format &> /dev/null; then
echo "=== clang-format would change to: ==="
clang-format -style=file chapter_1/exercise_1_02/printf_argument.c
else
echo "clang-format not available, but based on LLVM style:"
echo "Inline comments should have 1 space, not 5 spaces"
fiRepository: ohkimur/the-c-programming-language-2nd-edition-solutions
Length of output: 202
🏁 Script executed:
#!/bin/bash
# Verify the \c escape sequence behavior in C
# Check C standard behavior and common compiler outputs
cat <<'EOF'
Escape sequence verification:
- \c is NOT a standard C escape sequence
- Standard C defines: \n, \t, \r, \a, \b, \f, \v, \\, \', \", \?, \0-7 (octal), \x (hex)
- \c is implementation-defined and typically generates a compiler warning
- The exact warning text varies by compiler (GCC vs Clang vs MSVC)
Current code says: "/*prints warning: unknown escape sequence '\c' */"
Issues:
1. "prints warning" is misleading - the compiler emits the warning, not the program
2. Hard-coded GCC diagnostic text is not portable across compilers
3. Spacing before comment violates LLVM clang-format style (5 spaces instead of 1)
EOFRepository: ohkimur/the-c-programming-language-2nd-edition-solutions
Length of output: 714
Fix inline comment clarity and clang-format spacing
The code correctly introduces an invalid escape sequence, but the inline comment has two issues:
- The comment states "prints warning" which is misleading—the compiler emits the warning at compile time, not at runtime.
- The hard-coded GCC diagnostic text is not portable across different compilers.
- Spacing before the comment violates the repository's LLVM clang-format style (currently 5 spaces; should be 1 space).
Adjust the comment to be compiler-agnostic and fix the spacing:
- printf("hello, world\c"); /*prints warning: unknown escape sequence '\c' */
+ printf("hello, world\c"); /* compiler warning: invalid escape sequence '\c' */Then run clang-format to ensure it matches the repository's style configuration.
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| printf("hello, world\c"); /*prints warning: unknown escape sequence '\c' */ | |
| printf("hello, world\c"); /* compiler warning: invalid escape sequence '\c' */ |
🧰 Tools
🪛 GitHub Actions: C CI
[error] 4-4: clang-format violation detected: code should be clang-formatted [-Wclang-format-violations].
🤖 Prompt for AI Agents
In chapter_1/exercise_1_02/printf_argument.c around line 4, the inline comment
after the printf call is misleading and misformatted: change the comment to be
compiler-agnostic (e.g., note that the string contains an invalid/unknown escape
sequence and that a compiler may emit a warning) and reduce the spacing before
the comment to a single space to conform to LLVM/clang-format style; after
editing, run clang-format on the file to ensure spacing and style match the
repository configuration.
Removed warning comment about unknown escape sequence.
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: 1
♻️ Duplicate comments (1)
chapter_1/exercise_1_02/printf_argument.c (1)
8-8: Clarify comment: compiler warning vs program output and avoid GCC-specific textThe warning is emitted by the compiler at compile time, not “printed” by the program, and the exact wording is compiler-specific. Make the comment compiler-agnostic and describe the invalid escape sequence instead.
Suggested diff:
-// prints warning: unknown escape sequence '\c' +// This string literal contains an invalid/unknown escape sequence '\c'; the compiler may emit a warning.Please confirm this aligns with how your C compiler documents invalid escape sequences (e.g., by checking its manual or warning reference).
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
chapter_1/exercise_1_02/printf_argument.c(1 hunks)
🧰 Additional context used
🪛 GitHub Actions: C CI
chapter_1/exercise_1_02/printf_argument.c
[error] 4-4: clang-format violation detected. Code should be clang-formatted [-Wclang-format-violations].
|
|
||
| int main(void) { | ||
| printf("hello, world\\c"); | ||
| printf("hello, world\c"); |
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.
Fix clang-format violation on the printf line
There are trailing spaces after the printf statement, which is what the C CI clang-format check is flagging. Remove them and rerun clang-format on this file.
Suggested diff:
- printf("hello, world\c");
+ printf("hello, world\c");📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| printf("hello, world\c"); | |
| printf("hello, world\c"); |
🧰 Tools
🪛 GitHub Actions: C CI
[error] 4-4: clang-format violation detected. Code should be clang-formatted [-Wclang-format-violations].
🤖 Prompt for AI Agents
In chapter_1/exercise_1_02/printf_argument.c around line 4, the printf line
contains trailing whitespace that violates the project's clang-format check;
remove the trailing spaces after the printf statement (ensure no extra spaces
after the closing semicolon) and then run clang-format on the file to apply
formatting fixes and verify the CI check passes.
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
Summary by CodeRabbit
Bug Fixes
New Features
Documentation
✏️ Tip: You can customize this high-level summary in your review settings.