format specifiers

This is a discussion on format specifiers within the C Programming forums, part of the General Programming Boards category; What is the format specifier for strings? Specifically, why does this not work? Code: #include <stdio.h> char name; int main() ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    8

    format specifiers

    What is the format specifier for strings?
    Specifically, why does this not work?

    Code:
    #include <stdio.h>
    
    char name;
    
    int main()
    {
    	printf("Enter your name:\n");
    	scanf("%s", &name);
    	printf("Hi, %s!", name);
    	getchar();
    	return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well you have declared a char and trying to read a string. What else you can expect.

    Code:
    #include <stdio.h>
    
    int main()
    {
            char name[20];
    
            printf("Enter your name:\n");
            /* Dont use scanf to read string, use fgets() */
            scanf("&#37;s", &name);  
            fgets(name, sizeof(name), stdin);
    
            printf("Hi, %s!", name);
    
            getchar();
            return 0;
    }
    ~ And dont use global variables ~

    ssharish2005
    Last edited by ssharish2005; 08-28-2007 at 05:53 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    scanf("&#37;s", name);
    would suffice, but as you say, fgets() would be a better choice.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    8
    thanks for the help guys

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Even better how about:

    Code:
    scanf("%19s", name);
    To prevent possible overflow?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    It would, but maintaining the format width in response to changes in the size of the buffer is hard work.

    Also, the fact that the width does NOT count the trailing \0, but one is stored anyway is another trap for the unwary.

    Then there's the whole "flush input" problem which comes with using scanf with other input methods to deal with.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    You could always do the following:

    Code:
    static const int MAX_SIZE = 20;
    ...
    char name[MAX_SIZE] = {0};
    ...
    scanf ("%.*s", MAX_SIZE-1, name);
    If you need to increase the size of your buffer, there's only one place that you need to alter and you're protected against overflow.

    QuantumPete

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by QuantumPete View Post
    You could always do the following:

    Code:
    static const int MAX_SIZE = 20;
    ...
    char name[MAX_SIZE] = {0};
    ...
    scanf ("%.*s", MAX_SIZE-1, name);
    If you need to increase the size of your buffer, there's only one place that you need to alter and you're protected against overflow.

    QuantumPete
    That doesn#t solve the other problems with scanf, such as "it quits inputting strings at whitespace after the first non-whitespace". fgets() is definitely better at this.

    --
    mats

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Except it doesn't work at all.
    Code:
    $ gcc -W -Wall -ansi -pedantic foo.c
    foo.c: In function `main':
    foo.c:6: warning: unknown conversion type character `.' in format
    foo.c:6: warning: too many arguments for format
    $ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( ) {
        char buff[] = "abcdefghijklmnopqrstuvwxyz";
        scanf("&#37;.*s", 10, buff);
        printf("%s\n", buff);
        return 0;
    }
    $ ./a.exe
    thisisareallylongword
    abcdefghijklmnopqrstuvwxyz
    $
    A * in a scanf call is assignment suppression, not take the next numeric parameter and substitute the width.
    "%*d" for example would parse an integer, but it would not assign the result anywhere. It's use is in skipping fields of input.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Salem View Post
    A * in a scanf call is assignment suppression, not take the next numeric parameter and substitute the width.
    "%*d" for example would parse an integer, but it would not assign the result anywhere. It's use is in skipping fields of input.
    My mistake. I was thinking of printf format specifiers ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. format specifiers
    By Itanium in forum C# Programming
    Replies: 1
    Last Post: 03-26-2007, 08:31 AM
  2. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  3. wsprintf and format specifiers
    By incognito in forum Windows Programming
    Replies: 2
    Last Post: 01-03-2004, 10:00 PM
  4. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 01:51 PM
  5. gcc/borland and sscanf format specifiers
    By Sargnagel in forum C Programming
    Replies: 2
    Last Post: 01-24-2003, 06:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21