0

I currently have a problem - if I write a program and in main and I use sqrt and for compiling I use arm-linux-gnueabi-gcc program.c -o prog-arm -lm I have no problem (where -lm for linking math).

So sqrt (25) will print 5 at the end

But if I call another function from main for example:

fkt (int a) {
  int x = sqrt (a);
  return x;
}
int main () {
  int b = fkt (25);
  printf ("% d", b);
  return 0;
}

with arm-linux-gnueabi-gcc program.c -o prog-arm -lm 25 is output instead of 5. There is no compilation error.

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Vojo
  • 1
  • 1
    Welcome to StackOverflow! Please post a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) as text, the shortest ***complete*** code that shows what you have tried. The best way to do that is by copy/paste, after you check it does exhibit the behaviour described. – Weather Vane Nov 03 '21 at 10:59
  • 4
    `fkt` returns a `int` and it doesn't have a return type. Is it a typo? – kiner_shah Nov 03 '21 at 11:01
  • There might be no compiler error but there are multiple warnings all over the place. Warnings do _not_ mean "here's a cosmetic issue you can optionally fix" but rather "here is a bug that likely prevents your program from working as intended". See [What must a C compiler do when it finds an error?](https://software.codidact.com/posts/277340) – Lundin Nov 03 '21 at 11:18
  • 1
    Does your program.c have `#include ` and `#include `? Also, the return type of `fkt` should be declared explicitly. Implicit `int` was removed in the 1999 version of the C standard. – Ian Abbott Nov 03 '21 at 11:18
  • Ah yeah missing includes could actually be the perfect explanation for this bug. – Lundin Nov 03 '21 at 11:20
  • I didn't manage to provoke the error when I purposely ignored includes though, tested at gcc 11.2 arm-linux. What I was thinking is that in case includes are missing, the `sqrt` call might go bananas in case the compiler can't optimize it away, but in case it is optimized, it will just get replaced by 5. – Lundin Nov 03 '21 at 11:34
  • What happens if you replace the slop code with valid standard C? That is `#include int fkt (int a) { return (int)sqrt(25); }`. Does the problem persist? – Lundin Nov 03 '21 at 11:35

1 Answers1

-1

Try using arm-linux-gnueabihf-gcc It's a bit different compiling, because of compilers underlying libraries (to the best of my knowledge). More to the topic: Difference between arm-eabi arm-gnueabi and gnueabi-hf compilers

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 03 '21 at 11:52