Re: printf scanf problem

Discussion in 'C Programming' started by Dan Pop, Aug 4, 2003.

  1. Dan Pop

    Dan Pop Guest

    In <bgg40n$emg$> "herrcho" <> writes:

    >Hi ~ i started to learn C a few weeks ago.
    >and i hope i can get much help from this group.


    You can, especially if you show some efforts of solving the problem
    yourself, before asking for help.

    >#include <stdio.h>


    #include <string.h>

    Needed by strlen, which doesn't return int. If you use *any* str or mem
    function, don't forget to include <string.h>.

    >main()


    It's better to spell it: int main(), the implicit return type of int is an
    obsolete feature of the language.

    >{
    > char name[50];
    >
    > printf("Input a string\n");
    > scanf("%s",name);


    if (scanf("%49s", name) < 1) /* bad user input */

    You must protect yourself against user abuses: too much input or none at
    all.

    > printf("%s has %d byte length \n", name, strlen(name));


    printf("'%s' has %d byte length \n", name, (int)strlen(name));

    strlen() does NOT return int, but %d expects int! The *explicit*
    conversion is necessary. You also want to delimit the user input from your
    own text, hence the apostrophes.

    return 0;

    You have defined main() as returning int, so it's better to return an
    int value. 0 means: the program has successfully terminated.

    >}
    >
    >i've got gcc compiler.


    Then, you should write the code in such a way that gcc -Wall -O -ansi
    -pedantic doesn't produce any warning!

    >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'
    >
    >Could anyone help me ?


    I'm sure your C book could! %s starts by discarding any preceding white
    space characters, then reads characters up to the first white space
    character, which is left unprocessed for the next conversion descriptor.
    Which is precisely the behaviour you have observed.

    Reading strings containing white space characters with scanf requires one
    of the most advanced features of scanf: scan sets. A scan set specifies
    either all the characters allowed in the string or all the characters not
    allowed in the string (if its first character is ^). In your case, you
    want everything up to the newline character that terminates the input
    line, so the scan set is [^\n] and the complete format string "%49[^\n]".

    That gives the following program:

    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char name[50 + 1];

    printf("Input a string [max 50 characters]\n");
    if (scanf("%50[^\n]", name) < 1)
    printf("Bad user input.\n");
    else
    printf("'%s' has %d byte length\n", name, (int)strlen(name));
    return 0;
    }

    It compiles cleanly with the gcc invocation recommended above and accepts
    any user input, ignoring the excess characters (if any).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 4, 2003
    #1
    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. code_wrong

    Re: printf scanf problem

    code_wrong, Aug 2, 2003, in forum: C Programming
    Replies:
    1
    Views:
    416
    /dev/null
    Aug 2, 2003
  2. sushant
    Replies:
    4
    Views:
    9,054
    Chris Torek
    Jan 10, 2005
  3. Dawn Minnis
    Replies:
    6
    Views:
    489
    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,400
    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:
    680
    Richard Bos
    May 2, 2006
Loading...

Share This Page