Why it doesn't work?
I'm trying to read untill the end of the buffer but it even finishes to scan as far as I've checked.Code:#include<stdio.h> int main(){ char string[30]; scanf("%[^\n]\n",&string); printf("%s",string); return 0; }
Why it doesn't work?
I'm trying to read untill the end of the buffer but it even finishes to scan as far as I've checked.Code:#include<stdio.h> int main(){ char string[30]; scanf("%[^\n]\n",&string); printf("%s",string); return 0; }
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."
Keeps doesn't working Well Ive re-compiled with GCC using -Wall and got a bunch of warnings.Originally Posted by Exile
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?
I used the following code:
compiled as:Code:#include <stdio.h> int main(){ char string[30]; scanf("%[^\n]\n",string); printf("%s",string); return 0; }
And didn't get any errors during the compile, no warnings either. I'm using MinGW if that makes a difference.Code:gcc test.c -Wall
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 :
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().Code:#include <stdio.h> int main(){ char string[30]; scanf("%[^\n]",string); printf("\n%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."
Weeeeeeeeeee it works now! thanks a lot for clearing my doubt!Originally Posted by Exile
http://www.eskimo.com/~scs/c-faq/q12.17.htmlPerhaps 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.*
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.
I got it:Originally Posted by Salem
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)