weird bug in the scanf command

This is a discussion on weird bug in the scanf command within the C Programming forums, part of the General Programming Boards category; Code: char array1[50]; scanf("%s", array1); printf("\nYou entered: %s\n", array1); when i enter "hello world" it puts only the word "hello" ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    weird bug in the scanf command

    Code:
    char array1[50];
    scanf("%s", array1);
    
      printf("\nYou entered: %s\n", array1);
    when i enter
    "hello world"

    it puts only the word "hello" in array1
    Last edited by transgalactic2; 10-24-2008 at 12:45 PM.

  2. #2
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    It's not a bug. scanf() by definition is only going to read up until the first space or new line.

    Check this code snippet out to see.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(int argc, char * argv[])
    {
    	char string1[20], string2[20];
    	scanf("%s %s", string1, string2);
    	
    	printf("%s %s", string1, string2);
    	
    	return 0;
    }

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Since that's what it's supposed to do, the word "bug" seems a bit over-the-top.

  4. #4
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    what happens to the next word "world"

    is it eliminated or it added to the start of the next input string?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Something like that, yes.

    You are wanting to do something like this:

    Example:
    Code:
    char line[256];
    fgets(line, sizeof(line), stdin);
    strtok(line, "\n");
    printf("You typed: \"&#37;s\"", line);

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by transgalactic2 View Post
    what happens to the next word "world"

    is it eliminated or it added to the start of the next input string?
    It's held internally by the system in a keyboard buffer (aka the STDIN buffer for all intensive purposes) for your next scanf() or other read function.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    puts reads all the inputted string

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    puts doesn't read anything. If you mean fgets, then yes.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i meant gets
    sorry

    gets reads every thing?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    fgets, yes.
    gets is an entirely different function, which is dangerous and bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how its bad?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    gets doesnt know the size of the inputed string so it ma cause bufferoverflow

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,768
    Yes, it may. And buffer overflows are bad.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 10-06-2007, 01:25 PM
  2. Problem with RPC and scanf
    By Ricardo_R5 in forum C Programming
    Replies: 11
    Last Post: 01-08-2007, 05:15 PM
  3. Scanf bug??
    By nymph in forum C Programming
    Replies: 7
    Last Post: 03-17-2004, 08:13 PM
  4. Weird bug
    By Hunter2 in forum Windows Programming
    Replies: 1
    Last Post: 08-12-2002, 03:45 PM
  5. Weird bug in the forum
    By SilentStrike in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-16-2002, 05:16 AM

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