0

Consider a ordinary code:

#include <stdio.h>

int x;

int func(int a){
    x++;
    return a;
}

int main(){
    int a = 1;
    int n = func(a);
    printf("x = %d, n = %d", x, n);
    return 0;
}

It will print x = 1, n = 1 as expected.

But if I slightly edit the code as follows:

#include <stdio.h>

int x;

int func(int a){
    x++;
    return a;
}

int main(){
    int a = 1;
    int n = func;
    printf("x = %d, n = %d", x, n);
    return 0;
}

With the function not properly written, the complier does not report an error but prints x = 0, n = 4199760 instead.

What happened to the program?

Does the name of the function have any speical meanings?

Thank you.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • 1
    You didn't take the return value of the function, you took its address. You forgot the (). – Michael Chourdakis Jun 22 '22 at 15:09
  • 1
    You are assigning a function ***pointer***, instead of calling that function and assigning the result. – πάντα ῥεῖ Jun 22 '22 at 15:10
  • It should be `int n = func(a);` – Barmar Jun 22 '22 at 15:10
  • 9
    You should have gotten a warning when you compiled the program. If you didn't, turn up your warning level. – Barmar Jun 22 '22 at 15:10
  • @MichaelChourdakis they appear to recognize that, the question is why it didn't generate a compile error if I understand the question. – Mark Ransom Jun 22 '22 at 15:11
  • 1
    Example (2) does not compile (errors not warnings) - live - https://godbolt.org/z/14PevGcK5 How did you get example (2) to compile ? – Richard Critten Jun 22 '22 at 15:14
  • *Does the name of the function have any special meanings?* The name `func` decays to `&func`. **C** has some rather forgiving conversions between different types (which had come from its predecessor **B**, which was inspired by **BCPL**), which is why **C** is considered a *weakly* typed language. But that's why the conversion from a *function address* to an `int` is allowed. Most compilers will warn about that suspicious conversion if the compiler's warning levels are turned up. **C++** inherited that behavior from **C**. – Eljay Jun 22 '22 at 15:14
  • @pptaszni Oh, this is similar to that question. So his question is a duplicate and I'll close it. Thanks. –  Jun 22 '22 at 15:18
  • 1
    Is this code C++ or C? You've tagged it as both, but they're two separate languages. – Nathan Pierson Jun 22 '22 at 15:19
  • Doesn't compile in C++ and warning is filed in C: https://godbolt.org/z/boW7493Gx – Marek R Jun 22 '22 at 15:21
  • @Nathan Pierson Ok. I'll remove the C++ tag. –  Jun 22 '22 at 15:23
  • @Richard Critten Only compilable in C (but have warnings). –  Jun 22 '22 at 15:26

1 Answers1

2

Yes. In your second iteration, the line:

int n = func;

basically is stating "retrieve the memory address where function "func" has been placed and store that value into variable "n". That line of code is not calling your function. So, variable "x" is not being updated and remains at a value of "0", which gives you the output you see. And, if you happen to run your program again, the value for the memory address of your function could change and you might see a different value printed.

Hope that clarifies things.

Regards.

NoDakker
  • 3,390
  • 1
  • 10
  • 11