Suppose I want a user to enter an integer between 1 and 8.

To start with, here is the program that uses scanf to accept integer from user.

#include <stdio.h>
#include <cs50.h>
int main (void)
int number;
printf ("enter a number ");
scanf ("%i", &number);
printf ("the number entered is %i", number);

Gradually, I am to let the user enter integer between 1 and 8. If anything else is entered, the cursor will wait till the user enters integer between 1 and 8 (Mario World 1-1). The Hints apparenty prescribes to use get_int.

My query is if a similar objective cannot be by scanf. What is the difference between scanf and get_int? Is get_int better, more powerful?

1 Answer 1


As you will discover later on during the course, get_int() is part of the cs50 library "cs50.h" which is not used in the real world, except in the course. To ease the apprenticeship, David and his team created simplified function to let students understand more rapidly the dev logic. You have all the details of this library here : https://cs50.readthedocs.io/libraries/cs50/c/

So scanf() is the normal way to proceed and is similar to get_int()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .