What do ^/ ^\ and * do in a scanf statement. Couldn't find the answer anywhere on the Net.
What do ^/ ^\ and * do in a scanf statement. Couldn't find the answer anywhere on the Net.
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
Perhaps you should show an example of what you're actually talking about?Yes I encountered that page, doesn't say.
Something like the following perhaps?
Note the link above does describe these conversions:Code:// read all whitespace, then store all characters up to a newline scanf(" %[^\n]%*d", s);
[^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.The '\' character in the above snippet is the "normal" character escape character that is combined with the next character 'n' to produce the end of line character sequence '\n'.The format specifier can also contain sub-specifiers: asterisk (*), width and length (in that order), which are optional and follow these specifications:
* An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).
These two examples concern the use of forward and backslash respectively in combination with ^ That is what my query is about, not the actual code of the examples. The function of these slashes is what my question is about.Code:int main(void) { int i; char str[MAX]; for(i=0;i<MAX;i++) str[i]='\0'; printf("Enter a date [dd/mm/yy]: "); scanf("%[^\n]s",str); printf("%s\n",str); return 0; } if (scanf("%9[^/]/%2[^/]/%4s", day, month, year) != 3)
Okay so then part of your confusion is "%9[^/]" do you realize that the '[' ']' combination tells scanf() to retrieve a string, the %9 limits this string to 9 characters (including the required end of string character '\0').
So then the "[^/]" sequence says to process the stream until a '/' character is encountered. Assuming that the '/' is in the stream every character encountered before the '/' character is placed into the string (day in this case), including any white space characters, the '/' is extracted from the stream and discarded.
(Re example #2) Are you saying that:
1, scanf() normally inputs one value
2. but by using [] you van give several variables etc. as value?
So why the != 3. I tried the statement and it would process any number of inputs.
(Re the first example, above the second)
The first example shows slashes that go the other way, what is the difference in function comparing the use of slashes in the two different examples? In the first example, on the face of it inputs only one value, but you can input several: day, month and year. Does the fact that the slash is the other way do that?
What? The scanf() function can be used with almost any number of parameters.1, scanf() normally inputs one value
Again, what? The using the [] retrieve input into one string per [] pair.2. but by using [] you van give several variables etc. as value?
Because you are retrieving how many strings?So why the != 3.
Yes, do you understand what the '\' slash does in character literals? Do you realize that '\n' is the new line character sequence?The first example shows slashes that go the other way,
No, look closer at your format string: "%[^\n]s", notice that caret, it means process everything until you encounter the end of line character ('\n'). By the way that trailing 's' is probably either a typo or an outright mistake.In the first example, on the face of it inputs only one value, but you can input several: day, month and year. Does the fact that the slash is the other way do that?
I thought the s which you say might be a mistake means string, like e.g. d is for integer.
So:
The [ ] means make into a separate string. In the first example, that is being done anyway until the user punches the Return-key. So why the [ ]? The input is for one string anyway.
The second example three strings are being formed. Each new string is formed after encountering an / in the input.
So the carrot means: "this is a command, I am not part of the value".
The !=3 . . .Scanf mysteriously counts the strings it made.
Bingo?
Nope, for the first example, it means that the stream must contain an 's' right after a new line character (that s shouldn't really be in the format string).I thought the s which you say might be a mistake means string, like e.g. d is for integer.
No it means process the next input as a string. The characters within the [] tell scanf() how to interpret the stream for that particular string.The [ ] means make into a separate string.
Because the programmer wants the string to be able to contain white space characters (other than the new line character) not stop processing the stream if a white space character is encountered.In the first example, that is being done anyway until the user punches the Return-key. So why the [ ]?
Correct.The second example three strings are being formed.
Kind of. It really means that the string is delimited by a '/' character.Each new string is formed after encountering an / in the input.
No the carrot is the "negation" operator, meaning accept all characters except those included within the []. Realize that you can have multiple characters within the []. Normally, without the carrot, scanf() would only include the characters contained within the [] in the string.So the carrot means: "this is a command, I am not part of the value".
Not mysterious at all, read about the return value from the link contained above.The !=3 . . .Scanf mysteriously counts the strings it made.
So if you have 3 arguments in the argument list you should expect a return value of 3, anything else signifies a processing error.Return Value
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
Jim, thank you very much for your very informative information!!