Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C User Input

User Input

You’ve already discovered that printf( ) serves to display values in C.

For obtaining user input, scanf( ) function can be utilized.

Example

Display a number provided 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 requires two arguments: the format specifier for the variable (%d in the above example) and the address-of operator (&myNum), which stores the memory address of the variable.

Tip: In the upcoming chapter, you’ll delve deeper into memory addresses and functions.

Multiple Inputs

The scanf() function also permits multiple inputs, as demonstrated in the following example where an integer and a character are accepted.

Example

// Create an int and a char variable
int myNum;
char myChar;

// Ask the user to type a number AND a character
printf(“Type a number AND a character and press enter: \n”);

// Get and save the number AND character the user types
scanf(“%d %c”, &myNum, &myChar);

// Print the number
printf(“Your number is: %d\n”, myNum);

// Print the character
printf(“Your character is: %c\n”, myChar);

Take String Input

You can also obtain a string input from the user.

Example

Display the name provided by the user:

// Create a string
char firstName[30];

// Ask the user to input some text
printf(“Enter your first name: \n”);

// Get and save the text
scanf(“%s”, firstName);

// Output the text
printf(“Hello %s”, firstName);

 

Note: When using strings in scanf(), it’s essential to specify the size of the string/array (in our example, we used a sufficiently high number like 30 to ensure it can store enough characters for the first name), and there’s no need to use the reference operator (&).

However, the scanf( ) function is limited in that it treats spaces (whitespace, tabs, etc.) as terminating characters. Consequently, it can only process a single word even if multiple words are inputted. For instance:

Example

char fullName[30];

printf(“Type your full name: \n”);
scanf(“%s”, &fullName);

printf(“Hello %s”, fullName);

// Type your full name: John Doe
// Hello John

In the example provided, while one might anticipate the program to output “John Doe,” it only outputs “John.”

That’s why, when dealing with strings, the fgets() function is commonly employed to read an entire line of text. It’s important to include the following arguments: the string variable’s name, sizeof (string_name), and stdin:

Example

char fullName[30];

printf(“Type your full name: \n”);
fgets(fullName, sizeof(fullName), stdin);

printf(“Hello %s”, fullName);

// Type your full name: John Doe
// Hello John Doe

 

Utilize the scanf() function for receiving a single word as input, and opt for fgets() when handling multiple words.