Complete the code to read an integer from the user using scanf.
#include <stdio.h> int main() { int number; scanf([1], &number); printf("You entered: %d\n", number); return 0; }
The format specifier %d is used to read an integer with scanf.
Complete the code to read a float value from the user using scanf.
#include <stdio.h> int main() { float value; scanf([1], &value); printf("You entered: %f\n", value); return 0; }
The format specifier %f is used to read a float with scanf.
Fix the error in the scanf statement to correctly read a character.
#include <stdio.h> int main() { char ch; scanf([1], &ch); printf("You entered: %c\n", ch); return 0; }
The format specifier %c is used to read a single character with scanf.
Complete the code to read a string safely using scanf.
#include <stdio.h> int main() { char name[20]; scanf([1], name); printf("Hello, %s!\n", name); return 0; }
To safely read a string into an array, use %19s to limit input size. When reading strings into arrays, do not use the ampersand (&) because the array name is already a pointer.
Fill all three blanks to read two integers and one float from the user.
#include <stdio.h> int main() { int a, b; float c; scanf("[1] [2] [3]", &a, &b, &c); printf("Integers: %d, %d and Float: %f\n", a, b, c); return 0; }
The format string should match the variables: "%d %d %f" to read two integers and one float. Fill "%d" for the first two blanks and "%f" for the third.