input a string in gcc

This is a discussion on input a string in gcc within the C Programming forums, part of the General Programming Boards category; following is the code to accept a string from user, but i get segmentation fault as soon i have finished ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    2

    input a string in gcc

    following is the code to accept a string from user, but i get segmentation fault as soon i have finished entering a sentence i.e. as soon as i press the 'enter' key.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main (void)
    {
    	char str[50];
    	printf ("Enter string:\n");
    	fgets (str, sizeof str, stdin);
            //further code
    }
    if anybody can help me enter the string, thanx!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Is that the actual program you tried?

    Or just something which you think looks like the program you tried?

    Because as it stands, I can't see any reason why those 3 lines in a main() would cause a segfault.

    So please post something which actually crashes.
    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 support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    I guessing it's the "further code" part

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    2
    Well i didn't consider further code important because i had already
    used a printf("test") statement immediately after fgets & it's not
    getting executed. I get seg fault eror the instant i hit the enter
    key.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,401
    Well i didn't consider further code important because i had already
    used a printf("test") statement immediately after fgets & it's not
    getting executed. I get seg fault eror the instant i hit the enter
    key.
    That's interesting. Try the code below and see if you get a seg fault:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        char str[50];
        printf("Enter string:\n");
        fgets(str, sizeof str, stdin);
        printf("&#37;s\n", str);
        return 0;
    }
    If you do not get a seg fault, then something else must be wrong with your original code, so you should post the smallest and simplest program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    printf("test");
    Most likely wouldn't work, since you'd put it in the stdout buffer (which probably won't get flushed). Then you run the code that segfaults and it never gets flushed/printed. Bit like:

    Code:
    enter string
    put 'test' in the output buffer
    segfault
    Change it to:
    Code:
    printf("test\n");
    or
    Code:
    printf("test");
    fflush(stdout);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - String input header
    By Nextstopearth in forum C Programming
    Replies: 10
    Last Post: 06-10-2009, 11:25 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM

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