Thread: scanf - data is "put back" - screws up next scanf

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    scanf - data is "put back" - screws up next scanf

    Just starting to learn C - trying to figure out what this means exactly - "When scanf encouters a character that can't be part of the current item (in this case an array), the character is Put Back to be read again during the scanning of the next input item or during the next call of scanf. "

    I wrote this program to see what would happen if i entered too many numbers on the same line before hitting the enter key. I then did two more scanf() functions and print() and the data that was "put back" because i entered 12 integers instead of only 10 automatically is printed out. I'm just trying to understand how scanf works with the "buffer" that it goes to. Any insight into how this works would be great. I've read that leaving a space before the conversion specifier (%d here) is necessary because if you don't the "newline" character will be read by scanf the next time it's called.

    /***********************************************/

    Code:
    #include<stdio.h>
    
    main()
    {
    	int a, b, c, d[10];
    
    		printf( "Please enter 10 numbers: \n" );
    	
    	for ( b = 0; b < 10; b++)
    	{
    		scanf( " %d", &d[b] );
    	}
    	
    	printf( "Here are the numbers you entered: \n\n" );
    
    	for ( b = 0; b < 10; b++)		
    		printf( "%d ", d[b] );
    
    	printf( "\n\nPlease enter another number: " );
    	scanf( "  %d", &a );
    
    	printf( "\n\n %d \n\n", a );
    
    		printf( "\n\nPlease enter another number: " );
    	scanf( " %d", &c );
    
    	printf( "\n\n %d \n\n", c );
    	
    	return 0;
    }
    Last edited by voltson; 10-09-2002 at 07:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Replies: 3
    Last Post: 04-18-2008, 10:06 AM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM