Thread: sscanf() help in c

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    21

    sscanf() help in c

    please help me with sscanf() function detailing with an example..

    Also try to help me with this program
    Code:
    #include<stdio.h>
    void main()
    {
     char buffer[50];
     int no=97;
     double val=2.34174;
     char name[10]="Shewta";
     clrscr();
     sprintf(buffer,"%d %lf %s",no,val,name);
     printf("\n%s",buffer);
     sscanf(buffer,"%4d %2.2lf %s",&no,&val,name);
     printf("\n%s",buffer);
     printf("\n%d %lf %s",no,val,name);
     getch();
    }
    Also please tell what format specifiers like (4 and 2.2 in above program) do in scanf() funtion.I know about their usage in printf() but unable to understand in scanf().

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in printf you should use %f not %lf
    sscanf does the same as scanf just as input stream uses not the stdio but supplied buffer

    width in scanf format could be used to specify number of chars to convert
    http://msdn.microsoft.com/en-us/library/xdb9w69d.aspx
    I'm not familiar with availability of precision in scanf format
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    And what vart means by that last sentence is that precision is not available in scanf format (%2.2lf could do a lot of things, deciding on how the compiler decides to parse it, but the one thing it cannot do is read a two-character double with two decimal digits of precision) -- you can do %5lf, meaning "read at most five characters", but that's it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf and string handling question
    By ursula in forum C Programming
    Replies: 14
    Last Post: 05-30-2009, 02:21 AM
  2. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  4. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 07:52 PM
  5. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 08:46 AM