function automatically returns the value

C

chellappa

hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69

Thanks All

by
chellappa
 
R

Richard Heathfield

chellappa said:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69

foo@bar:~/scratch> ./foo
ENTER ANY THREE NUMBERS :
TWENTY-THREE TWENTY-THREE TWENTY-THREE
sum = 45768
 
T

Thad Smith

chellappa said:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69

The problem is that your program works by accident, since calsum does
not explicitly return a value. On another implementation it may not.
In general, when your code doesn't conform to the standard (and
implementation specifications) you get undefined behavior. That
occurs with your program. It is particularly nasty because it may
work in some cases, or implementations, and not others.

A powerful technique for avoiding those cases is to enable all
warnings and heed them. You should have gotten appropriate warnings
for your code.
 
M

Mike Wahler

chellappa said:
hi All,
function

accidentally

returns the value,am not used "return"
i am using Linux -gcc complier

Then you're using a *very old* version, or have
it configured to ignore serious errors, or use
some very strange extension(s).
please tell me.... what is problem...

Undefined behavior.
source
=====
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("sum = %d\n", sum);
}
calsum(x,y,z)
int x,y,z;

This is archaic syntax, not valid standard C.

calsum(int x, int y, int z) // C89

int calsum(int x, int y, int z) // C99
{
int d;
d=x+y+z;
}

Undefined behavior. return statement required.

What probably happened for you was that the value of
'd' just accidentally happened to get stored in a
register which was also accessed by the calling
function. The standard C language can't be used
to explain the behavior because you don't have
a standard C program. If you want to inquire
about the quirks of gcc, see a gcc group.
output
=====
ENTER ANY THREE NUMBERS :
23
23
23
sum = 69


-Mike
 
C

Christopher Benson-Manica

Mike Wahler said:
Then you're using a *very old* version, or have
it configured to ignore serious errors, or use
some very strange extension(s).

Or he's getting better at trolling...
 
M

Mike Wahler

Richard Heathfield said:
chellappa said:


foo@bar:~/scratch> ./foo
ENTER ANY THREE NUMBERS :
TWENTY-THREE TWENTY-THREE TWENTY-THREE
sum = 45768

I get output of "ELEVENTY JILLION AND SIX", followed by my
speakers emitting the faint sound of coyotes howling
in the distance. And my nose is beginning to itch.
Maybe th%)v^ *H)^_()&./7[NO CARRIER]
 
K

Keith Thompson

Mike Wahler said:
This is archaic syntax, not valid standard C.

Yes, it's archaic, but it's still legal (except that C99 forbids
implicit int). But of course there's no good reason not to use
prototypes.
 
T

Tatu Portin

chellappa said:
hi All,
function automatically returns the value,am not used "return"
i am using Linux -gcc complier
please tell me.... what is problem...

#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("sum = %d\n", sum);
}

calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}

In C, functions that have not defined return value,
do return value of int.
So, following prototypes are identical:

int func (int x, char b);

func (int x, char b);

If you want to declare a function without return value,
you use void:

/* A function that doesn't return a value. */
void func (int x, char b);
 
K

Keith Thompson

Tatu Portin said:
#include <stdio.h>
main()
{
int a,b,c,sum;
printf("ENTER ANY THREE NUMBERS :\n");
scanf("%d%d%d",&a,&b,&c);
sum=calsum(a,b,c);
printf("sum = %d\n", sum);
}

calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
}

In C, functions that have not defined return value,
do return value of int.
So, following prototypes are identical:

int func (int x, char b);

func (int x, char b);

That's true in C90, but not in C99 -- and it's a bad idea in either.
If you want to declare a function without return value,
you use void:

/* A function that doesn't return a value. */
void func (int x, char b);

Yes, of course, but since chellappa was assigning the result of the
function call to a variable, he obviously wanted the calsum() function
to return a value. The fix is not to change the function so it
returns void; the fix is to add a return statement (and to use proper
prototypes rather than the archaic, but still legal, pre-ANSI function
declarations, and to fix a few other problems).

The original question was why the incorrect code was still "working"
(i.e., the value of x+y+z was assigned to sum even though there was no
return statement). The general answer is that the program invokes
undefined behavior, and the observed behavior is as legitimate as any
other behavior. The specific answer probably has to do with how the
particular compiler uses CPU registers, but quite frankly there's
little reason to care.
 
P

Peter Nilsson

Mike said:
This is archaic syntax, not valid standard C.

As others have stated, it's valid C89.
calsum(int x, int y, int z) // C89

// comments are not valid C89. ;)
int calsum(int x, int y, int z) // C99


Undefined behavior. return statement required.

The return statement is required because the calling function attempts
to
use the value.

C99 did not remove the 'feature' that, in general, non-void functions
are not
required to return a value.
 
T

Tatu Portin

Keith said:
That's true in C90, but not in C99 -- and it's a bad idea in either.


Yes, of course, but since chellappa was assigning the result of the
function call to a variable, he obviously wanted the calsum()
function
to return a value. The fix is not to change the function so it
returns void; the fix is to add a return statement (and to use
proper prototypes rather than the archaic, but still legal, pre-ANSI
function declarations, and to fix a few other problems).

The original question was why the incorrect code was still "working"
(i.e., the value of x+y+z was assigned to sum even though there was
no
return statement). The general answer is that the program invokes
undefined behavior, and the observed behavior is as legitimate as
any
other behavior. The specific answer probably has to do with how the
particular compiler uses CPU registers, but quite frankly there's
little reason to care.


Well, yes, and the main() could also return the number of characters
printed by printf (). Have to read more carefully.
 
M

Mark McIntyre

Yes, it's archaic, but it's still legal (except that C99 forbids
implicit int). But of course there's no good reason not to use
prototypes.

I guess one might be stuch with a pre-ansi compiler. Thats why gcc
have a website mind you...
 
H

haroon

Mike Wahler wrote:

[snip]
What probably happened for you was that the value of
'd' just accidentally happened to get stored in a
register which was also accessed by the calling
function.

on intel x86 it happens to be EAX. this functions returns 0 on my x86
and x86_64 machines because the line asm("xor %eax, %eax") clears the
eax contents.
/*CODE BEGINS*/
calsum(x,y,z)
int x,y,z;
{
int d;
d=x+y+z;
asm("xor %eax, %eax");
}
/*CODE ENDS*/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top