2

I have been trying to figure out this error for almost an hour and am having trouble. I was reading this post: C compile error: Id returned 1 exit status and someone said that it could be because the program is still running. I have checked that and it is not running and I still get the error.

This is what the error looks like: C:\Program\collect2.exe [Error] ld returned 1 exit status.

I also looked at some more posts with answers such as this one: Id returned 1 exit status error in my C program and the top response was that it was a "linker error" and that the program is getting complied incorrectly. I tried creating a new project and copying the code but end up with the same error. I then went into the compiler options and I took a screenshot of the options. Do these options look okay?

enter image description here

Maybe its just an error in my code I am overlooking. I have to create a tail recursion function to add numbers. For example the user enters 4 and the function adds 4+3+2+1 to get 10.

I believe the error is in this piece of code in the while loop:

else{

        printf("Answer = %d \n", addNum(num));  
    }

When I comment that out I can run it and enter negative numbers or zero and get the output.If my program is off as in the function wont output correctly I do not want you to correct it. I will tweak that stuff later and figure the answer out for myself, I am just looking for why I am getting this error.

#include <stdio.h>
#include <stdlib.h>

int addNum(int n);
int main(int argc, char *argv[]) {

    int num;
    int count = 1;


    printf("Enter an integer greater than zero, (q to quit): \n");


    while(count < 3){

        printf("Enter a positive number: \n");
        scanf("%d", &num);

        if(num < 0){

            printf("Enter a positive number: \n");
        }
        else if(num == 0){

            printf("Answer = 0 \n");
        }
        else{

            printf("Answer = %d \n", addNum(num));  
        }


     }


    int addNum(int n){

       int answer; 

       if(n > 0){

          answer = n += addNum(n - 1);  
       }
       return answer;
    }

   return 0;
  }
Community
  • 1
  • 1
Drizzy
  • 105
  • 1
  • 1
  • 10
  • 5
    That `addNum` implementation should be *outside* of `main()`. – WhozCraig Mar 02 '17 at 04:08
  • 1
    Not the problem, but your `while` loop will never end looping – Spikatrix Mar 02 '17 at 04:10
  • And I'm pretty sure the error is shown to you somewhere before the `ld returned 1` message – Spikatrix Mar 02 '17 at 04:15
  • @CoolGuy Yeah I am going to be adding a key to enter so that the while loop quits when the user enters the key. As I said I am just using count to test the while loop. And what do you mean before the ld returned 1 message? – Drizzy Mar 02 '17 at 04:26
  • Can you look at the "Error" pane of your Visual Studio? I am sure there would be some error displayed by the linker. – Ajay Brahmakshatriya Mar 02 '17 at 04:28
  • 1
    Anyway, if you jus move the definition of addNum outside main like WhozCraig said, it should compile fine. – Ajay Brahmakshatriya Mar 02 '17 at 04:31
  • @WhozCraig That was the problem, thank you so much! Can you explain to me again why that works? From what I thought, a function can be in the main as long as you only intend to use it locally, so why did this not work? – Drizzy Mar 02 '17 at 04:31
  • @AjayBrahmakshatriya Yeah moving the function outside of the main helped. However I don't understand why it did not work before because I was using it locally. Shouldn't it work since I am using it in the main? – Drizzy Mar 02 '17 at 04:34
  • 1
    There is no notion of local functions in C. – Ajay Brahmakshatriya Mar 02 '17 at 04:36
  • 2
    Moving it worked because C doesn't support local function definitions (ie. nested functions). Frankly I'm surprised your code *compiled*. If your compiler supports this via some extension (and they're doing you no favors if they do), then the issue becomes one of location. – WhozCraig Mar 02 '17 at 04:36
  • @WhozCraig Oh I was unaware the C does not support local functions. Thanks for informing me of that. Can you elaborate on what you mean by you are surprised my code compiled? Do you mean you are surprised if it complied when I had the function inside the main? If so, it didn't I just thought that I could use local functions and that was my mistake. It only compiled after I moved the function out of the main. – Drizzy Mar 02 '17 at 04:42
  • @Drizzy You've probably got other messages besides the `ld returned 1 exit status`. That's just the linker saying "Failed to link. Goodbye". It could be due to a lot of reasons. The main error is probably somewhere in the other messages. Something like `undefined reference to \`addNum'` – Spikatrix Mar 02 '17 at 04:46
  • Yes, that's what I meant. – WhozCraig Mar 02 '17 at 04:47
  • @WhozCraig It would be better if you post an answer. – Spikatrix Mar 02 '17 at 04:50
  • @WhozCraig The error message explaining the failure is why I asked the question. I got this: "C:\Program\collect2.exe [Error] ld returned 1 exit status". Is there a way to see more detail about this error in the environment? I use Dev-C++ I am new to this IDE and C in general. I am also not sure how to look for more information regarding errors in IDE's. I have not really learned that yet. – Drizzy Mar 02 '17 at 04:52
  • @CoolGuy WhozCraig already gave me the answer. What do you mean? – Drizzy Mar 02 '17 at 04:53
  • 2
    [Nested functions are a GCC extension](https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html). They are _not_ supported by standard C. – Spikatrix Mar 02 '17 at 04:54
  • He means I should post an answer down below these comments, but really this is just a duplicate about nested functions. [Nested functions in C](http://stackoverflow.com/questions/2608158/nested-function-in-c). – WhozCraig Mar 02 '17 at 04:55
  • @WhozCraig Oh okay. How should I close the question? I still want to be able to look back at this if I run into a problem such as this later so I don't want to delete it. – Drizzy Mar 02 '17 at 05:01
  • 1
    @Drizzy I'm out of close-votes for the day, but hopefully someone will happen by and mark this as a duplicate. Anyway, happy coding. – WhozCraig Mar 02 '17 at 05:03
  • @WhozCraig Aright. Thanks again for all the help! I really appreciate it even though it is something that was very simple that I overlooked. – Drizzy Mar 02 '17 at 05:05
  • Possible duplicate of [Nested function in C](http://stackoverflow.com/questions/2608158/nested-function-in-c) – Ajay Brahmakshatriya Mar 02 '17 at 05:29

1 Answers1

4

You are getting this error, due to addnum as it is defined inside the main function. C doesn't support the nested functions by default. But, gcc has a extended feature that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.

Here is the link

sreepurna
  • 1,562
  • 2
  • 17
  • 32