Thread: questions about scanf

  1. #1
    Registered User kirtikjr's Avatar
    Join Date
    Nov 2008
    Posts
    4

    questions about scanf

    plz tell me what does line means:

    scanf ("%[^\n]s",char);

    and some variants like: "%*s" and "%*s%s"
    plz correct if any thing is wrong..

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think this is a bad scanf line:

    scanf ("%[^\n]s",char);

    and there are other reasons besides.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by kirtikjr View Post
    plz tell me what does line means:

    scanf ("%[^\n]s",char);

    and some variants like: "%*s" and "%*s%s"
    plz correct if any thing is wrong..
    I presume you would already know the purpose of scanf
    [^\n] implies to accept every character except newline character '\n'
    in the string named char(note that keywords are not allowed as variable names);
    It is called as circumflex if i remember correctly.

    %*s is assignment suppression operator.
    A string is read from the console but not stored in any variable.

    BTW,When using scanf to read strings you should be careful and specify the size of string in
    the format string to avoid buffer overflow exploits.
    Below link explains how to use scanf:
    http://cboard.cprogramming.com/showp...37&postcount=9
    Last edited by stevesmithx; 11-30-2008 at 06:17 AM. Reason: add link
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Ooops! Thanks steve.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    you're welcome.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    Registered User kirtikjr's Avatar
    Join Date
    Nov 2008
    Posts
    4
    Quote Originally Posted by stevesmithx View Post
    I presume you would already know the purpose of scanf
    [^\n] implies to accept every character except newline character '\n'
    in the string named char(note that keywords are not allowed as variable names);
    It is called as circumflex if i remember correctly.
    Generally scanf can take only single word of input. it wont take multi-word input.
    scanf("%[^\n]s", char1) > allows us to input multiple words to char1. (in my first post i have used char just to stress on the character inputs, i did not mean to use any keyword as variable..)...
    But masking \n should allow the compiler to ignore only \n input, but when we press Enter the input ends. Whereas, it allows the compiler to mask any space(space, tab) and we can store multiple words. can u plz tell me why is it so?

    Quote Originally Posted by stevesmithx View Post
    %*s is assignment suppression operator.
    A string is read from the console but not stored in any variable.
    Also can u give a small example where this is used?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kirtikjr View Post
    Generally scanf can take only single word of input. it wont take multi-word input.
    scanf("%[^\n]s", char1) > allows us to input multiple words to char1. (in my first post i have used char just to stress on the character inputs, i did not mean to use any keyword as variable..)...
    But masking \n should allow the compiler to ignore only \n input, but when we press Enter the input ends. Whereas, it allows the compiler to mask any space(space, tab) and we can store multiple words. can u plz tell me why is it so?


    Also can u give a small example where this is used?
    Because that's what scansets do, they match the largest thing that fits the criterion. So all characters until a \n is read. It needs to then have an s, afterward, although that probably doesn't matter much since there's nothing after it.

  8. #8
    Registered User kirtikjr's Avatar
    Join Date
    Nov 2008
    Posts
    4
    thanks...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. A very long list of questions... maybe to long...
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-16-2007, 05:36 AM
  3. Trivial questions - what to do?
    By Aerie in forum A Brief History of Cprogramming.com
    Replies: 23
    Last Post: 12-26-2004, 09:44 AM
  4. 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
  5. Float/double compiler error and TONS of questions!
    By musayume in forum C Programming
    Replies: 5
    Last Post: 10-24-2001, 01:40 PM