By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,871 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,871 IT Pros & Developers. It's quick & easy.

Regarding function default return value?

P: 12
e.g. The func returns 11 on this execution? Can anyone elucidate its reason?

Expand|Select|Wrap|Line Numbers
  1. func()
  2. {
  3.    printf("Hello World");
  4. }
  5. int main()
  6. {
  7.     func()
  8.     return 0;
  9. }
  10.  
Sep 23 '10 #1
Share this Question
Share on Google+
11 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Where are you seeing 11?

All I see is "Hello world". True, printf returns the number of characters displayed which in this case is 11 but you are not capturing the 11.
Sep 23 '10 #2

P: 12
Ok, Let me modify the code to make it more understandable:

Expand|Select|Wrap|Line Numbers
  1.  func()
  2.  {
  3.     printf("Hello World");
  4.  }
  5.  int main()
  6.  {
  7.      printf("\n%d",func());
  8.      return 0;
  9.  }
  10.  
Now this code will print 11 when printf of main will execute. Can anybody please elucidate the reason for the same.
Sep 23 '10 #3

100+
P: 1,059
This is really interesting.

You are getting the string length of the string that is passed to printf function.

but not all printf. if you add 10 more printf in this case you will get only the last string length in the last printf function.

I am not sure but may be i read or heard from somewhere that function use AX/EAX register to return value.

If this information is true then it is very much possible that printf function use AX/EAX register but it do not reset the register. that is why in return you are getting 11 which is your string length.
Sep 23 '10 #4

100+
P: 1,059
after posting the previous answer i made a search on google. Here what I found on x86 calling convention

It might give you a good idea
Sep 23 '10 #5

Banfa
Expert Mod 5K+
P: 8,916
func is declared without a return type. That means this is C and it is using default int as the return type.

That has nothing to do with the return value. Because func does not return a value then the return value of func is whatever happened to be in the location that carries the return value at the time the function returned.

Since the last thing func does is call printf and printf sets an int return value correctly.

Since x86 uses a register to return the return value and nothing has interfered with that register between the printf call and func returning when func returns the return value of the printf statement is still in the register and so that appears as the return value of func.

Relying on this sort of behaviour is extremely poor practice.
Sep 23 '10 #6

Expert 100+
P: 2,398
You ought to have gotten a compiler error that func wasn't returning an explicit value. Are you sure there wasn't an error or warning message?
Sep 24 '10 #7

100+
P: 1,059
I compiled on GCC it didn't gave any error message warning. compiled clean
Sep 24 '10 #8

Expert 100+
P: 2,398
If you invoke GCC from the command line, then add -Wall, othewise explore your IDE and enable all warnings. You should be getting an error at the end of func.

However, the following code would not necessarily produce an error:
Expand|Select|Wrap|Line Numbers
  1. void func(void) {
  2.    printf("Hello World");
  3.    }
  4.  
  5. int main(void) {
  6.    printf("%d\n", func());
  7.    }
In your example, func is declared without a return type (which means it defaults to returning int) but doesn't have a return statement. It is the missing return statement that I expect to produce a warning message. In my version, func is declared to return void, so no return statement is expected; thus no warning. In either case, main will print some number (I'll take your word that it is 11). That's because the format string doesn't match the argument type. It is not the compiler's responsibility to catch this kind of bug (although some will) because the types of the value arguments are implied by the format string, not specified by the function prototype. (For example, printf("%d\n", 4.5); will have interesting results, but won't produce a warning.)

(However, the compiler might complain about passing a void function call as an argument because there wouldn't be a value to pass.)
Sep 24 '10 #9

Banfa
Expert Mod 5K+
P: 8,916
Do you use the "-Wall" and preferably "-Wall -pedantic" switches? If not get in the habit of using them, it switches on all warnings and forces standard compliance.

Adding in a #include stdio.h to the supplied source, and using a ; with mingw 4.4.0 without -Wall I get no errors but with -Wall I get

Expand|Select|Wrap|Line Numbers
  1. bytes.c:4: warning: return type defaults to 'int'
  2. bytes.c: In function 'func':
  3. bytes.c:6: warning: control reaches end of non-void function
First warning tells of the rather out-dated use of default int return and the second of reaching the end of a function that returns a value without having a return statement.
Sep 24 '10 #10

100+
P: 1,059
the command i have used to test was like this:

Expand|Select|Wrap|Line Numbers
  1. #gcc -o rt.o rt.c
and it didnt generate any error.
Sep 24 '10 #11

100+
P: 1,059
Banfa,
You replied by the time i was writing the reply.
got the point
Sep 24 '10 #12

Post your reply

Sign in to post your reply or Sign up for a free account.