Index
- Single Input
- Multiple Input
- Take String Input
- [[#scanf-limitations|
scanf
Limitations]]
Single Input
To get user input, you can use the scanf()
function, example:
- The
scanf()
function takes two arguments:- the C Format Specifiers of the variable
- the C Variables And Constants where the input will be saved
oss: if the input is not accepted, the variable won’t be saved oss: you can specify the input text
The input from the user must follow the same sintax:
[! warning] Note: it’s better to not inset spaces inside the scanf sintax
Multiple Input
The scanf()
function also allow multiple inputs (an integer and a character in the following example):
Take String Input
You can also get a string entered by the user:
[! warning] Note: When working with strings in
scanf()
:
- you must specify the size of the string/array
- you don’t have to use the reference operator (
&
).
scanf
Limitations
scanf considers space (whitespace, tabs, etc) as a terminating character,
- which means that it can only display a single word (even if you type many words).
Example:
From the example above, you would expect the program to print “John Doe”, but it only prints “John”.
kikketta To Solve scanf
Limitations
Using this metod you can reed more of one word at the time
In this example, we declare a character array line
of size 100 to store the user’s input. We use the %[^\n]
format specifier to read a string with spaces, which tells scanf
to read all characters up to (but not including) a newline character ('\n'
).
Note:
We are basicly ignoring all escaping character but not inlcluding
\n
(new line)