What will be the main return value?

Possible duplicate:
What makes the main return?

Hi,

I have doubts about this example.

   int temp = temp +1;
   int main()
   {
      return temp;
   }

will the code be valid? if so, what will the temp value be?

Thank.

+3
source share
5 answers

I am sure that it is defined, and temp should be set to 1 .

"Objects with static storage must be initialized with zeros before any other initialization."

+4
source

This should return 1 because the global int variable is initialized to the first 0.

+2
source

C, undefined , temp + 1 , , .

+1

Assuming you are on a linux / unix machine using the GNU toolchain, you can easily check this:

Copy the above code to test.c:

$ echo "int temp = temp +1;
   int main()
   {
      return temp;
   }" > test.c

Testing to check if it is really C (it is not):

$ gcc test.c
test.c:2: error: initializer element is not constant

Testing to make sure it is valid in C ++ (this!):

$ g++ test.c

Check return code:

$ ./a.out && echo "success: return code $?" || echo "failure: return code $?"
failure: return code 1
+1
source

This is valid code: temp will be 1

0
source

All Articles