C User Input

User Input

You have already learned that printf() is used to output values in C.

To get user input, you can use the scanf() function:

Example

Output a number entered by the user :

// Create an integer variable that will store the number we get from the user
int myNum;

// Ask the user to type a number
printf("Type a number: \n");

// Get and save the number the user types
scanf("%d", &myNum);

// Output the number the user typed
printf("Your number is: %d", myNum);

The scanf() function takes two arguments: the format specifier of the variable (%d in the example above) and the reference operator (&myNum), which stores the memory address of the variable.

 

Continue reading C User Input