# “Guess the Number” in C

This is my first program in C. Do you have any improvements to this program? The program allows you to guess a number and also returns the attempts.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
/*
* Sophie
* 21.03.2021
*/
void main() {
int secret;
int guess;
int try = 1;

time_t t;
srand((unsigned ) time(&t));
secret = rand() % 100+1;

scanf("%i", &guess);

while (guess != secret){
if (guess > secret)
printf("Guess too high. \n");
else
printf("Guess too low. \n");

scanf("%i", &guess);
try++;
}
printf("You gussed the number! \n");
printf("You needed %i Tries.", try);

getch();
}

• This is a great program to learn with!  The first program I ever wrote — in BASIC on a borrowed Video Genie, no less — was a guessing game.  Though after all this time I can't remember whether the computer picked the number and the human had to guess it (as here), or vice versa…  Both are good, so once you're happy with one, try the other! – gidds Mar 22 at 14:25

I also liked the program - it did not compile, but that was easy to fix - what are <conio> and getch()?

I enjoyed a game or two, until I wondered if it was 0-99 or 1-100. The spacing of  % 100+1 was a bit misleading.

I simplified the time() call.

Then I wondered why there are two scanf() calls. The input messages are different (please enter / please enter NEW), but the input itself is the same. So I ended up with while(scanf() > 0).

It is not so clear what to put in that while condition. Also what to do with invalid input. My version stops and prints the number of tries.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* modified number guessing game, with while(scanf()) loop */
/* original by Sophie 21.03.2021 */

void main() {
int secret, guess;
int try = 0;
srand(time(NULL));
secret = rand() % 100 + 1;

while (scanf("%i", &guess) > 0) {
try++;
if (guess == secret) {
printf("You guessed the number!\n");
break;
}
if (guess > secret)
printf("Guess too high. ");
else
printf("Guess too low. ");

}

printf("You tried %i times.\n", try);
}


I am afraid you have to sacrifice that nice

while (guess != secret) {


and concentrate more on the scanf() return.

(Well with that negation != it wasn't even that great - more like "trapped in eternal loop as long as guess is wrong")

• The spacing of % 100+1 was a bit misleading don't fix that with spaces, use braces: secret = (rand() % 100) + 1; – Paul Evans Mar 22 at 10:47
• @PaulEvans I'd call those brackets, round brackets, parentheses, or parens; braces are curly brackets — { and } — and have a very different meaning in most programming languages!  But yes, whatever you call them, they're useful for clarifying precedence.  This case is a bit subjective; I'd consider % similar enough to division that the precedence was already obvious — but if it's not obvious to you, by all means use brackets. – gidds Mar 22 at 14:30
• Your version still doesn't compile (error: invalid prototype for main function). – Konrad Rudolph Mar 22 at 14:43
• @gidds I should have looked it up and used parentheses, round brackets, or first brackets but I think the idea is clearly conveyed by the example. And don't consider % similar enough to division that the precedence was already obvious use parentheses to make your code totally clear! Be kind to your readers and to your future self, make reading your code as easy as possible. – Paul Evans Mar 22 at 16:04
• @gidds "I'd consider % similar enough to division that the precedence was already obvious." Obvious what the precedence is or obvious what precedence the previous coder intended? – Kevin Mar 22 at 20:46
• void main is a really bad habit. It must be int main.

• Always check what scanf returns. For example, try to enter a non-numeric input, and see your program entering the infinite loop.

• Avoid conio.h (and hence getch). It is very non-portable.

• It could be worth mentioning why is void main a bad habit. I don't think it's good to tell someone that something is bad, without telling why. (I believe the reason is because it is not valid C code, altho some compilers will ignore it and still compile it, but i might be wrong.) – AnswerMyQuestion Mar 22 at 17:42
• Yes, the C standard mandates that main() returns an int. Moreover, the program needs to return a status code to the operating system, and this return value is how you do it. (Sometimes embedded systems don't have an operating system to return to, but usually they will either specify a different function to use or else they will still use int main() even though the return value is meaningless.) – Kef Schecter Mar 22 at 21:39

It looks good for a first program in C, good job.

An improvement would be to ask the user to insert a number which will be the maximum number to guess (instead of the default 100 value).

You also could change the try variable name to tries.

• I think there are arguments both for try (which makes more sense within the loop) and tries (which reads better below it).  (I might pick tries myself, but probably from my experience in languages like Java and Kotlin where try is a keyword, which obviously doesn't apply here!) – gidds Mar 22 at 14:33
• That's right. "attempts" could also be a great name for this variable. – Kfir Ram Mar 22 at 14:39