Lesson 3 of 23

Reading Input with scanf()

8 min 20 XP

scanf() reads values from the user. Put & (address-of) before the variable name so scanf can write into it. Use %lf to read a double.

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

Reading a string (char array) needs no &, because an array's name is already an address. %s reads only one word, up to a space.

Challenge

+20 XP

Read the radius of a circle (a double) and print its area with 2 decimal places. Use PI = 3.14159. Input: 2 → Output: 12.57

Output Loading C (first time takes a few seconds)
Press Run to see the result here.