scanf issue

This is a discussion on scanf issue within the C Programming forums, part of the General Programming Boards category; scanf ("%s ", &test); scanf ("%s", &test); scanf (" %s", &test); scanf (" %s ", &test); Er........I remember once someone ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    147

    scanf issue

    scanf ("%s ", &test);

    scanf ("%s", &test);

    scanf (" %s", &test);

    scanf (" %s ", &test);


    Er........I remember once someone told me to leave a space after scanf ("%s<space>", &test);

    actually i do not know why, do they produce different results? or are there specific consequences or results which occurs by leaving a space as shown in the above conditions?
    Only by the cross are you saved...

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    I never leave spaces between the quotes and the %d, %s, etc and have never had any problems.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    75
    should give you no troubles

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    8

    Re: scanf issue

    actually it is advisable to leave a space before the % sign.

    eg : scanf ("<space>%s", &test);

    This is specially relevant when we declare more than two characters.

    without the space..when you press enter after inputing the first value,,c considers the enter key as your next value and will not allow you to enter two values.

    Originally posted by fkheng
    scanf ("%s ", &test);

    scanf ("%s", &test);

    scanf (" %s", &test);

    scanf (" %s ", &test);


    Er........I remember once someone told me to leave a space after scanf ("%s<space>", &test);

    actually i do not know why, do they produce different results? or are there specific consequences or results which occurs by leaving a space as shown in the above conditions?

  5. #5
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    I'm just nitpicking, but shouldn't &test be test? Because test is char*?

  6. #6
    .........
    Join Date
    Nov 2002
    Posts
    303
    Yea it should be.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >actually i do not know why, do they produce different results?
    They can. scanf is unique in how it handles whitespace. A newline is taken to be whitespace, so plenty of people (even experienced C programmers) have trouble with scanf and string data. The most common problem being something like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char s[100];
    
        puts("One");
        scanf("%s", s);
        puts("Two");
        fgets(s, sizeof (s), stdin);
        puts("End");
    
        return 0;
    }
    scanf stops reading at whitespace and leaves the resulting newline character in the input stream. Since fgets stops reading at a newline and the first character it reads is the leftover newline from scanf, it appears as if the call to fgets is skipped completely. Some people like to add a space in the format string of scanf since a space tells scanf to read a length of whitespace. This of course is a hack that rarely works as planned. Other hacks include
    Code:
    fflush(stdin); /* Undefined */
    and
    Code:
    while (getchar() != '\n')
        ;
    and a better (if cryptic) use of scanf to remove extraneous characters from the stream and then eat the newline if scanf succeeded:
    Code:
    if (scanf("%99s%*[^\n]", s) >= 0)
            getchar();
    Note the 99 added to mark the maximum length of acceptable input. This is a good idea in any situation because the vanilla %s format flag is open to buffer overrun if there's too much input.

    All of this should tell you that it's usually better to avoid scanf completely and use fgets for all of your interactive input needs. Then you can use a host of parsing techniques (such as sscanf which doesn't suffer the problems of scanf).
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with scanf and char
    By Covalent in forum C Programming
    Replies: 6
    Last Post: 11-02-2008, 11:06 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. 'cin' and 'scanf()' issue
    By Brain Cell in forum C++ Programming
    Replies: 4
    Last Post: 11-07-2004, 09:53 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21