Segmentation Fault!

This is a discussion on Segmentation Fault! within the C Programming forums, part of the General Programming Boards category; I keep getting this error for some reason... Code: #include <stdio.h> int main (void) { //local declaratives int input1; int ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    5

    Angry Segmentation Fault!

    I keep getting this error for some reason...

    Code:
    #include <stdio.h>
    
    int main (void)
    {
         //local declaratives
         int     input1;
         int     input2;
         int     sum;
         int     quotient;
         double     half1;
         double     half2;
         int     remainder;
         double     fraction;
    
         printf("\nCreated by: Coach Ortiz");
         printf("\nWelcome! Please enter two integers and press <return>\n");
         printf("\n\n");
         //user input
         scanf("%d %d", &input1, &input2);
    
         sum = input1 + input2; //statements
         quotient = input1 / input2;
         remainder = input1 % input2;
         half1 = input1 / 2;
         half2 = input2 / 2;
         fraction = input1 * input2;
    
         printf("\n%20s%20s", "Description", "Data"); //output
         printf("\n%20s%20s", "-----------", "----");
         printf("\n%20s%20s", "Input1",input1);
         printf("\n%20s%20s", "Input2",input2);
         printf("\n%20s%20s", "Sum",sum);
         printf("\n%20s%20s", "Quotient",quotient);
         printf("\n%20s%20s", "Remainder",remainder);
         printf("\n%20s%20s", "Half of Input 1",half1);
         printf("\n%20s%20s", "Half of Input 2",half2);
         printf("\n520s%20s", "Fraction", fraction);
    return 0;
    }

  2. #2
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    The second arguments to these printf are int / double and you are printing them with %s ??

    Read this:
    RMS's gdb Tutorial

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    Yea im new to C programing... Is there an easier way to print this? Im no longer getting segmentation errors, but the information isnt printing properly.

  4. #4
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Check this out and do read the GDB debugger manual I pointed to in the above post.
    Input/Output Formatting in C

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    5
    ok thank you very much, the information now prints, however the doubles need to be right aligned when it prints. How do you do this?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In printf(" %- left justifies numbers, %Nd puts the integer in a field N digits wide, printing them in the far right hand side of the field, by default. %N.2lf, puts the double in a field N digits wide, adding a decimal point and two digits afterward. Zero's are added if the double has no fractional part.

  7. #7
    Noob AnishaKaul's Avatar
    Join Date
    Jan 2010
    Location
    Gurgaon, India
    Posts
    115
    Quote Originally Posted by coachortiz View Post
    ok thank you very much, the information now prints, however the doubles need to be right aligned when it prints. How do you do this?
    You re welcome , but I am not very much well versed or interested in "formatting" issues, see the above post !

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    This should have been debugged before getting anywhere near the debugger.

    Code:
    $ gcc -W -Wall foo.c
    foo.c: In function ‘main’:
    foo.c:30: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:31: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:32: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:33: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:34: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘int’
    foo.c:35: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘double’
    foo.c:36: warning: format ‘%20s’ expects type ‘char *’, but argument 3 has type ‘double’
    foo.c:37: warning: too many arguments for format
    Turn on as many warnings as you can find, and don't run code until the warnings are at zero.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why am I getting segmentation fault on this?
    By arya6000 in forum C++ Programming
    Replies: 6
    Last Post: 10-12-2008, 06:32 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, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

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