473,796 Members | 2,585 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2072
"sunil" <su*******@gmai l.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*******@gmai l.comwrote:

>>#include<stdl ib.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.nlwr ote in message
news:45******** *******@news.xs 4all.nl...
"sunil" <su*******@gmai l.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.re mcomp.frwrote in message
news:45******** *************** @news.orange.fr ...
Richard Bos wrote:
"sunil" <su*******@gmai l.comwrote:

>#include<stdli b.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.re mcomp.frwrote in message
news:45******** *************** @news.orange.fr ...
>>Richard Bos wrote:
>>>"sunil" <su*******@gmai l.comwrote:

#include<st dlib.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.re mcomp.frwrites:
Rod Pemberton wrote:
>"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:45******* *************** *@news.orange.f r...
>>>Richard Bos wrote:
"sunil" <su*******@gmai l.comwrote:

>#include<s tdlib.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_Keit h) 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.re mcomp.frwrites:
>>Rod Pemberton wrote:
>>>"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:45***** *************** ***@news.orange .fr...

Richard Bos wrote:

>"sunil" <su*******@gmai l.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.6931471805599 45309417232121;

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("Val ue 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.re mcomp.frwrites:
Keith Thompson wrote:
>jacob navia <ja***@jacob.re mcomp.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.6931471805599 45309417232121;

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("Val ue 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_Keit h) 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

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

Similar topics

1
1461
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 doesn't return any result like php exec function. I want to know about php equivalent exec function in Python. So If anyone know regarding this Kindly Mail me. with regards
1
1569
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 to system function then It will return 0 only for both conditions(success and failure). So What I need to do for this. I want the result like php system
0
1595
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 for fgets() using fgetc() and realloc() to read a line of > arbitrary length. I know that the better programmers in this group could > write a more robust function, but here is my shot at it anyway. > I would appreciate people's comments on my...
3
2991
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
1566
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 decrement to facilitate a movenext and movelast. I have been pulling my hair out and am at the end of my rope. PLEASE, how should I implement this? I have tried everything I can think of, which really isn't much considering I am just beginning...
1
1316
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 machine. I tested this code in VB.net 2003 in a WINDOW FORM class and it seems to work fine. However, when I test it in VB.net 2005 Express Edition it works for the login portion but it doesn't display the error message if the user name and password...
8
2328
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 calculates loan amortization schedules, writes the data to a text file, and then upon user prompt, the program will display the created file and print the file. Until this morning, everything worked fine, and then I started messing around with...
4
2313
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 console window: the display would be like: Hello XXX(user) Today is XXX(date) The Time is XXX(current system time) I was asked to use a Windows System Call---void GetLocalTime(SYSTIME
2
3800
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 Align; union header { struct {
0
9673
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9525
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10221
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...
0
10003
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...
1
7546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6785
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();...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.