Thread: sscanf question

  1. #1
    Registered User
    Join Date
    Feb 2008

    Question sscanf question

    Hi All,

    I wonder how sscanf can take the width of a string as a vaiable. For example, in the following sscanf function call:

    sscanf(buf, "%s", name);

    if name is defined as char name[SIZE], how can we put SIZE in the above sscanf call , instead of putting an integer value?


  2. #2
    Registered User
    Join Date
    Oct 2006
    A quick search brings up thsi nice solution here
    Quote Originally Posted by rmat_string_If_possible_how
    You can't specify a variable field with a fixed format string, but you can get around this by making the format string variable:
     int width;
     char format[20];  /* or whatever size is appropriate */
     int value;
     sprintf(format, "%%%dd", width); /* generates a string like "%5d" */
     scanf(format, &value);
    This example is for "scanf", but of course can be applied to "sscanf".

  3. #3
    Registered User
    Join Date
    Feb 2010

    Re: sscanf question

    yeah you can give like,

    sprintf(format, "%%%ds", SIZE);
    sscanf(buf, format, name);
    If you didn't specify the length also name will store only SIZE no. of characters.
    If free location is there it will point remaining characters also. So that you can view the all characters while printing that string.
    Last edited by sganesh; 03-05-2010 at 10:36 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > sprintf(format, "%%%ds", SIZE);
    It's not quite so simple...
    #include <stdio.h>
    #include <string.h>
    int main ( ) {
      char  buff[20];
      scanf( "%10s", buff );
      printf( "Buff=%s, len=%d\n", buff, strlen(buff) );
      return 0;
    $ gcc foo.c
    $ ./a.out 
    Buff=this_strin, len=10
    If you simply use the buffer size, then that is how many characters will be read - EXCLUDING the \0.
    The \0 would go beyond the size and result in a buffer overflow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2008
    So I can just do the below:

    sprintf(format, "%%%ds", SIZE-1);
    sscanf(buf, format, name);

    Thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sscanf() question
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 02-28-2009, 02:57 PM
  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. sscanf question
    By Little_Dump in forum C Programming
    Replies: 5
    Last Post: 10-27-2003, 02:16 PM
  4. Dumb Question: What is sscanf?
    By KingZoolerius66 in forum C Programming
    Replies: 3
    Last Post: 10-04-2003, 08:19 PM
  5. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM