134k views
4 votes
Suppose we have a variable of type int named height. Which of the following lines reads a value from stdin and stores it in this variable?

a. scanf("%d", height)
b. scanf("%d", &height)
c. scanf("%s", height)
d. scanf("%s", &height)
e. scanf(height)

1 Answer

2 votes

Final answer:

The correct code to read an integer from stdin and store it in the variable 'height' is 'scanf("%d", &height)', which is option b.

Step-by-step explanation:

To read a value from stdin (standard input) and store it in a variable of type int named height, the correct line of code would be: scanf("%d", &height). This line is represented by option b. The %d format specifier is used for int data types in C/C++. Importantly, the ampersand (&) is necessary before the variable name height to pass the address of variable to scanf, allowing it to store the input value in the correct memory location.

User Vijayan
by
7.3k points