473,378 Members | 1,467 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,378 software developers and data experts.

return from a function

#include <stdio.h>
//#include <stdlib.h>

int main()
{
int c;
printf("c before call=%d\n",c);
c=message();
printf("c after call=%d\n",c);
return 0;
}

message()
{
printf("Live");
}

In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??
Jan 15 '08 #1
7 2761
In article <05**********************************@e25g2000prg. googlegroups.com>,
asit <li*****@gmail.comwrote:
>#include <stdio.h>
//#include <stdlib.h>
>int main()
{
int c;
printf("c before call=%d\n",c);
c=message();
printf("c after call=%d\n",c);
return 0;
}
>message()
{
printf("Live");
}
>In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??
Coincidence. You are using undefined behaviour (printing
an uninitialized variable, failing to return a value from a routine
that is used as an expression). You could get just about any value
printed there.

Note: you have used // comments, which are C99 but not C89, but
you have used an implicit return type for message(), which is permitted
in C89 but not in C99. We deduce that you are using a C89 compiler
with extensions.

--
We regret to announce that sub-millibarn resolution bio-hyperdimensional
plasmatic space polyimaging has been delayed until the release
of Windows Vista SP2.
Jan 15 '08 #2
asit wrote:
#include <stdio.h>
//#include <stdlib.h>

int main()
{
int c;
printf("c before call=%d\n",c);
[snip]
In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??

You got lucky.

Right at the first printf() (which tries to access an uninitialized
variable and "invokes" UB) anything can happen.

You got lucky, you didn't get your hard disk reformatted :)
Jan 15 '08 #3

"asit" <li*****@gmail.comwrote in message
#include <stdio.h>
//#include <stdlib.h>

int main()
{
int c;
printf("c before call=%d\n",c);
c=message();
printf("c after call=%d\n",c);
return 0;
}

message()
{
printf("Live");
}

In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??
Hidden reason.
In the days of yore functions would return int unless told otherwise, which
was and still usually is implemeted by designating a register as the return
value.
Your message function is implicit int, but doesn't actually set the value.
So the return can be garbage, maybe it can even be some sort of trap
representation - you'd have to dig around in the standard to find out, but
it hardly matters. The code is wrong.
However compilers aren't usually too bothered about tripping up bad
programs. The special return register will be set by printf(), which we can
presume is correctly written. Since your code simply ignores it, it is
passed uncorrupted up to a higher level.
Needless to say, you cannot rely on this happening, but it is what you would
expect given a typical compiler.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Jan 15 '08 #4
asit wrote:
#include <stdio.h>
//#include <stdlib.h>

int main()
{
int c;
printf("c before call=%d\n",c);
c=message();
printf("c after call=%d\n",c);
return 0;
}

message()
{
printf("Live");
}

In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??
It's a hidden reason, and hidden it shall remain. Do
not seek to learn it, for should you succeed in gaining the
fatal knowledge the Inner Brotherhood would have no choice
but to put you to death. In agony. Did I mention the part
about the agony? The word the Inner Brotherhood use for it
is "prolonged," and they take a long view of things. Don't
Go There.

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jan 16 '08 #5
On Jan 15, 12:06*pm, asit <lipu...@gmail.comwrote:
#include <stdio.h>
//#include <stdlib.h>

int main()
{
* * * * int c;
* * * * printf("c before call=%d\n",c);
* * * * c=message();
* * * * printf("c after call=%d\n",c);
* * * * return 0;

}

message()
{
* * * * printf("Live");

}

In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??
Compiler says:
C:\tmp>type foo.c
#include <stdio.h>
int main()
{
int c;
printf("c before call=%d\n", c);
c = message();
printf("c after call=%d\n", c);
return 0;
}
message()
{
printf("Live");
}

C:\tmp>cl /W4 /Ox foo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

foo.c
foo.c(6) : warning C4013: 'message' undefined; assuming extern
returning int
foo.c(11) : warning C4431: missing type specifier - int assumed. Note:
C no longer supports default-int
c:\tmp\foo.c(13) : warning C4716: 'message' : must return a value
c:\tmp\foo.c(5) : warning C4700: uninitialized local variable 'c' used
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation. All rights reserved.

/out:foo.exe
foo.obj

Compiler says:
dcorbit@DCORBIT64 /c/tmp
$ gcc -W -Wall -ansi -pedantic foo.c
foo.c: In function `main':
foo.c:6: warning: implicit declaration of function `message'
foo.c: At top level:
foo.c:11: warning: return type defaults to `int'
foo.c: In function `message':
foo.c:13: warning: control reaches end of non-void function

Splint says:
C:\tmp>splint foo.c
Splint 3.1.1 --- 12 Mar 2007

foo.c: (in function main)
foo.c(5,34): Variable c used before definition
An rvalue is used that may not be initialized to a value on some
execution
path. (Use -usedef to inhibit warning)
foo.c(6,9): Unrecognized identifier: message
Identifier used in code has not been declared. (Use -unrecog to
inhibit
warning)
foo.c: (in function message)
foo.c(13,2): Path with no return in function declared to return int
There is a path through a function declared to return a value on
which there
is no return statement. This means the execution may fall through
without
returning a meaningful result to the caller. (Use -noret to inhibit
warning)

Finished checking --- 3 code warnings

Lint says:
C:\tmp>lin foo.c

C:\tmp>"C:\Lint\Lint-nt" +v -i"C:\Lint" std.lnt -os(_LINT.TMP)
foo.c
PC-lint for C/C++ (NT) Vers. 8.00u, Copyright Gimpel Software
1985-2006

--- Module: foo.c (C)

C:\tmp>type _LINT.TMP | more

--- Module: foo.c (C)
_
printf("c before call=%d\n", c);
foo.c(5) : Warning 530: Symbol 'c' (line 4) not initialized --- Eff. C+
+ 3rd
Ed. item 4
foo.c(4) : Info 830: Location cited in prior message
_
c = message();
foo.c(6) : Info 718: Symbol 'message' undeclared, assumed to return
int
foo.c(6) : Info 746: call to function 'message()' not made in the
presence of a
prototype
_
{
foo.c(11) : Info 745: function 'message()' has no explicit type or
class, int
assumed
_
}
foo.c(13) : Warning 533: function 'message(void)' should return a
value (see
line 10)
foo.c(10) : Info 830: Location cited in prior message

---
output placed in _LINT.TMP
P.S.
Splint is here:
http://www.splint.org/

If you use it, then you will at least know how to ask the right
questions.
Jan 16 '08 #6
asit <li*****@gmail.comwrote:
In the above program, after call of function 'message', c gets the
value what printf returns. Is it a coincidence or a hidden reason
reason lies behind it ??
Both. There is a hidden reason behind it, but that that hidden reason
continues to work today on your computer is almost complete coincidence.
On another computer your program could easily crash.

Richard
Jan 16 '08 #7
user923005 wrote:
C:\tmp>cl /W4 /Ox foo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762
for 80x86
Copyright (C) Microsoft Corporation. All rights reserved.

foo.c
foo.c(6) : warning C4013: 'message' undefined; assuming extern
returning int
foo.c(11) : warning C4431: missing type specifier - int assumed. Note:
C no longer supports default-int
It is really funny to read that from a compiler which did never bother to
even begin trying to conform to C99, or (IIRC) to C95, either.

--
Army1987 (Replace "NOSPAM" with "email")
Jan 19 '08 #8

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

Similar topics

17
by: strout | last post by:
function F(e) { return function(){P(e)} } Can anybody tell me what the code is doing? If return another function all in a function I would do function F(e)
10
by: LaEisem | last post by:
On-the-job, I have "inherited" a lot of old C language software. A question or two about when "casting" of null pointer constants is needed has occurred during behind-the-scenes cleanup of some...
16
by: G Patel | last post by:
Hi, If I want to call functions that don't return int without declaring them, will there be any harm? I only want to assign the function(return value) to the type that it returns, so I don't...
15
by: Greenhorn | last post by:
Hi, when a function doesn't specify a return type ,value what value is returned. In the below programme, the function sample()is returning the value passed to 'k'. sample(int); main() { int...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
3
by: Thomas Scheiderich | last post by:
I am curious as to why ASP.NET returns values a different way from VB or VB.net (or can you use both). In my one book I have it returning using a return statement ...
12
by: Michael Maes | last post by:
Hello, I have a BaseClass and many Classes which all inherit (directly) from the BaseClass. One of the functions in the BaseClass is to (de)serialize the (inherited) Class to/from disk. ...
7
by: nafri | last post by:
hello all, I want to create a function that returns the first element of the Array that is input to it. However, the Input Array can be an Array of points, double, or anyother type, which means...
2
by: mosesdinakaran | last post by:
Hi everybody, Today I faced a problem where I am very confused and I could not solve it and I am posting here.... My question is Is is possible to return a value to a particular function ...
7
by: Terry Olsen | last post by:
How do I get this to work? It always returns False, even though I can see "This is True!" in the debug window. Do I have to invoke functions differently than subs? Private Delegate Function...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.