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