Thread: Scanf Doubt

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    277

    Scanf Doubt

    Why it doesn't work?

    Code:
    #include<stdio.h>
    
    
    int main(){
    char string[30];
    	
    scanf("%[^\n]\n",&string);
    printf("%s",string);
    	return 0;
    }
    I'm trying to read untill the end of the buffer but it even finishes to scan as far as I've checked.

  2. #2
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    You don't need to put the & in fron of arrays.
    Code:
    int main(){
    char string[30];
        
    scanf("%[^\n]\n",string);
    printf("%s",string);
        return 0;
    }
    So, do you understand everything you know about this yet?

    "Begin at the beginning," the King said, very gravely, "and go on till you come to the end; then stop."

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Exile
    You don't need to put the & in fron of arrays.
    Code:
    int main(){
    char string[30];
        
    scanf("%[^\n]\n",string);
    printf("%s",string);
        return 0;
    }
    Keeps doesn't working Well Ive re-compiled with GCC using -Wall and got a bunch of warnings.

    Code:
    collect2: ld terminated with signal 11 [Falha de segmentação]
    scanbuffer(.rodata+0x0):/build/buildd/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crtn.S:23: multiple definition of `_fp_hw'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.rodata+0x0):../sysdeps/i386/elf/start.S:47: first defined here
    scanbuffer(.data+0x4): In function `__data_start':
    : multiple definition of `__dso_handle'
    /usr/lib/gcc-lib/i486-linux/3.3.4/crtbegin.o(.data+0x0): first defined here
    scanbuffer(.init+0x0): In function `_init':
    /build/buildd/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crti.S:32: multiple definition of `_init'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crti.o(.init+0x0):/build/buildd/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crti.S:11: first defined here
    scanbuffer(.text+0x0): In function `_start':
    ../sysdeps/i386/elf/start.S:47: multiple definition of `_start'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.text+0x0):../sysdeps/i386/elf/start.S:47: first defined here
    scanbuffer(.fini+0x0): In function `_fini':
    /build/buildd/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crti.S:47: multiple definition of `_fini'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crti.o(.fini+0x0): first defined herescanbuffer(.text+0x4b): In function `__i686.get_pc_thunk.bx':
    /build/buildd/glibc-2.3.2.ds1/build-tree/i386-libc/csu/crtn.S:35: multiple definition of `__i686.get_pc_thunk.bx'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crti.o(.gnu.linkonce.t.__i686.get_pc_thunk.bx+0x0): first defined here
    scanbuffer(.got+0x0): multiple definition of `_GLOBAL_OFFSET_TABLE_'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.got.plt+0x0):../sysdeps/i386/elf/start.S:47: first defined here
    scanbuffer(.rodata+0x4): multiple definition of `_IO_stdin_used'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.rodata+0x4):../sysdeps/i386/elf/start.S:53: first defined here
    scanbuffer(.data+0x0): In function `__data_start':
    : multiple definition of `__data_start'
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.data+0x0):../sysdeps/i386/elf/start.S:47: first defined here
    scanbuffer.o(.text+0x0): In function `main':
    : multiple definition of `main'
    scanbuffer(.text+0xc4): first defined here
    /usr/lib/gcc-lib/i486-linux/3.3.4/../../../crt1.o(.dynamic+0x0):../sysdeps/i386/elf/start.S:47: multiple definition of `_DYNAMIC'
    scanbuffer(.dynamic+0x0): first defined here

    is it a problem with the name Ive selected?

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    77
    I used the following code:
    Code:
    #include <stdio.h>
    
    int main(){
    char string[30];
        
    scanf("%[^\n]\n",string);
    printf("%s",string);
        return 0;
    }
    compiled as:
    Code:
    gcc test.c -Wall
    And didn't get any errors during the compile, no warnings either. I'm using MinGW if that makes a difference.

    I did find a problem with that scanf() statement. Scanf is a little funny about the formatting that surrounds the text to take in. With that trailing \n it was waiting for the users to press return twice before returning any value.

    If you change the code to :
    Code:
    #include <stdio.h>
    
    int main(){
    char string[30];
        
    scanf("%[^\n]",string);
    printf("\n%s",string);
        return 0;
    }
    You won't have that problem. Just remember that scanf() will echo the final return that the user types so you don't have to include it in your output. With the code I just gave you end up with a blank line (two \n's) between what the user types and the output from the printf(). 1 \n from the user hitting return, the second from the printf().
    So, do you understand everything you know about this yet?

    "Begin at the beginning," the King said, very gravely, "and go on till you come to the end; then stop."

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Exile
    I used the following code:
    Code:
    #include <stdio.h>
    
    int main(){
    char string[30];
        
    scanf("%[^\n]\n",string);
    printf("%s",string);
        return 0;
    }
    compiled as:
    Code:
    gcc test.c -Wall
    And didn't get any errors during the compile, no warnings either. I'm using MinGW if that makes a difference.

    I did find a problem with that scanf() statement. Scanf is a little funny about the formatting that surrounds the text to take in. With that trailing \n it was waiting for the users to press return twice before returning any value.

    If you change the code to :
    Code:
    #include <stdio.h>
    
    int main(){
    char string[30];
        
    scanf("%[^\n]",string);
    printf("\n%s",string);
        return 0;
    }
    You won't have that problem. Just remember that scanf() will echo the final return that the user types so you don't have to include it in your output. With the code I just gave you end up with a blank line (two \n's) between what the user types and the output from the printf(). 1 \n from the user hitting return, the second from the printf().
    Weeeeeeeeeee it works now! thanks a lot for clearing my doubt!

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    http://www.eskimo.com/~scs/c-faq/q12.17.html
    Perhaps surprisingly, \n in a scanf format string does not mean to expect a newline, but rather to read and discard characters as long as each is a whitespace character.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Nothing wrong with your code, but there is something wrong with your compiler.
    It looks like you've got two versions installed for some reason.

    Try
    gcc -v
    to get the version (paste the result here).

    Also try compiling the "hello world" and see what you get from that. Paste full command line and output if it doesn't work.
    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.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    277
    Quote Originally Posted by Salem
    Nothing wrong with your code, but there is something wrong with your compiler.
    It looks like you've got two versions installed for some reason.

    Try
    gcc -v
    to get the version (paste the result here).

    Also try compiling the "hello world" and see what you get from that. Paste full command line and output if it doesn't work.
    I got it:

    imanewbie@isengard:~ $ gcc -v
    Reading specs from /usr/lib/gcc-lib/i486-linux/3.3.4/specs
    Configured with: ../src/configure -v --enable-languages=c,c++,java,f77,pascal,objc,ada,treelang --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-gxx-include-dir=/usr/include/c++/3.3 --enable-shared --with-system-zlib --enable-nls --without-included-gettext --enable-__cxa_atexit --enable-clocale=gnu --enable-debug --enable-java-gc=boehm --enable-java-awt=xlib --enable-objc-gc i486-linux
    Thread model: posix
    gcc version 3.3.4 (Debian 1:3.3.4-9ubuntu5)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM