Section 4.9 The char
Datatype
So far we have only stored integer numbers in variables.
You can also store letters or symbols in variables (in computer speak these are called characters).
The datatype to store a character is called char
, the format specifier is %c
Here is an example:
admin.....open in new window
Activity 4.8. printf
Practice.
Write a program that prints out “ENGS 20” using the following printf()
statement:
printf("%c%c%cS 20\n", letter1, letter2, letter3);
Please do not make any changes to the printf
statement since the autograder will not function correctly if you do.
admin.....open in new window
When you are done, please put your code into the box below:
Reading characters from the user input.
You can use scanf()
to read a character from the user input:
admin.....open in new window
Investigate 4.9.
What do you think would happen if you tried to use the scanf()
function to read a character, but you use the format specifier for an integer-type variable, %d
? Will it work or fail?
Hint.
Take your best guess, and then feel free to try it out in the code window above