Thread: input a string in gcc

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 04:13 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Basic C Programming Help Needed
    By Smurphygirlnz in forum C Programming
    Replies: 8
    Last Post: 09-26-2002, 07:12 PM