Strange behavior of Strings

This is a discussion on Strange behavior of Strings within the C Programming forums, part of the General Programming Boards category; Hi All, The following code line behaves strange...Could anyone of you help in understanding this out.... Code: char a[10]; printf("\nEnter ...

  1. #1
    ..................
    Join Date
    Oct 2005
    Posts
    11

    Strange behavior of Strings

    Hi All,

    The following code line behaves strange...Could anyone of you help in understanding this out....

    Code:
    char a[10];
    printf("\nEnter a string:");
    scanf("%s ",a);
    printf("\nAccepted String is :%s",a);
    I gave an extra space " " in the scanf statment after "%s".

    The output behaves as follows:

    Enter a String:abcdef
    a
    Accepted String is :abcdef


    Although I press enter key after entering the string when prompted...it is waiting for me to give one more character. After entering an extra character in 2nd line, then the output is being displayed....

    Kindly help me out...

    Best Regards,
    Shyamlal SVS.

  2. #2
    ..................
    Join Date
    Oct 2005
    Posts
    11
    Added to the above information....

    If i remove the space in scanf statement...the program behaves normally....

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's not strange. It's just scanf. Don't pass it scan specifiers you don't plan on using.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ..................
    Join Date
    Oct 2005
    Posts
    11
    Sorry Quzah...little confusing....Plz explain me in detail...

    Thx inadvance

  5. #5
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Because scanf() waits for exactly the same format as indicated in the quoted part. That's why you are required to enter one space at the end.

    If you are expecting a string like

    Code:
    Hello my name is + blabla
    You must use something like this

    Code:
    scanf("%s %s %s %s + %s",...);
    Match each word with a %s. Now I think you have the idea!
    Last edited by fnoyan; 03-27-2006 at 04:35 AM.

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    ...and by the way, while fnoyan's example
    Code:
    scanf("%s %s %s %s + %s",...);
    is what you have to do in scanf(), it's something you should never find yourself doing in a real program.
    Sent from my iPadŽ

  7. #7
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    SlyMaelstrom said
    >.... you should never find yourself doing in a real program.

    Sure I prefer fgets() if unformatted input will be accepted.

  8. #8
    ..................
    Join Date
    Oct 2005
    Posts
    11
    Hi fnoyan..

    I got ur answer...but one more question is....

    Although I give space/enter key(n times) ...it is waiting for one character...Until i give any character(I mean any letter/number/sp. character)..the program is not terminating.....

  9. #9
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Just a prediction, somebody will correct me if I am wrong!

    Here is a new code
    Code:
    #include <stdio.h>
    int main()
    {
    char a[10];
    char b[10];
    printf("\nEnter a string:");
    scanf(" %s %s",a,b);
    printf("\nAccepted String is :%s %s",a,b);
    return 0;
    }
    There are two white soaces before and after the first char array. Use any number of white spaces after first string, scanf() treats as if there is only one white-space.

    What about the white space at the begining then! scanf() simply ignores it! So, entering
    Code:
    [WS][WS][WS]trial[WS][WS][WS][WS]message[WS][CR]
    will accept the strings "trial" and "message". All the WS (white spaces) characters will be ignored.
    I think, scanf() does not thread a WS as a charater when it is the first.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's also important to note that scanf is considered 'line buffered' input. That means nothing really happens until you hit enter.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Problem with Strings, Please help!
    By varus in forum C++ Programming
    Replies: 8
    Last Post: 11-27-2006, 10:47 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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