473,386 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Why does this work?

Hello everyone,
I chanced to hit upon a very weird piece of code snippet and i
cannot account for its
functioning. I tried gcc (even with -Wall option) and it worked fine.
So does CC (Solaris)
and aCC (HP-UX) without a warning. Now I can account for a, b, c - for
argc, argv and envp
but how to account for the other variables d and e? Are the compilers
at fault or I am missing
something? Thanks in advance for the help.
#include <stdio.h>
#include <stdlib.h>

int main (int a, int b, int c, int d, int e)
{
return (a + b + c + d +e);
}

Regards,

kevin.
Jul 19 '05 #1
5 4557
Tom
The OP wrote:
I chanced to hit upon a very weird piece of code snippet and i
cannot account for its
functioning.

Ron Natalie wrote:
The language only requires the compiler properly support
int main() and int main(int argc, char* argv[])
Other signatures (as long as they return int) are up to the
language to deal with. Most likely you are just violating
the definitions and these end up not being resolved at link time.


I think there is more going on here then you gave the OP credit for,
but I also think it may be compiler-specific.

Here's the OP's code, edited to eliminate the C-specific header file
#includes:

int main (int a, int b, int c, int d, int e)
{
return (a + b + c + d + e);
}

When I compile and link this as a C (not C++) with all diagnostics
turned on, gcc version 3.3 gives me the expected warnings:

C:\djgpp\mystuff>gcc -W -Wall -pedantic -o mainarg.exe mainarg.c

mainarg.c:3: warning: second argument of `main' should be `char **'
mainarg.c:3: warning: third argument of `main' should probably be
`char **'
mainarg.c:3: warning: `main' takes only zero or two arguments

(Despite what you said about the definitions not being resolved at
link time, gcc will successfully link the program and generate an
executable. The return value of the program upon execution is
presumably garbage.)

But if I compile and link the exact same program as a C++ program,
again with all diagnostics turned on, gcc version 3.3 generates no
diagnostic:

C:\djgpp\mystuff>gcc -W -Wall -pedantic -o mainarg.exe mainarg.cpp

In other words, the compiler is issuing a diagnostic if it's a C
program, but not if it's a C++ program. I would have expected the
compiler to generate the same diagnostic in both cases.

(FWIW, Comeau's online compiler compiled the program both as a C
program and as a C++ program without generating a diagnostic in either
case.)

I assume this is compiler-specific - i.e., a bug (or "feature") in
gcc, not a C++ language issue. But before I go complaining to the gcc
mailing list, can anyone think of a reason why, as a C program, this
should get a diagnostic, but not as a C++ program?

Best regards,

Tom
Jul 19 '05 #2

"Tom" <Th***********@yahoo.com> wrote in message news:7b**************************@posting.google.c om...
I assume this is compiler-specific - i.e., a bug (or "feature") in
gcc, not a C++ language issue. But before I go complaining to the gcc
mailing list, can anyone think of a reason why, as a C program, this
should get a diagnostic, but not as a C++ program?

Neither language requires a diagnostic.
Jul 19 '05 #3
Tom
I wrote:
I assume this is compiler-specific - i.e., a bug (or "feature") in
gcc, not a C++ language issue. But before I go complaining to the gcc
mailing list, can anyone think of a reason why, as a C program, this
should get a diagnostic, but not as a C++ program?

"Ron Natalie" wrote:
Neither language requires a diagnostic.


Agreed. Perhaps I phrased my question carelessly by using the word
"should" instead of "would." Allow me to rephrase it. Does anyone
know of reason, either based on the language specification or common
usage, why a compiler writer would elect to have the compiler issue a
diagnostic in this situation for C code but not for C++ code?

(I recognize that a C++ compiler and a C compiler are separate, but I
hope everyone understands the question I'm asking here.)

Best regards,

Tom
Jul 19 '05 #4

"Jeff Rosenfeld" <sp********@comcast.net> wrote in message news:zb********************@comcast.com...
I don't claim to know for certain, but here's a legitimate model of what
could be happening:

In C, there is only one function named main and it has a known prototype.
The compiler can warn you about it when you use the wrong number of
arguments.

In C++, there can be many functions named main. You defined one of them;
there's nothing for the compiler to complain about. The linker either found
a default main(int,char**)


Main is "special" in both langauges. There is no prototype in C for main.
The compiler is required to recquired to recognize both of the common
forms (void and with the arglist). The same is true of C++. Neither
language limits the implementation from accepting other forms of main.
Jul 19 '05 #5
"Victor Bazarov" <v.********@attAbi.com> wrote in message news:<vg************@corp.supernews.com>...
"Jeff Rosenfeld" <sp********@comcast.net> wrote...
[...]
In C++, there can be many functions named main.[...]


I wonder from where you get this nonsense. The Standard
clearly states in '3.6.1 Main function' subclause: "This
function shall not be overloaded."

Victor


Sure. Overloading is limited to namespaces, however. That means
myNameSpace::main( std::string ) doesn't overload the ::main( )
function.

3.6.1/3 is clear:
The function main shall not be used (3.2) within a program. The
linkage (3.5) of main is implementation-defined. A program that
declares main to be inline or static is ill-formed. The
name main is not otherwise reserved. [Example: member functions,
classes, and enumerations can be called main, as can entities
in other namespaces. ]

used(3.2) means you can't call ::main( ) recursively etc. That's
good, since it allows the compiler to hide the global ctor calls
in the compiled version of main().

Regards,
--
Michiel Salters
Jul 19 '05 #6

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

Similar topics

7
by: Jonas | last post by:
This works fine in Win XP but does not work at all in Win 98. Private WithEvents objIExplorer As InternetExplorer I have to do it like this to get it to work in Win 98 Dim objIExplorer As...
3
by: Julian | last post by:
Hi I am trying to update a date field in my table but some how this simple code does not work, I know the select work because if I write the fields, it will show the data from the table but why...
5
by: me | last post by:
I have a Class Library that contains a Form and several helper classes. A thread gets created that performs processing of data behind the scenes and the Form never gets displayed (it is for debug...
22
by: Robert Bralic | last post by:
CAN anybody tell me any address where I can download some small(1000-2000) lines C++ proghram source. Or send me ,a small(1000-2000) lines C++ program source that I can compille with gpp under...
12
by: Frank Hauptlorenz | last post by:
Hello Out there! I have a DB2 V7.2 Database (Fix11) on Win 2000 Professional. It was before a NT 4 based Domain - now it is a Win 2000 Domain. The database server is a domain member. Now...
0
by: Jarod_24 | last post by:
How does tabindex work in ASP .net pages I dosen't seem to work quite like in regular forms. and there isn't any TabStop property either. 1 .How do you prevent a control form beign "tabbed"....
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
89
by: Cuthbert | last post by:
After compiling the source code with gcc v.4.1.1, I got a warning message: "/tmp/ccixzSIL.o: In function 'main';ex.c: (.text+0x9a): warning: the 'gets' function is dangerous and should not be...
14
by: webEater | last post by:
I have a problem, it's not browser specific, and I don't get a solution. I have an (X)HTML document, I show you a part of it: .... <!--<div class="pad">--> <div id="eventImages"><img src=""...
1
by: =?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?= | last post by:
I get the above error in some of the ASP.NET web applications on a server, and I need some help figuring out how to deal with it. This is a rather long post, and I hope I have enough details that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...

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.