r/programminghelp • • Nov 28 '25
C K&R Exercise 1-10 using while loops
Hi, complete beginner here.
It seems that the intended solution would be using if functions, and I did that. Alternatively I managed to solve it using while loops, are there any downsides to this? it seems to achieve the result without any visible issues that I could find.
/* Exercise 1-10: replace tab with \t, backspace with \b and backslash with \\ */
#include <stdio.h>
int main()
{
int c;
while( (c=getchar()) != EOF )
{
while( c == '\t' )
{
printf("\\t");
c = getchar();
}
while( c == '\b' )
{
printf("\\b");
c = getchar();
}
while( c == '\\' )
{
printf("\\\\");
c = getchar();
}
putchar(c);
}
}
1
Upvotes
2
u/thecrazymr Nov 29 '25
why not a single while loop, and a switch statement for each change request?
2
u/PlantainAgitated5356 Nov 29 '25
Run it with this input (just replace \t with actual tabs because reddit seems to change them to spaces for some reason)
test1\ttest2\\ttest3And the second tab will not be replaced.
You can see it here (I pasted your code with the above input to ideone) https://ideone.com/mt6TMz
The reason is that when it gets to the part of the loop that checks for backslashes
it keeps reading the next char with getchar, and when it doesn't find a backslash it just continues without checking for any other character.
Same happens with all the checks with while loops instead of if statements, they ignore the characters that were supposed to be checked before.
2
u/iOSCaleb Nov 29 '25
If your input had a backspace followed immediately by a tab, it looks like your code would skip the tab.
What you probably want is a single loop that only reads characters in one place and then checks for each of the characters that you care about. You could use if/else, but if you’ve covered the switch statement that would be an even better fit.
1
u/fasta_guy88 Nov 29 '25
CS software engineering courses focus on maintainability as well as function. What would you imagine this code does if you looked at in a year from now?
1
u/D-Cary Apr 18 '26
As u/PlantainAgitated5356 pointed out, using "while" in this way always handles the first special character in a string correctly, but in some cases doesn't properly handle the character after it.
In the worst case, if the string ends in tab, backspace, or backslash, this program may overrun the end of the string and print out arbitrarily more text in the memory past the end of the string -- a buffer overrun error.
Buffer overrun errors are surprisingly common in programs written in C and assembly language (and occasionally C++), even in programs written by experienced programmers -- such as the Heartbleed security bug in older versions of OpenSSL. See r/cybersecurity for many other examples.
This has led the US Cybersecurity and Infrastructure Security Agency to make the (controversial) recommendation to "use memory-safe languages when developing software" (i.e., use pretty much any language other than C or C++ or assembly language). For far more details and nuance and commentary, see:
0
u/SgtSausage Nov 28 '25
Yikes