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: 42
Here,
printf()
asks the user to enter a number.scanf("%d", &number);
takes the user's input and stores it in thenumber
variable.%d
tellsscanf
that you're expecting an integer.&number
is 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.