Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
C#
scanf(" \"%[^\"]",s);
    printf("%s",s);

this takes input which is within qoutes
for example if i give input as "hello bye " go
then it will print only hello bye
what i want to know is the meaning and other combination possible for " \"%[^\"]" in scanf
and one more doubt is that %s is used for string why it is not used here and still it takes string input
Posted

3 minutes with google found:

http://msdn.microsoft.com/en-us/library/xdb9w69d[^]

Reading Undelimited strings
--------------------------------------------------------------------------------

To read strings not delimited by whitespace characters, a set of characters in brackets ([ ]) can be substituted for the s (string) type character. The set of characters in brackets is referred to as a control string. The corresponding input field is read up to the first character that does not appear in the control string. If the first character in the set is a caret (^), the effect is reversed: The input field is read up to the first character that does appear in the rest of the character set.

Note that %[a-z] and %[z-a] are interpreted as equivalent to %[abcde...z]. This is a common scanf function extension, but note that the ANSI standard does not require it.
 
Share this answer
 
The format code you have here is essentially saying "take everything after the first double quote up to but not including the second double quote". The special
%[]
structure still allows you to specify a string, but instead of accepting any characters it sees, you can restrict the characters using codes within the square brackets. In this case those codes are
^\"
where the leading caret means "not any of the following characters", and the slash escapes the double quote, so the result is "take a string until you find a double quote".


As for what else you can do, check your particular library's docs, but for instance you can do things like "a-z" to accept only lower case letters (the dash specifies all the characters between a and z in the character set). So for example if you wanted to take only a DNA sequence you could use

[AGCT]
, or
[AGCTagct]
if you didn't want to be case sensitive. It is something like a regular expression, but very much simpler.


Hope that helps!

 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900