Thread: Problem with string

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    1

    Problem with string

    Hi guys,

    I have tried running this code.

    Code:
    #include <stdio.h>
    #include <conio.h>
    int main(){
    	char name[20];
    	printf("Enter name: ");
    	scanf_s("%s", name);
    	printf("Your name is %s.", name);
    	_getch();
    	return 0;
    }
    However, I get this output.

    Enter name: abcd
    Your name is .

    Why is the output blank space?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Because you didn't read the manual page for scanf_s before using it.
    scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l

    You will note that you need another parameter - one telling the function the size of the buffer being passed.
    Since you didn't say anything (and your compiler didn't warn you either), you're left scratching your head and posting on forums.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    You may also want to check out the less 'Windows Centric' sscanf and fgets.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by FloridaJo View Post
    You may also want to check out the less 'Windows Centric' sscanf and fgets.
    It is true that sscanf_s() and similar functions were introduced by Microsoft, but they are also now defined in Annex K of the 2011 C standard. So characterising them as "windows centric" is both unfair and outdated.

    (Admittedly, it is not mandatory that an C11 implementation support Annex K. However, it is not difficult to do so, so most hosted C11 implementations probably will.)
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by grumpy View Post
    It is true that sscanf_s() and similar functions were introduced by Microsoft, but they are also now defined in Annex K of the 2011 C standard. So characterising them as "windows centric" is both unfair and outdated.

    (Admittedly, it is not mandatory that an C11 implementation support Annex K. However, it is not difficult to do so, so most hosted C11 implementations probably will.)

    Okay, on a Mac then under what library do they reside?

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by FloridaJo View Post
    Okay, on a Mac then under what library do they reside?
    In the draft standard, these are listed in <stdio.h> (see Annex B.20: http://www.open-std.org/jtc1/sc22/wg...docs/n1570.pdf)

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Quote Originally Posted by Matticus View Post
    In the draft standard, these are listed in <stdio.h> (see Annex B.20: http://www.open-std.org/jtc1/sc22/wg...docs/n1570.pdf)
    My compiler doesn't recognize scanf_s.

    Problem with string-scanf-png
    Last edited by FloridaJo; 10-11-2014 at 04:23 PM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by FloridaJo View Post
    My compiler doesn't recognize scanf_s.

    Problem with string-scanf-png
    Neither does mine.

    Though as grumpy pointed out, it's both C11 and not mandatory, so it's not surprising that our compilers don't support it.

    I think the point was that it has been recognized by recent standards, and as such is technically fair game with regard to the standard (i.e. no longer strictly OS specific), though not necessarily widely adopted in implementation.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    161
    Okay, gotcha. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-01-2013, 10:11 PM
  2. Replies: 0
    Last Post: 04-27-2013, 06:36 AM
  3. Replies: 1
    Last Post: 04-27-2013, 04:36 AM
  4. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM