Test Your Knowledge (Ch 4)

The following questions are based on what we have learned so far in Chapter 4 – The Scanf Function. To test your understanding, try answering the following questions. The explanations are also provided to each question so you can always check the correctness of your answers. All the best 👍


Question 1

What does the scanf function in C do?

Prints formatted output to the console
Reads formatted input from the console
Allocates memory dynamically
Performs mathematical calculations
explanation

The scanf function is used to read formatted input from the console. It allows you to specify the format of the input and store it in the appropriate variables.

Question 2

Which header file should be included to use the “scanf” function?

<stdio.h>
<stdlib.h>
<string.h>
<math.h>
explanation

The scanf function is defined in the <stdio.h> header file, which stands for standard input/output. This header file provides necessary functions to perform input and output operations in C.

Question 3

How many arguments does the “scanf” function needs to read a single integer from the console?

0
1
2
3
explanation

The scanf function needs a minimum of two arguments:
1. the format string: “%d”
2. a variable

For example, the following program is capable of receiving a single integer from the console and storing it in the variable named x.

#include <stdio.h>

int main() {
    int x;
    scanf("%d", &x);
    printf("The value of x is %d", x);
    return 0;
}

Output:

10
The value of x is 10
Question 4

What is the return value of the “scanf” function?

The number of characters read successfully
The formatted input string
The memory address of the input variable
The ASCII value of the first character read
explanation

The scanf function always return the number of items matched and assigned successfully. This means it returns the number of characters read successfully.

For example, consider the following program:

#include <stdio.h> 
int main() {
    int x, y, z;
    printf("The number of items read are %d", scanf("%d%d%d", &x, &y, &z));
    return 0;
}

Output:

10 20 30
The number of items read are 3

If only two integers are provided, then the output will be 2.

10 20
The number of items read are 2
Question 5

How can you prevent the “scanf” function from reading the whitespace characters?

By using the ‘\n’ character as a format specifier
By using the ‘*’ character as a format specifier
By using the ‘\t’ character as a format specifier
By using the ‘@’ character as a format specifier
explanation

By using ‘*’ before the format specifier, we can tell scanf to skip whitespace characters.

For example, consider the following program:

#include <stdio.h>
int main()
{
    int n;
    char c;
    scanf("%d%c", &n, &c); 
    printf("n = %d and c = %c", n, c); return 0;
}

Let’s say the inputs are “10 A” (without quotes). Then, the output will be

10 A
n = 10 and c = 

The variable c is not empty; it is holding the whitespace character.
Now, to skip reading the whitespace characters, we can include the format specifier %*c before the format specifier %c as follows:

#include <stdio.h>
int main()
{
    int n;
    char c;
    scanf("%d%*c%c", &n, &c); 
    printf("n = %d and c = %c", n, c); return 0;
}

Output:

10 A
n = 10 and c = A

Alternatively, we can add a whitespace between %d and %c as follows:

#include <stdio.h>
int main()
{
    int n;
    char c;
    scanf("%d %c", &n, &c); 
    printf("n = %d and c = %c", n, c); return 0;
}

Output:

10 A
n = 10 and c = A
Question 6

What is the correct way to read an integer and a floating-point number using “scanf”? (Considering the user inputs are separated by only whitespaces)

scanf(“%d %f”, num, flt);
scanf(“%d, %f”, &num, &flt);
scanf(“%d &f”, &num, &flt);
scanf(“%d %f”, &num, &flt);
explanation

Option (d) is clearly the correct option. For verification, consider the following program:

#include <stdio.h>
int main() {
    int x;
    float y;
    scanf("%d %f", &x, &y);
    printf("x = %d and y = %f", x, y);
    return 0;
 }

Output:

10 3.14159
x = 10 and y = 3.141590
Question 7

What will be the output of the following code?

#include <stdio.h>
int main() {
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    printf("%c\n", num);
    return 0;
}
The program will not compile successfully
The program will print the ASCII character corresponding to the entered number
The program will print the garbage value
The program will go into an infinite loop
explanation

The format specifier mentioned in the printf function is %c, and therefore, the program will print the ASCII character corresponding to the number entered by the user.

Question 8

What will be the output of the following code?

#include <stdio.h>
int main() {
    int num = 20;
    printf("%d\n", &num);
    return 0;
}
The program will not compile successfully
The program will print the value 20
The program will print the address of the variable num in decimal
None of the above
explanation

In the given program, the address-of operator (&) is used in front of the variable “num” inside the printf call. Therefore, the address of the variable “num” will be displayed.

Question 9

What will be the output of the following code if the input entered by user is “23Hi” (without quotes)?

#include <stdio.h>
int main() {
    int i;
    printf("Enter a number: ");
    scanf("%d", &i);
    return 0;
}
The program will not compile successfully
The program will generate a runtime error
Variable i will receive “23Hi”
Variable i will receive value 23
explanation

As the user input is “23Hi” (without quotes), the scanf function will read the integers 2 and 3, and store them as 23 in variable i. The remaining characters (Hi) are left to be read by the next call of scanf.

Question 10

Suppose that the scanf function is called as follows:

scanf("%f%d%f", &x, &y, &z);

If the user enters

10.5 19.625 789

what will be the values of x, y, and z after the call? (Assume that x and z are float variables, and y is an integer variable)

The program will generate error
x = 10.5, y = 19.625, and z = 789.00
x = 10.5, y = 19, and z = 0.625
x = 10.5, y = 19, and z = 789.00
explanation

Try running the following code:

#include <stdio.h>
int main()
{
    float x, z;
    int y;
    scanf("%f%d%f", &x, &y, &z); 
    printf("x = %f, y = %d and z = %f", x, y, z); 
    return 0;
}

Output:

Hence, verified.

Your score is out of



Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.