0

Problem : Rewrite small numbers from input to output. Stop processing input after reading in the number 42. All numbers at input are integers of one or two digits.

Example

Input : 1 2 88 42 99

Output : 1 2 88

My solution :

#include<stdio.h>
int main()
{
int i;
scanf("%d",&i);
while(i!=42)
{ 
printf("%d",i);
scanf("%d",&i);
}
return 0;
}

Correct Solution :

#include <stdio.h>

int main( void ) {
int i;
while( 1 ) {
    scanf( "%d", &i );
    if( i == 42 ) break;
    printf( "%d\n", i );
}
return 0;
}

Both the programs end if the input is 42, then what is the difference between the two?

EDIT : I just realized that this should have been posted at codereview.stackexchange.com Admins please do the needful.

reb94
  • 139
  • 2
  • 8
  • 3
    One prints "\n", one doesn't. – Oliver Charlesworth May 31 '13 at 13:25
  • 2
    I believe they are strictly equivalent, and some hardcore no-goto no-break guys would even say your code is more elegant. – Medinoc May 31 '13 at 13:27
  • 2
    I agree with @Medinoc. I detest `while(1)` statements, as on their surface, they appear to loop forever. If the intent of code is to stop on a specific condition, then the code should be written to clearly reflect that intent. – abelenky May 31 '13 at 13:28

4 Answers4

2

Not too much. The only differences I see are that:

  1. You don't output a newline "\n" after printing each number.

  2. Your code isn't completely "DRY" (you repeat the line scanf("%d",&i);).

Otherwise they're (functionally) identical. But I feel compelled to add:

  • Your code is slightly uglier... Use proper indentation!
surfreak
  • 808
  • 1
  • 7
  • 16
1

In the first program you enter the cycle after the user has entered the first value, so you have to check the value in the while condition.

The second program, instead, checks input always inside the cycle, and is therefore clearer.

Claudio
  • 9,427
  • 3
  • 28
  • 67
1

From the question itself I can say that it is from one of the programming contest website practice questions.

These sites also match the format in which you are answering your question. So a newline(\n) is necessary.

Harshil Lodhi
  • 6,098
  • 1
  • 31
  • 41
  • Yes you are correct. My solution was accepted after I added the newline character. Thanks for your help. – reb94 May 31 '13 at 14:02
0

They are functionally equivalent, and personally I much prefer your solution: while(1) is considered by some, including me, to be bad practice. It is easier to see when the while loop terminates just by looking at the single line, rather than having to trace through the code. A few differences otherwise:

  • You need to indent your code, or it is hard to read
  • You are not printing a newline (\n)
devrobf
  • 6,323
  • 1
  • 25
  • 40
  • 2
    Why is `while (1)` considered bad practice? Do you object to all forms of infinite loop or do you prefer `while(true)`, `for(;;)`, `goto` ...? – simonc May 31 '13 at 13:30
  • 1
    I personally am not keen on infinite loops whenever possible, for the reason I stated above. I think it is clearer and less likely to cause bugs, because there could be code paths that you miss which do not break when they should. As I say though, this is my (and at least some others, see comments on the question) opinion, it is still functionally correct. – devrobf May 31 '13 at 13:38
  • Thanks for making the update. Not sure I agree with your reasoning but the reasons for it are clear now :-) – simonc May 31 '13 at 13:39
  • I don't understand what `while (1)` does. 1 is just a constant. Why is it treated as the loop condition? Does 1 imply that no condition is to be evaluated before executing the body of the `while` loop? If this is the case, then can I not enter an other character or number instead of 1? – reb94 May 31 '13 at 14:12
  • In C, there is no such thing as true and false - instead, `0` (as an integer) is treated and false non-zero values are treated as true. Therefore `1` is `true` in C. `while (1)` means "loop forever". – devrobf May 31 '13 at 14:18
  • So can I type 2 or 3 or any other number except zero, instead of 1? – reb94 May 31 '13 at 14:25
  • Yes. It might look a bit odd, although you often see this when checking that an integer is non-zero. – devrobf Jun 01 '13 at 00:30