Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Oct 2003

    Segmentation Fault

    I am new at C, and I made a program to test out my newly acquired knowledge of functions. Here is what the code looks like:

    /* This is to test functions, and hopefully comparitive/logical operators */
    	char reply;
    	int output;
    	printf("Do you want to (a)dd or (s)ubtract the numbers 5 and 4?");
    	switch (reply) {
    	case 'A':
    		output = Add();
    	case 'S':
    		output = Subtract();
    		printf("Input Error. Use the letters A or S next time.");
    	printf("Done. %d",output);
    int Add()
    	int output;
    int Subtract()
    	int output;
    This code compiles fine, with no errors or warnings. Then, when I run it and type in A or S at the printf statement, it says "Segementation Fault". I have no idea what that means, but I get it a lot on programs. Before, I just gave up, but now it is getting annoying and I want to know what I'm doing incorrectly.

    I am using GCC to compile the program, GVIM to program it, and Debian GNU/Linux as my operating system.

  2. #2
    Join Date
    Sep 2003
    reply isn't a pointer, which scanf expects, so you need to pass the address:
    scanf("%c", &reply);
    The information given in this message is known to work on FreeBSD 4.8 STABLE.
    *The above statement is false if I was too lazy to test it.*
    Please take note that I am not a technical writer, nor do I care to become one.
    If someone finds a mistake, gleaming error or typo, do me a favor...bite me.
    Don't assume that I'm ever entirely serious or entirely joking.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    note: be careful with scanf("%c",&whatever); because in usual cases, the carriage return is left in the input buffer.

    another thing:

    please use int main()
    otherwise you'll be flamed to death.
    int main()
      //stuff goes here
      return 0;
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Registered User
    Join Date
    Sep 2003

    With Scanf

    Point to note!!
    Whenever you are using Scanf(), remember it's a function, you must include the address operator with the variable..e.g

    scanf("%d", &var_name);
    where var_name represents your variable name..

    since scanf is deals with input , it need to know the address of whatever it's working with...which you get by using &

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM