473,757 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

main return value stange question

hello to all members,

i have strange question in C.
main()
{
printf("%d",mai n) ;
}

here o/p is same for all m/c in TC++ version 3.0 i.e 657.
I think this is not garbage.
what u think??
plz reply.
Nov 14 '05 #1
11 6916

"Sweety" <sw************ @yahoo.co.in> wrote in message
news:11******** *************** ***@posting.goo gle.com...
hello to all members,

i have strange question in C.
main()
{
printf("%d",mai n) ;
}

here o/p is same for all m/c in TC++ version 3.0 i.e 657.
I think this is not garbage.
what u think??


The behavior of the above program is undefined.

-Mike
Nov 14 '05 #2
Something that calls itself Sweety wrote:
hello to all members,

i have strange question in C.
main()
{
printf("%d",mai n) ;
}

here o/p is same for all m/c in TC++ version 3.0 i.e 657.
I think this is not garbage.
what u think??
I think you be a sweet little troll.
plz reply. cat Sweety.c #include <stdio.h>

main() {
printf("%d", main);
}
gcc -Wall -std=c99 -pedantic -o Sweety Sweety.c

Sweety.c:3: warning: return type defaults to `int'
Sweety.c: In function `main':
Sweety.c:4: warning: int format, pointer arg (arg 2)

Nov 14 '05 #3
Sweety wrote:

hello to all members,

i have strange question in C.
main()
{
printf("%d",mai n) ;
}

here o/p is same for all m/c in TC++ version 3.0 i.e 657.
I think this is not garbage.
what u think??


It's garbage. More specifically, the program invokes
undefined behavior, and anything at all might happen. On
the system you are using at the moment, "anything at all"
appears to be "print 657," but that is not guaranteed.

The program exhibits undefined behavior for two reasons,
and "peculiar behavior" for two more:

- It calls a variadic function without a prototype in
scope. Functions that take a variable number of
arguments -- like printf() -- must be properly
declared before use, or undefined behavior results.
The best way to declare printf() is to #include
<stdio.h>.

- It uses the "%d" conversion specifier with a value
that is not an `int'. `main' in the printf() call
is a pointer to the function named `main', and a
function pointer is not an `int'. When you tell
printf() to expect an argument of one type (`int')
and actually supply something different (function
pointer), undefined behavior results.

- It fails to end its final (only) line of output with
a newline character '\n'. On some implementations ,
the output may not appear at all unless each line
ends with a newline.

- It "drops off the end" of the `int'-valued function
main() without returning an `int' value. If the
environment tries to use the value returned by main()
as an indication of the program's success or failure
(most environments do this), great confusion can
result if main() fails to return a value.

--
Er*********@sun .com
Nov 14 '05 #4
Mike Wahler <mk******@mkwah ler.net> spoke thus:
main()
{
printf("%d",mai n) ;
}
The behavior of the above program is undefined.


Of course - the challenge is to enumerate all the different ways it
can explode :) I'll give it a shot...

1) main() will work fine on a C89 implementation, but will be rejected
by a mythical C99 implementation. Yes?
2) There is no newline in the printf.
3) Implicitly casting main's function pointer (is that what it is in that
context?) to an int.
4) Nothing is returned, which is fine for C99 but not C89... right?
5) stdio.h wasn't included (in the post itself, at least)

Does the behavior improve if the program is changed to

#include <stdio.h>

int main()
{
printf( "%p\n", (void *)main );
return EXIT_SUCCESS;
}

?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #5
Christopher Benson-Manica <at***@nospam.c yberspace.org> wrote in
news:bs******** **@chessie.cirr .com:
#include <stdio.h> #include <stdlib.h>
int main(void)
{
printf("%p\n", (void *) main);
return EXIT_SUCCESS;
}

--
- Mark ->
--
Nov 14 '05 #6
Sweety wrote:
hello to all members,

i have strange question in C.
main()
{
printf("%d",mai n) ;
}

here o/p is same for all m/c in TC++ version 3.0 i.e 657.
I think this is not garbage.
what u think??
plz reply.


(From the top) I think that
1) You forgot to #include <stdio.h>, thus not providing the prototype for
the variadic function printf(). This is extremely naughty.
2) Since the Turbo C implementation you are using claims to be a C89
compiler, omitting the return type for main() is OK, since in C89 it
defaults to int. When you get a C99 compiler doing this becomes naughty.
3) Failing to terminate the last line of output with an end-of-line
character ('\n') results in implementation-defined behavior. This is very
naughty.
4) When you get a C99 compiler, leaving off the return statement (or call
to exit()) will be acceptable, although it is probably a good idea to
return an int from a function that promises to do so. main() is such a
function. However, you are using a C89 compiler. The same standard that
allows you to get away with omitting the return type [see #2], makes
leaving off the return statement result in implementation-defined behavior.
You have been naughty yet another time.
--
Martin Ambuhl

Nov 14 '05 #7
Christopher Benson-Manica wrote:

Of course - the challenge is to enumerate all the different ways it
can explode :) I'll give it a shot...

1) main() will work fine on a C89 implementation, but will be rejected
by a mythical C99 implementation. Yes?
2) There is no newline in the printf.
3) Implicitly casting main's function pointer (is that what it is in that
context?) to an int.
I'm not sure what you mean here. I don't think any conversion is
happening, though. A function pointer is passed, but printf expects an
int. The type mismatch is the only problem here, as far as I can tell.
4) Nothing is returned, which is fine for C99 but not C89... right?
5) stdio.h wasn't included (in the post itself, at least)

Does the behavior improve if the program is changed to

#include <stdio.h>

int main()
{
printf( "%p\n", (void *)main );
return EXIT_SUCCESS;
}


void * cannot portably contain a function address, only an object address.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #8
Kevin Goodsell <us************ *********@never box.com> spoke thus:
I'm not sure what you mean here. I don't think any conversion is
happening, though. A function pointer is passed, but printf expects an
int. The type mismatch is the only problem here, as far as I can tell.
I guess I was trying to talk about the type mismatch, and blew it ;(
void * cannot portably contain a function address, only an object address.


I see. Thanks.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #9
Groovy hepcat E. Robert Tisdale was jivin' on Tue, 23 Dec 2003
13:17:32 -0800 in comp.lang.c.
Troll Alert: main return value stange question's a cool scene! Dig it!
I think you be a sweet little troll.


Well, well! Look who's calling the kettle black.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?
Nov 14 '05 #10

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

Similar topics

4
1670
by: Rhamphoryncus | last post by:
First a bit about myself. I've been programming in python several years now, and I've got several more years before that with C. I've got a lot of interest in the more theoretical stuff (language design, component architectures, etc). Of late my focus has been on concurrent operations (and on how to design a GUI architecture, but that's not what this post is about). I've looked at threads, and the inability to kill them easily was a...
45
3618
by: Steven T. Hatton | last post by:
This is a purely *hypothetical* question. That means, it's /pretend/, CP. ;-) If you were forced at gunpoint to put all your code in classes, rather than in namespace scope (obviously classes themselves are an exception to this), and 'bootstrap' your program by instantiating a single application object in main(), would that place any limitations on what you could accomplish with your program? Are there any benefits to doing things that...
12
1755
by: Bill Cunningham | last post by:
> #include <stdlib.h> > #include <stdio.h> > > int main(int argc, char *argv) { > FILE *in, *out; > > if(argc==1) > { > in = stdin; > out = stdout;
5
2809
by: Seong-Kook Shin | last post by:
Hi, I'm reading Steve's "C Programming FAQs" in book version, and have two question regarding to Q11.16 ... Also, a `return' from `main' cannot be expected to work if data local to main might be needed during cleanup. (Finally the two forms are obviously not equivalent in a recursive call to `main'). My questions are
25
4511
by: Christian Christmann | last post by:
Hi, the ANSI-C 99 standard specifies that the main function has "int" as return type. However, there are still lots of people declaring "void" as main return type. Did previous ANSI-C standards defined "void" as return type or is this issue just a lack of C knowledge? Regards,
8
2743
by: Michal Nazarewicz | last post by:
Hi, What does returning 0 from main() mean according to C89/C90 standard? I've found that in C99 it means successful termination (7.20.4.3p5) however as I'm editing book on C at Polish Wikibooks I'd like to know what C89/C90 says about it - quotation from C89 or C90 would be nice. Moreover, in C99 main() function is somewhat special because lack of return statement is equivalent with returning zero (5.1.2.2.3p1). Is it also the case...
37
3484
by: Army1987 | last post by:
Is that in the object line a conforming program? If so, why? If not, why? I'd expect it to be much like int main(void) { for (;;); } But if I compile it with lcc-win32 and run it in its rundos.exe, it says the
27
2505
by: junky_fellow | last post by:
Guys, Can I return 0, from main() ? Is this equivalent to exit(EXIT_SUCCESS) ? thanks for any help...
11
3578
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header file but it is n't.
0
10069
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9904
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9735
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8736
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6556
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3828
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3395
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2697
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.