Thread: Elementary question (sorry)

  1. #1
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75

    Elementary question (sorry)

    What do ^/ ^\ and * do in a scanf statement. Couldn't find the answer anywhere on the Net.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...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

  3. #3
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Yes I encountered that page, doesn't say.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Yes I encountered that page, doesn't say.
    Perhaps you should show an example of what you're actually talking about?

    Something like the following perhaps?
    Code:
    // read all whitespace, then store all characters up to a newline
    scanf(" %[^\n]%*d", s);
    Note the link above does describe these conversions:
    [^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.
    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).
    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'.

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by Ivory348 View Post
    What do ^/ ^\ and * do in a scanf statement. Couldn't find the answer anywhere on the Net.
    Please show the specific scanf() call, that shows the use of these chars, and we can explain.

  6. #6
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by rstanley View Post
    Please show the specific scanf() call, that shows the use of these chars, and we can explain.
    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)
    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.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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.

  8. #8
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by jimblumberg View Post
    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?

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    1, scanf() normally inputs one value
    What? The scanf() function can be used with almost any number of parameters.

    2. but by using [] you van give several variables etc. as value?
    Again, what? The using the [] retrieve input into one string per [] pair.

    So why the != 3.
    Because you are retrieving how many strings?

    The first example shows slashes that go the other way,
    Yes, do you understand what the '\' slash does in character literals? Do you realize that '\n' is the new line character sequence?

    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?
    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.

  10. #10
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Quote Originally Posted by Ivory348 View Post
    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)
    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.
    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?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    I thought the s which you say might be a mistake means string, like e.g. d is for integer.
    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).

    The [ ] means make into a separate string.
    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.

    In the first example, that is being done anyway until the user punches the Return-key. So why the [ ]?
    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.


    The second example three strings are being formed.
    Correct.

    Each new string is formed after encountering an / in the input.
    Kind of. It really means that the string is delimited by a '/' character.

    So the carrot means: "this is a command, I am not part of the value".
    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.

    The !=3 . . .Scanf mysteriously counts the strings it made.
    Not mysterious at all, read about the return value from the link contained above.

    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.
    So if you have 3 arguments in the argument list you should expect a return value of 3, anything else signifies a processing error.

  12. #12
    Registered User Ivory348's Avatar
    Join Date
    Oct 2019
    Posts
    75
    Jim, thank you very much for your very informative information!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Elementary question
    By Dan. in forum C++ Programming
    Replies: 1
    Last Post: 03-02-2011, 01:47 PM
  2. Very elementary C++ question
    By Dondrei in forum C++ Programming
    Replies: 13
    Last Post: 07-28-2008, 09:00 AM
  3. elementary question on vector and pop_back
    By mc61 in forum C++ Programming
    Replies: 3
    Last Post: 04-09-2008, 09:08 AM
  4. An Elementary Windows Question
    By samGwilliam in forum Windows Programming
    Replies: 7
    Last Post: 01-20-2006, 04:20 PM
  5. elementary question
    By misplaced in forum Tech Board
    Replies: 8
    Last Post: 12-17-2004, 07:27 AM

Tags for this Thread