Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof() O/P

Reply
Thread Tools

sizeof() O/P

 
 
raghu
Guest
Posts: n/a
 
      12-24-2005
i'm surprised at the output of the following code. compiled in turbo C

void main()
{
printf("%d",sizeof(printf());
}
the output was : 2
how come the output is 2? actually what is the property of sizeof()
and printf()?are there any return types for the two functions? hoping
for the positive responses.. thanks a lot in advance.

 
Reply With Quote
 
 
 
 
Chris Hills
Guest
Posts: n/a
 
      12-24-2005
In article <(E-Mail Removed) .com>,
raghu <(E-Mail Removed)> writes
>i'm surprised at the output of the following code. compiled in turbo C
>
>void main()
>{
>printf("%d",sizeof(printf());
>}
>the output was : 2
>how come the output is 2? actually what is the property of sizeof()
>and printf()?are there any return types for the two functions? hoping
>for the positive responses.. thanks a lot in advance.


Ignoring that it should have been

#include <stdio.h>
int main(void)
{
printf("%d",sizeof(printf());
return 0;
}

Who set this home work?


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ http://www.velocityreviews.com/forums/(E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      12-24-2005
raghu said:

> i'm surprised at the output of the following code. compiled in turbo C
>
> void main()


Don't be. Any program which gives the wrong return type for main() exhibits
undefined behaviour, so any behaviour, surprising or not, is permissible as
far as the C language is concerned.

> {
> printf("%d",sizeof(printf());


Calling printf without a correct prototype in scope invokes undefined
behaviour. Calling printf without any arguments invokes undefined
behaviour.

Start your program like this:

#include <stdio.h>

int main(void)

....and then try your program again, first fixing any diagnostics issued by
your compiler.


--
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)
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-24-2005
Chris Hills said:

> In article <(E-Mail Removed) .com>,
> raghu <(E-Mail Removed)> writes
>>i'm surprised at the output of the following code. compiled in turbo C
>>
>>void main()
>>{
>>printf("%d",sizeof(printf());
>>}
>>the output was : 2
>>how come the output is 2? actually what is the property of sizeof()
>>and printf()?are there any return types for the two functions? hoping
>>for the positive responses.. thanks a lot in advance.

>
> Ignoring that it should have been
>
> #include <stdio.h>
> int main(void)
> {
> printf("%d",sizeof(printf());


No, %d doesn't match size_t.

--
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)
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-24-2005
Richard Heathfield said:

> raghu said:
>
>> printf("%d",sizeof(printf());

>
> Calling printf without a correct prototype in scope invokes undefined
> behaviour. Calling printf without any arguments invokes undefined
> behaviour.


Except, of course, that the code doesn't actually call printf without any
arguments! Sorry about that. But the rest of my reply stands.

--
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)
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-24-2005
raghu wrote:
> i'm surprised at the output of the following code. compiled in turbo C
>
> void main()
> {
> printf("%d",sizeof(printf());
> }
> the output was : 2
> how come the output is 2? actually what is the property of sizeof()
> and printf()?are there any return types for the two functions? hoping
> for the positive responses.. thanks a lot in advance.


There should be no cause for surprise, no matter what
output is or is not produced. You should not be surprised
if the code makes demons fly out of your nose, because it
invokes undefined behavior not once, not twice, but thrice:

- The main() function is not `void'

- It is U.B. to call a variadic function like printf()
without a prototype in scope

- It is U.B. to pass a non-`int' to the "%d" specifier
(On some "exotic" systems where `size_t' is narrower
than `int' this might be all right, so I really should
say that whether the behavior is defined or undefined
is implementation-defined.)

.... and on top of all that, the fate of a newline-less "line"
at the end of a stream of output is implementation-defined.

(Three instances of U.B. and one of I.D.B., or possibly
two U.B. and two I.D.B. -- all in just four lines, two of
which consist only of braces. Is this density of errors --
a density of densness, one might say -- a candidate for the
Guinness Book? Sadly, I fear not.)

Here's a cleaned-up version:

#include <stdio.h> /* for printf() prototype */
int main(void) {
printf ("%d\n", /* note newline */
(int)sizeof printf()); /* note type coercion */
return 0; /* required in C90, optional in C99 */
}

Now, you may still be puzzled about the output of the
cleaned-up version of your bletcherous code. Much will become
clearer if you ponder two questions:

- What does the `sizeof' operator do with its argument?

- What type does the printf() function return?

Answer these, Grasshopper, and you will be on the path
to enlightenment -- but if you keep on writing code like the
sample you provided here, it means you're on the right path
but walking in the wrong direction.

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      12-24-2005
raghu a écrit :
> i'm surprised at the output of the following code. compiled in turbo C
>
> void main()
> {
> printf("%d",sizeof(printf());
> }


This code invokes an undefined behaviour

- The type returned by main() is int.
- An explicit valid value must be returned.
"%d" expects an int and the sizeof operator returns a size_t.

> the output was : 2


Supposing the code was fixed, this 2 is the size of the type returned by
printf (actually int) on your machine.

> how come the output is 2? actually what is the property of sizeof()


Like your C-book said, the sizeof operator return the size of an object
or of a type (with parens) in number of bytes. The type of the returned
value is size_t.

> and printf()?


printf() is a function returning int. Details belong to your C-book.

> are there any return types for the two functions?


sizeof is not a function but a C-unary-operator
--
A+

Emmanuel Delahaye
 
Reply With Quote
 
Chris Hills
Guest
Posts: n/a
 
      12-24-2005
In article <dojjl4$qeo$(E-Mail Removed)-infra.bt.com>, Richard
Heathfield <(E-Mail Removed)> writes
>raghu said:
>
>> i'm surprised at the output of the following code. compiled in turbo C
>>
>> void main()

>
>Don't be. Any program which gives the wrong return type for main() exhibits
>undefined behaviour, so any behaviour, surprising or not, is permissible as
>far as the C language is concerned.


void main (void) *may* be permissible with turbo C as I think it could
turn out code that would run with out an OS. Somewhere I have a ROM
kit for Turbo-C.

In general (ie unless explicitly specified as self hosted) it MUST be
int main ([args])


>
>> {
>> printf("%d",sizeof(printf());

>
>Calling printf without a correct prototype in scope invokes undefined
>behaviour. Calling printf without any arguments invokes undefined
>behaviour.
>
>Start your program like this:
>
>#include <stdio.h>
>
>int main(void)
>
>...and then try your program again, first fixing any diagnostics issued by
>your compiler.
>
>


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ (E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
Giannis Papadopoulos
Guest
Posts: n/a
 
      12-24-2005
Eric Sosman wrote:
> raghu wrote:
>
>> i'm surprised at the output of the following code. compiled in turbo C
>>
>> void main()
>> {
>> printf("%d",sizeof(printf());
>> }
>> the output was : 2
>> how come the output is 2? actually what is the property of sizeof()
>> and printf()?are there any return types for the two functions? hoping
>> for the positive responses.. thanks a lot in advance.

>
> ...
> ...
> Here's a cleaned-up version:
>
> #include <stdio.h> /* for printf() prototype */
> int main(void) {
> printf ("%d\n", /* note newline */
> (int)sizeof printf()); /* note type coercion */
> return 0; /* required in C90, optional in C99 */
> }


Well, this does not compile on gcc given -ansi -pedantic.
Perhaps printf("") instead of printf() would be better?
 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      12-24-2005
raghu wrote:
> i'm surprised at the output of the following code. compiled in turbo C
>
> void main()
> {
> printf("%d",sizeof(printf());
> }
> the output was : 2
> how come the output is 2?


Since you define main to have an invalid return type (main returns an
int) and leave out the declaration of printf (corrected by including
<stdio.h>), and fail to terminate the last line of output with an
end-of-line character, any action this program performs is completely
random.
 
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




Advertisments