return from a function

A

asit

#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 ??
 
W

Walter Roberson

#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;
}

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.
 
P

Pedro Graca

asit said:
#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 :)
 
M

Malcolm McLean

asit said:
#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.
 
E

Eric Sosman

asit said:
#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.
 
U

user923005

#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.
 
R

Richard Bos

asit said:
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
 
A

Army1987

user923005 said:
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.
 

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

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top