473,322 Members | 1,232 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

function automatically returns the value

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

Nov 23 '05 #1
12 1913
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

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Nov 23 '05 #2
"chellappa" <N.*********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
hi All,
function automatically
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
Nov 23 '05 #3
chellappa wrote:

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.

--
Thad
Nov 23 '05 #4
Mike Wahler <mk******@mkwahler.net> wrote:
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...

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 23 '05 #5

"Richard Heathfield" <in*****@invalid.invalid> wrote in message
news:dm**********@nwrdmz03.dmz.ncs.ea.ibs-infra.bt.com...
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


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]

Nov 23 '05 #6
"Mike Wahler" <mk******@mkwahler.net> writes:
"chellappa" <N.*********@gmail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...

[...]
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.


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '05 #7
chellappa wrote:
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);
Nov 23 '05 #8
Tatu Portin <ax****@mbnet.fi> writes:
chellappa wrote:
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);


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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 23 '05 #9
Mike Wahler wrote:
"chellappa" <N.*********@gmail.com> wrote in message
#include <stdio.h>
main()
{
...
sum=calsum(a,b,c);
...
}
calsum(x,y,z)
int x,y,z;
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
{
int d;
d=x+y+z;
}


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.

--
Peter

Nov 23 '05 #10
Keith Thompson wrote:
Tatu Portin <ax****@mbnet.fi> writes:
chellappa wrote:
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);


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.

Well, yes, and the main() could also return the number of characters
printed by printf (). Have to read more carefully.
Nov 23 '05 #11
On Wed, 23 Nov 2005 18:17:04 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:

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...
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 24 '05 #12

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*/

Nov 24 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
9
by: Marek Lewczuk | last post by:
Hello, I'm moving out from MySQL to PostgreSQL and there are some function which are not supported in PG so I'm trying to write my own functions. Currently I have big problem with function IF(),...
4
by: anonymous | last post by:
Thanks your reply. The article I read is from www.hakin9.org/en/attachments/stackoverflow_en.pdf. And you're right. I don't know it very clearly. And that's why I want to understand it; for it's...
7
by: chellappa | last post by:
hi this program return value automatically ... how it is possible ..i am not return any value... but i return correct values i am using Linux -gcc complier please tell me what is this main() {...
5
by: Rob Meade | last post by:
Hi all, Quick question if I may... I have a function which depending on whether it was succesful to do something or not returns True or False as a boolean. I have another function which...
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
16
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
7
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.