This is a discussion on Segfault within the C Programming forums, part of the General Programming Boards category; This code compiles fine but when I run it I'm getting a segmentation Fault. I'm not sure why. The program ...

  1. #1
    Registered User
    Join Date
    Apr 2009


    This code compiles fine but when I run it I'm getting a segmentation Fault. I'm not sure why. The program is suppose to Add two numbers without using the + operator.

    #include <stdio.h>
    #include <stdlib.h>
    int add(int a,int b);
    int main( int argc, char *argv[] ){
      int x;
      int y;
      x = atoi(argv[1]);
      y = atoi(argv[2]);
      printf("The sum of x and y is %d", add(x,y));
      return 0;
    int add(int a, int b){
      if( a == b) return (a);
      return(add( ++a , --b ) );

    Any Help would be greatly appreciated.

  2. #2
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    in front of my computer
    the way i see it, when you do this
    return(add( ++a , --b ) );
    theres no end to this recursive call.for eg when 3 and 2 are to be added, then this recursive call has no end because a is never getting equal to b.

  3. #3
    Registered User
    Join Date
    Jun 2005
    There is also the slight inconvenience that incrementing is an addition operation (as is decrementing, with somewhat more scope for liberal interpretation).

  4. #4
    "I Win!" by U. Lose vart's Avatar
    Join Date
    Oct 2006
    Rishon LeZion, Israel
    instead of
    if( a == b) return (a);

    should be

    if( 0 == b) return (a);

    also it is good idea check that b >=0 before processing
    To be or not to be == true

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    And even if the code works for small numbers, it will NEVER work for large numbers (about half a million or so would be the max, depending a little bit on the processor, OS and optimization levels [unless the compiler figures out that it's tail recursion and optimizes the recursion away completely - which is possible].

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Maz is offline
    Registered User Maz's Avatar
    Join Date
    Nov 2005
    Yes. Too deep reqursion will get you in troubles. Hence I would suggest you to think bitwise operations in loop. Basically you can do the summing with AND, XOR and shift to left.

    I'll leave the rest for you to ponder

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 08:13 AM
  2. Segfault with additional variable?
    By misterFry in forum C++ Programming
    Replies: 11
    Last Post: 11-12-2008, 10:55 AM
  3. malloc() resulting in a SegFault?!
    By cipher82 in forum C++ Programming
    Replies: 21
    Last Post: 09-18-2008, 12:24 PM
  4. use of printf prevents segfault!
    By MK27 in forum C Programming
    Replies: 31
    Last Post: 08-27-2008, 01:38 PM
  5. Segfault and Warning help
    By Uncle Rico in forum C Programming
    Replies: 1
    Last Post: 03-25-2005, 02:51 PM

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