Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof(<function name>); return wht??

Reply
Thread Tools

sizeof(<function name>); return wht??

 
 
vicks
Guest
Posts: n/a
 
      12-10-2007
Hi,

I had just written the following code but not able to figure out why
the sizeof() operator is printing the weired output when i give a
function name as its operand?

#include<stdio.h>
#include <assert.h>

int f();
int p();
int q(int , int );

int main() {

printf("size of %d \n",sizeof(f));
printf("size of %d \n",sizeof(p));
printf("size of %d \n",sizeof(q));
}

Output:
size of 1
size of 1
size of 1

Regards
Vikas Gupta
 
Reply With Quote
 
 
 
 
Chris Torek
Guest
Posts: n/a
 
      12-10-2007
In article <(E-Mail Removed)>
vicks <(E-Mail Removed)> wrote:
>I had just written the following code but not able to figure out why
>the sizeof() operator is printing the weired output when i give a
>function name as its operand?


[sample code snipped]

The problem is that you have not told your compiler to operate as
a C compiler. Most things labeled "C compilers" are not, in fact,
C compilers by default -- you have to give them some sort of
option(s) to make them obey their part of the "Standard C contract",
as it were.

In this case, my crystal ball says that you are using gcc, which
is not a C compiler unless you also use the "-ansi -pedantic".
Doing so will cause the compiler to (correctly) "emit a diagnostic"
telling you that sizeof should not be applied to function names.

(After producing the diagnostic, the compiler may -- or may not --
go on to produce an executable. If it does produce one and you
run it, Standard C says absolutely nothing about what will happen.
The documentation for that *particular* compiler might say what
will happen, but of course, if you ever use some *other* compiler,
something else may happen, or you might not get an executable
program.)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      12-10-2007
vicks wrote:
> Hi,
>
> I had just written the following code but not able to figure out why
> the sizeof() operator is printing the weired output when i give a
> function name as its operand?
>
> #include<stdio.h>
> #include <assert.h>
>
> int f();
> int p();
> int q(int , int );
>
> int main() {
>
> printf("size of %d \n",sizeof(f));
> printf("size of %d \n",sizeof(p));
> printf("size of %d \n",sizeof(q));
> }
>

Try turning your warning level up and see what your compiler tells you.

--
Ian Collins.
 
Reply With Quote
 
vicks
Guest
Posts: n/a
 
      12-10-2007
On Dec 9, 9:30 pm, Chris Torek <(E-Mail Removed)> wrote:
> In article <(E-Mail Removed)>


> The problem is that you have not told your compiler to operate as
> a C compiler. Most things labeled "C compilers" are not, in fact,
> C compilers by default -- you have to give them some sort of
> option(s) to make them obey their part of the "Standard C contract",
> as it were.
>
> In this case, my crystal ball says that you are using gcc, which
> is not a C compiler unless you also use the "-ansi -pedantic".
> Doing so will cause the compiler to (correctly) "emit a diagnostic"
> telling you that sizeof should not be applied to function names.

....

Well yeap after using these switched for compilation I got the
warnings but my compiler still goes to give putput.. its ok.. With
this one more thing..

If i using this switches(-ansi ) then it uses C89/c99 std why am
asking this is because It gives an error when I uses "//" comment!!
 
Reply With Quote
 
Chris Torek
Guest
Posts: n/a
 
      12-10-2007
>On Dec 9, 9:30 pm, Chris Torek <(E-Mail Removed)> wrote:
>> In this case, my crystal ball says that you are using gcc, which
>> is not a C compiler unless you also use the "-ansi -pedantic" [flags].


In article <(E-Mail Removed)>
vicks <(E-Mail Removed)> wrote:
>If i using this switches(-ansi ) then it uses C89/c99 std why am
>asking this is because It gives an error when I uses "//" comment!!


This is getting off-topic (there are gcc-specific groups for
gcc-specific questions), but ... there are a bunch of different
switches for various versions of gcc. Depending on which version
of gcc you have, it will support one or more of:

-ansi
-std=c89
-std=gnu89
-std=gnu99
-std=c99

The first two mean the same thing, selecting "C89" (also sometimes
called "C90" or "ANSI C", although technically "ANSI C" should now
refer to C99, as ANSI adopted the C99 standard shortly after that
standard was available). The "-pedantic" flag is required for
actual conformance, but the -std (or -ansi) flag is required to
select the mode before turning on conformance.

C89 does not have "//" comments, hence selecting C89 conformance
makes them become syntax errors, as they should be.

Gcc (any version) does not fully support C99 despite the -std=c99
option. Selecting -std=c99, if you have it at all -- it is not
present in older versions -- gets you "as close as possible" in
that particular version of gcc. Exactly how close depends on
which version of gcc you use.

The various "gnu" standards implement the GNUC languages, which
resemble C fairly closely, but are different in a number of important
ways, including things like allowing "sizeof function" (with the
result always being (size_t)1 in at least some variants of GNUC).
Note that different variants of GNUC support *different* features,
so that using any one particular feature may prevent you from using
another version of gcc on your code. (To avoid being trapped into
particular versions of particular compilers, you can simply avoid
using the extensions. Of course, if the extension does something
you really *want*, you have to choose between "portability" and
"convenience and perhaps even accomplishment".)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (4039.22'N, 11150.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 11:38 am, vicks <(E-Mail Removed)> wrote:
> Hi,
>
> I had just written the following code but not able to figure out why
> the sizeof() operator is printing the weired output when i give a
> function name as its operand?
>
> #include<stdio.h>
> #include <assert.h>
>
> int f();
> int p();
> int q(int , int );
>
> int main() {
>
> printf("size of %d \n",sizeof(f));
> printf("size of %d \n",sizeof(p));
> printf("size of %d \n",sizeof(q));
>
> }
>
> Output:
> size of 1
> size of 1
> size of 1
>
> Regards
> Vikas Gupta


I dont know how this get compiled. those compilers which follows the
ISO C++ standard will generate error on compilation.

Regards,
Sarath
 
Reply With Quote
 
Sarath
Guest
Posts: n/a
 
      12-10-2007
On Dec 10, 11:38 am, vicks <(E-Mail Removed)> wrote:
> Hi,
>
> I had just written the following code but not able to figure out why
> the sizeof() operator is printing the weired output when i give a
> function name as its operand?
>
> #include<stdio.h>
> #include <assert.h>
>
> int f();
> int p();
> int q(int , int );
>
> int main() {
>
> printf("size of %d \n",sizeof(f));
> printf("size of %d \n",sizeof(p));
> printf("size of %d \n",sizeof(q));
>
> }
>
> Output:
> size of 1
> size of 1
> size of 1
>
> Regards
> Vikas Gupta


Ignore my previous post. I forgot that you are asking about a C
environment.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments