473,387 Members | 1,579 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,387 software developers and data experts.

Regarding system function

Hi All,

Please have a look the below program

#include<stdlib.h>
int i = system("pwd");

I compiled the above program in UNIX ,it got compiled and
executed with out any errors.It prints the present working directory.Am
having certain doubts about this program.

1) without main how it got executed
2)is the system funcion is static
3) is the shell had executed that system command

if i write simply
#include<stdlib.h>
system("pwd")
it's thwoing errors at compilation stage

4) how return type is playing a role,is it mandatory
to collect the return type in C(upto my knowledge it's not mandatory to
collect the return type).

In case of any queries please revert me back.

Regards
Sunil

Oct 6 '06 #1
12 2036
"sunil" <su*******@gmail.comwrote:
#include<stdlib.h>
int i = system("pwd");

I compiled the above program in UNIX ,it got compiled and
executed with out any errors.
Well, that's wrong, then. Time to hitch up the warning level on your
compilations, or to stop compiling GNU almost-but-not-quite-C.

From the Standard, paragraph 6.7.8, item 4: "All the expressions in an
initializer for an object that has static storage duration shall be
constant expressions or string literals". A function call is not a
constant expression or a string literal; therefore, this code is not
correct C.
It prints the present working directory.Am
having certain doubts about this program.

1) without main how it got executed
It shouldn't have been.
2)is the system funcion is static
That does not make sense. Objects can have static duration; functions
just _are_.
3) is the shell had executed that system command
Well, yes, of course. That's what system() is for, after all.
4) how return type is playing a role,is it mandatory
to collect the return type in C(upto my knowledge it's not mandatory to
collect the return type).
No.
In case of any queries please revert me back.
Yer_what_? "Please revert me back"? Ok, if you say so... If someone
sends in a query I'll ditch today's version of you and go back to the
backup you made yesterday.

Richard
Oct 6 '06 #2
Richard Bos wrote:
"sunil" <su*******@gmail.comwrote:

>>#include<stdlib.h>
int i = system("pwd");

I compiled the above program in UNIX ,it got compiled and
executed with out any errors.


Well, that's wrong, then. Time to hitch up the warning level on your
compilations, or to stop compiling GNU almost-but-not-quite-C.

From the Standard, paragraph 6.7.8, item 4: "All the expressions in an
initializer for an object that has static storage duration shall be
constant expressions or string literals". A function call is not a
constant expression or a string literal; therefore, this code is not
correct C.

>>It prints the present working directory.Am
having certain doubts about this program.

1) without main how it got executed


It shouldn't have been.

> 2)is the system funcion is static


That does not make sense. Objects can have static duration; functions
just _are_.

> 3) is the shell had executed that system command


Well, yes, of course. That's what system() is for, after all.

> 4) how return type is playing a role,is it mandatory
to collect the return type in C(upto my knowledge it's not mandatory to
collect the return type).


No.

>>In case of any queries please revert me back.


Yer_what_? "Please revert me back"? Ok, if you say so... If someone
sends in a query I'll ditch today's version of you and go back to the
backup you made yesterday.

Richard
What is more mysterious is that the function calls "pwd".

But this will be the pwd where the COMPILER is, since the
compiler calls "pwd". It will NOT be the directory where
the program will be since the poor program is not even compiled yet!
Oct 6 '06 #3

"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
news:45***************@news.xs4all.nl...
"sunil" <su*******@gmail.comwrote:
#include<stdlib.h>
int i = system("pwd");

I compiled the above program in UNIX ,it got compiled and
executed with out any errors.

Well, that's wrong, then. Time to hitch up the warning level on your
compilations, or to stop compiling GNU almost-but-not-quite-C.
No reason to pick on GCC. The very old version of GCC I'm using, gives a
warning and fails to compiler _without_ any extra options such as '-Wall' or
'-pedantic'.
Rod Pemberton
Oct 6 '06 #4

"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr...
Richard Bos wrote:
"sunil" <su*******@gmail.comwrote:

>#include<stdlib.h>
int i = system("pwd");

What is more mysterious is that the function calls "pwd".

But this will be the pwd where the COMPILER is, since the
compiler calls "pwd". It will NOT be the directory where
the program will be since the poor program is not even compiled yet!
Jacob?

He stated that "pwd - print working directory" is either a shell command or
an equivalent external previously compiled program. It isn't the snippet he
posted. I don't believe his program works, but if does work as claimed,
system() would attempt to execute "pwd" when the program is run. It may not
find "pwd" on the default directories. It may or may not preference the
shell command over an executable of the same name. etc...
Rod Pemberton
Oct 6 '06 #5
Rod Pemberton wrote:
"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr...
>>Richard Bos wrote:
>>>"sunil" <su*******@gmail.comwrote:

#include<stdlib.h>
int i = system("pwd");

What is more mysterious is that the function calls "pwd".

But this will be the pwd where the COMPILER is, since the
compiler calls "pwd". It will NOT be the directory where
the program will be since the poor program is not even compiled yet!


Jacob?

He stated that "pwd - print working directory" is either a shell command or
an equivalent external previously compiled program. It isn't the snippet he
posted.
Ahh OK, I misunderstood. I thought that under GNU at global level:

int i = system("pwd");

would be executed at compile time, like

double p = sqrt(47.8);
I don't believe his program works, but if does work as claimed,
system() would attempt to execute "pwd" when the program is run. It may not
find "pwd" on the default directories. It may or may not preference the
shell command over an executable of the same name. etc...
Rod Pemberton

Oct 6 '06 #6
jacob navia <ja***@jacob.remcomp.frwrites:
Rod Pemberton wrote:
>"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr.. .
>>>Richard Bos wrote:
"sunil" <su*******@gmail.comwrote:

>#include<stdlib.h>
>int i = system("pwd");

What is more mysterious is that the function calls "pwd".

But this will be the pwd where the COMPILER is, since the
compiler calls "pwd". It will NOT be the directory where
the program will be since the poor program is not even compiled yet!
Jacob?
He stated that "pwd - print working directory" is either a shell
command or
an equivalent external previously compiled program. It isn't the snippet he
posted.

Ahh OK, I misunderstood. I thought that under GNU at global level:

int i = system("pwd");

would be executed at compile time, like

double p = sqrt(47.8);
Um, no. Function calls happen during execution; they can be optimized
away and evaluated during compilation only if the compiler can prove
that it makes no difference to the result or side effects. The
compiler (if it's sufficiently clever) knows what sqrt() does, and
knows that it has no side effects and depends only on the value of its
argument. No such optimization is possible for system().

Don't take this personally, but I have no idea how you could have
gotten the idea that system("pwd") could be evaluated during
compilation. (I trust your own compiler doesn't try to do this.)

(By "GNU" you mean "gcc", yes?)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 6 '06 #7
Keith Thompson wrote:
jacob navia <ja***@jacob.remcomp.frwrites:
>>Rod Pemberton wrote:
>>>"jacob navia" <ja***@jacob.remcomp.frwrote in message
news:45***********************@news.orange.fr.. .

Richard Bos wrote:

>"sunil" <su*******@gmail.comwrote:
>
>
>>#include<stdlib.h>
>>int i = system("pwd");

What is more mysterious is that the function calls "pwd".

But this will be the pwd where the COMPILER is, since the
compiler calls "pwd". It will NOT be the directory where
the program will be since the poor program is not even compiled yet!

Jacob?
He stated that "pwd - print working directory" is either a shell
command or
an equivalent external previously compiled program. It isn't the snippet he
posted.

Ahh OK, I misunderstood. I thought that under GNU at global level:

int i = system("pwd");

would be executed at compile time, like

double p = sqrt(47.8);


Um, no. Function calls happen during execution; they can be optimized
away and evaluated during compilation only if the compiler can prove
that it makes no difference to the result or side effects. The
compiler (if it's sufficiently clever) knows what sqrt() does, and
knows that it has no side effects and depends only on the value of its
argument. No such optimization is possible for system().

Don't take this personally, but I have no idea how you could have
gotten the idea that system("pwd") could be evaluated during
compilation. (I trust your own compiler doesn't try to do this.)

(By "GNU" you mean "gcc", yes?)
We had:

#include<stdlib.h>
int i = system("pwd");

I have been playing with the idea since a long time of allowing
global level initializations evaluated at run time.

For instance
double k = log(2);
instead of
double k = 0.693147180559945309417232121;

I haven't implemented this because (precisely) there are
functions that would make problems, "system" being one of
them, or even

double d = get_number("Value of d for this compilation");

When I saw that "program", I coumld not immediately interpret it,
and somehow I thought that my extensions ideas were implemented
already under GNU, what left me quite impressed.

Apparently I misunderstood. They will put those into their
C++ .ini sections probably, and the functions get called
like the constructors of C++.

Does this satisfy your curiosity?

jacob
Oct 6 '06 #8
jacob navia wrote:
I have been playing with the idea since a long time of allowing
global level initializations evaluated at run time.

DAMM!!!!
not run time but COMPILE TIME sorry.
Oct 6 '06 #9
jacob navia <ja***@jacob.remcomp.frwrites:
Keith Thompson wrote:
>jacob navia <ja***@jacob.remcomp.frwrites:
[...]
>>>Ahh OK, I misunderstood. I thought that under GNU at global level:

int i = system("pwd");

would be executed at compile time, like

double p = sqrt(47.8);
Um, no. Function calls happen during execution; they can be
optimized
away and evaluated during compilation only if the compiler can prove
that it makes no difference to the result or side effects. The
compiler (if it's sufficiently clever) knows what sqrt() does, and
knows that it has no side effects and depends only on the value of its
argument. No such optimization is possible for system().
Don't take this personally, but I have no idea how you could have
gotten the idea that system("pwd") could be evaluated during
compilation. (I trust your own compiler doesn't try to do this.)
(By "GNU" you mean "gcc", yes?)

We had:

#include<stdlib.h>
int i = system("pwd");

I have been playing with the idea since a long time of allowing
global level initializations evaluated at run time.

For instance
double k = log(2);
instead of
double k = 0.693147180559945309417232121;

I haven't implemented this because (precisely) there are
functions that would make problems, "system" being one of
them, or even

double d = get_number("Value of d for this compilation");

When I saw that "program", I coumld not immediately interpret it,
and somehow I thought that my extensions ideas were implemented
already under GNU, what left me quite impressed.

Apparently I misunderstood. They will put those into their
C++ .ini sections probably, and the functions get called
like the constructors of C++.

Does this satisfy your curiosity?
Yes it does; thank you for clearing this up.

I had missed the fact that the declaration
int i = system("pwd");
was outside any function, and that i is therefore of static storage
duration.

In standard C, "all the expressions in an initializer for an object
that has static storage duration shall be constant expressions or
string literals", i.e., they must be evaluable at compile time. That
explains why you assumed that the initializer for a static object must
be evaluated at compile time even if (as an extension) it's not a
constant expression. I wouldn't have made that assumption myself (for
one thing, consider cross-compilers), but it's understandable.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 6 '06 #10
jacob navia <ja***@jacob.remcomp.frwrites:
jacob navia wrote:
>I have been playing with the idea since a long time of allowing
global level initializations evaluated at run time.

DAMM!!!!
not run time but COMPILE TIME sorry.
Really? Global level initializations are normally evaluated at
compile time. Allowing them at run time might be an interesting
extension. Your initial statement made sense.

If you're talking about evaluating things like system("pwd") at
compile time, that just seems counterintuitive and not particularly
useful. If you really want to provide a feature like that, I suggest
doing it via some special construct that makes it clear what's
happening, perhaps:

int i = __COMPILE_TIME__(system("pwd"));

It would never have occured to me that, without such a marker, the
system() call would be evaluted before the program's actual execution,
and I suspect most programmers would make the same assumption I do.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Oct 6 '06 #11
sunil wrote:
Please have a look the below program

#include<stdlib.h>
int i = system("pwd");

I compiled the above program in UNIX ,it got compiled and
executed with out any errors.It prints the present working directory.Am
having certain doubts about this program.

1) without main how it got executed
I can't explain this; a program without a main function will not link
under any of the compiler systems I have here (including with GCC, the
most common C compiler on Unix systems).

I'm very curious to know what compiler will produce an executable for
your program.

C++ compilers will _compile_ your program fine, as it is allowed for you
to use non-constant initialisers. The system() function would be called
during program initialisation stage, before main function starts executing.

But most systems won't link it into an executable without a main
function (or other implementation specific form, like WinMain).

--
Simon.
Oct 8 '06 #12
"Rod Pemberton" <do*********@bitfoad.cmmwrote:
>
"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
news:45***************@news.xs4all.nl...
"sunil" <su*******@gmail.comwrote:
#include<stdlib.h>
int i = system("pwd");
>
I compiled the above program in UNIX ,it got compiled and
executed with out any errors.
Well, that's wrong, then. Time to hitch up the warning level on your
compilations, or to stop compiling GNU almost-but-not-quite-C.

No reason to pick on GCC. The very old version of GCC I'm using, gives a
warning and fails to compiler _without_ any extra options such as '-Wall' or
'-pedantic'.
And newer versions? Really, I can't imagine any other compiler which
would compile that code without special options, on Unix. MS C++## might
do it, but it would result in "Bad command or file name".

Richard
Oct 9 '06 #13

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

Similar topics

1
by: Mosas | last post by:
Dear All, Php has exec function to run system commands and this function return last line of system command result. But in Python I can see only os.system() function to run system command but it...
1
by: praba kar | last post by:
Dear All, In Php If I send a command to system function then It will return 1 on success and 0 on failure. So based upon that value I can to further work. But In Python If I send a command...
0
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement...
3
by: Marcelo | last post by:
Hello, I am trying to create a postback event, and it is working, just not calling the sub. I have a datagrid which has <asp:DataGrid id="Mensajes" ... <Columns> <asp:HyperLinkColumn ....
3
by: Aaron | last post by:
Why do my session values return to nothing on post back? I want to click a button and have the row, as in: dataset.table(0).rows(THIS ROW NUMBER IS WHAT I AM TALKING ABOUT), either increment or...
1
by: Terrance | last post by:
Hello, I was wondering if someone can help me understand something. Below is some code that I got from the MS website that is suppose to authenticate for the username and password on the local...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
4
by: Benny Van | last post by:
Hi all! I have a question regarding a windows operating system function: I was asked to write a small program for a homework to display the user name and computer name and the system time out to a...
2
by: somenath | last post by:
Hi All, I have one question regarding the alignment of pointer returned by malloc. In K&R2 page number 186 one union is used to enforce the alignment as mentioned bellow. typedef long...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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,...
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.