By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,913 Members | 1,263 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,913 IT Pros & Developers. It's quick & easy.

How command line arguments are passed ?

P: n/a
Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so
on.
Now, my question how does the console window pass command line arguments to
the executable with out knowing whether it is a C or Java executable.

Thanking In Advance,
Subhransu Sahoo
Jul 23 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Subhransu Sekhar Sahoo wrote:

Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so
on.
Now, my question how does the console window pass command line arguments to
the executable with out knowing whether it is a C or Java executable.


It doesn't.
The 'console window' passes arguments in always the same way. The key
point is, that it doesn't pass those arguments to your main() (or the
equivalent in other languages) directly. Before main() runs other code
gets started, ( which eg. initializes global variables, sets up the
memory management etc. ) and this code knows about the way the
arguments from the console are passed and what main() expects.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2

P: n/a
Sorry, I I don't understand how the code that you are refering to knows
about the calling convention that main() follows.
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...
Subhransu Sekhar Sahoo wrote:

Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so on.
Now, my question how does the console window pass command line arguments to the executable with out knowing whether it is a C or Java executable.


It doesn't.
The 'console window' passes arguments in always the same way. The key
point is, that it doesn't pass those arguments to your main() (or the
equivalent in other languages) directly. Before main() runs other code
gets started, ( which eg. initializes global variables, sets up the
memory management etc. ) and this code knows about the way the
arguments from the console are passed and what main() expects.
--
Karl Heinz Buchegger
kb******@gascad.at

Jul 23 '05 #3

P: n/a
Subhransu Sekhar Sahoo wrote:
Hi,

I have a very basic qus. An executable can be written using n number of
languages, viz C,,C++,Perl,Java,...And all these languages use different
calling convensions for example C uses cdecl and C++ uses fast call and so
on.
Now, my question how does the console window pass command line arguments to
the executable with out knowing whether it is a C or Java executable.

Thanking In Advance,
Subhransu Sahoo


executing a program does not mean calling main or whatever directly.
The whole thing is very plattform specific and each language has additionally
a wrapper around the main routine which adapts it to the runtime environment.
If you want to know more about this, you should read the documentation
for the linker (e.g. ld) of your development environment...

Tom
Jul 23 '05 #4

P: n/a
On Wed, 13 Jul 2005 11:36:16 GMT, Subhransu Sekhar Sahoo
<su************@yahoo.com> wrote:
Sorry, I I don't understand how the code that you are refering to knows
about the calling convention that main() follows.
the code knows that because it is created by the very same linker
("executable maker") which also processes the code resulting from your
main and other source code.

imagine this: a linker (or linker/compiler pair) is both platform and
programming language dependent. the latter dependency constitues what
source code can be processed, and the former dependency constitutes on
which platform the produced executables can be run.

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...
Subhransu Sekhar Sahoo wrote:
>
> Hi,
>
> I have a very basic qus. An executable can be written using n number

of
> languages, viz C,,C++,Perl,Java,...And all these languages use

different
> calling convensions for example C uses cdecl and C++ uses fast call

and

so
> on.
> Now, my question how does the console window pass command line

arguments

to
> the executable with out knowing whether it is a C or Java executable.
>


It doesn't.
The 'console window' passes arguments in always the same way. The key
point is, that it doesn't pass those arguments to your main() (or the
equivalent in other languages) directly. Before main() runs other code
gets started, ( which eg. initializes global variables, sets up the
memory management etc. ) and this code knows about the way the
arguments from the console are passed and what main() expects.
--
Karl Heinz Buchegger
kb******@gascad.at



Jul 23 '05 #5

P: n/a
Subhransu Sekhar Sahoo wrote:

Sorry, I I don't understand how the code that you are refering to knows
about the calling convention that main() follows.


It knows it, because that code is part of your compiler system.
The very same guys that wrote your compiler also wrote that startup
code, so they know what to do with the passed arguments. The very
sam guys also know on which platform their compiler executes, so they
also know in which way the arguments are passed from the commmand line.

Just in case you haven't guessed it already: That startup code is part
of your executable. When the operating system starts your executable, that
startup code gets the control and gets the arguments passed.
The startup code reformats that arguments and in turn
calls main(), where you eventually get your hands at the arguments.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #6

P: n/a
This does actually not quite relate to the command line argument passing,
but rather about the runtime startup code. The question is does that piece
of startup code run ahead of or after the initialization of global objects
and static objects, or does the startup code actually calls those
constructors?

ben
Jul 23 '05 #7

P: n/a
benben wrote:

This does actually not quite relate to the command line argument passing,
but rather about the runtime startup code. The question is does that piece
of startup code run ahead of or after the initialization of global objects
and static objects, or does the startup code actually calls those
constructors?


I would say: the later.
The startup code collects all global objects and calls constructors as
necessary (some code has to call them). How this is done exactly is highly
implementation specifc.

But make a thought experiment: Assume you write a compiler. How would you
do it? You can do anything you want and are not bound by the C++ rules.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.