Marco
asked

Expert
Kelish Rai answered
In C, you can take user input using the scanf() function. It reads values entered by the user and stores them in variables. For example,
#include
int main() {
int number;
printf("Enter a number: ");
// Take input and store it in number variable
scanf("%d", &number);
printf("You entered: %d\n", number);
return 0;
} Output
Enter a number: 42
You entered: 42Here,
printf()asks the user to enter a number.scanf("%d", &number);takes the user's input and stores it in thenumbervariable.%dtellsscanfthat you're expecting an integer.&numberis the address of the variable where the input will be stored.
Finally,
printf()displays the entered number.
Note: Always remember to use & (address-of operator) before the variable name in scanf().
C
This question was asked as part of the Practice: C Programming course.
Our Experts
Sudip BhandariHead of Growth/Marketing
Apekchhya ShresthaSenior Product Manager
Kelish RaiTechnical Content Writer
Abhilekh GautamSystem Engineer
Palistha SinghTechnical Content Writer
Sarthak BaralSenior Content Editor
Saujanya Poudel
Abhay Jajodia
Nisha SharmaTechnical Content Writer
Udayan ShakyaTechnical Content Writer