Re: printf scanf problem

Discussion in 'C Programming' started by code_wrong, Aug 2, 2003.

  1. code_wrong

    code_wrong Guest

    "herrcho" <> wrote in message
    news:bgg40n$emg$...
    > Hi ~ i started to learn C a few weeks ago.
    > and i hope i can get much help from this group.
    >
    > #include <stdio.h>
    > main()
    > {
    > char name[50];
    >
    > printf("Input a string\n");
    > scanf("%s",name);
    > printf("%s has %d byte length \n", name,
    > strlen(name));
    > }
    >
    > i've got gcc compiler.
    > when i run the above code, and enter
    > 'I am learning C'
    > the output is 'I has 1 byte length' .
    > i expected 'I am learning C has 15 byte length'


    scanf() stops reading at the space, effectively turning your string into
    several strings, one for each word. You have only provided scanf() with a
    pointer to one string.
    You should look into sscanf() or fgets()
     
    code_wrong, Aug 2, 2003
    #1
    1. Advertising

  2. code_wrong

    /dev/null Guest

    >> Hi ~ i started to learn C a few weeks ago.
    >> and i hope i can get much help from this group.
    >>
    >> #include <stdio.h>
    >> main()
    >> {
    >> char name[50];
    >>
    >> printf("Input a string\n");
    >> scanf("%s",name);
    >> printf("%s has %d byte length \n", name,
    >> strlen(name));
    >> }
    >>
    >> i've got gcc compiler.
    >> when i run the above code, and enter
    >> 'I am learning C'
    >> the output is 'I has 1 byte length' .
    >> i expected 'I am learning C has 15 byte length'

    >
    > scanf() stops reading at the space, effectively turning your string into
    > several strings, one for each word. You have only provided scanf() with a
    > pointer to one string.
    > You should look into sscanf() or fgets()
    >


    It might also help if there was a "#include<string.h>" somewhere in there
    too.
     
    /dev/null, Aug 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dan Pop

    Re: printf scanf problem

    Dan Pop, Aug 4, 2003, in forum: C Programming
    Replies:
    0
    Views:
    663
    Dan Pop
    Aug 4, 2003
  2. sushant
    Replies:
    4
    Views:
    9,320
    Chris Torek
    Jan 10, 2005
  3. Dawn Minnis
    Replies:
    6
    Views:
    519
    Lawrence Kirby
    Feb 8, 2005
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,515
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    703
    Richard Bos
    May 2, 2006
Loading...

Share This Page