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

function system() from stdlib.h

Hi,

I had write a program for Windows OS and put the following line:

system("pause")

that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?

thanks a lot

Feb 15 '07 #1
38 2543
On Feb 15, 4:48 pm, "britzkrieg" <eap.blitzkr...@gmail.comwrote:
pause the screen until I push a button.
http://c-faq.com/osdep/waitforkey.html

Feb 15 '07 #2
britzkrieg wrote:
Hi,

I had write a program for Windows OS and put the following line:

system("pause")

that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?
Why not use getchar() and discard the return value? It's more portable
than executing OS specific commands.

Feb 15 '07 #3
britzkrieg wrote:
Hi,

I had write a program for Windows OS and put the following line:

system("pause")

that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?
Well, what /I'd/ do is do

program | more

and not bother doing anything in the code. But if you really want
to wait for a character, why not call:

void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}

Pickier people might want to handle EOF. Prettier people might
want to parametrise with a reason message. Perfectionist people
will probably see five things wrong I won't notice until after
I post, or after my coffee.

--
Chris "problematic hedgehog" Dollin
"The path to the web becomes deeper and wider" - October Project

Feb 16 '07 #4

Chris Dollin wrote:
britzkrieg wrote:
Hi,

I had write a program for Windows OS and put the following line:

system("pause")

that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?

Well, what /I'd/ do is do

program | more

and not bother doing anything in the code. But if you really want
to wait for a character, why not call:

void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}
Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form to be
more readable, but I may be in the minority regarding that.
Pickier people might want to handle EOF. Prettier people might
want to parametrise with a reason message. Perfectionist people
will probably see five things wrong I won't notice until after
I post, or after my coffee.
What if stdin is already at end-of-file, when getchar() is invoked?

Feb 16 '07 #5
santosh wrote:
Chris Dollin wrote:
>>
void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}

Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form to be
more readable, but I may be in the minority regarding that.
Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.
>Pickier people might want to handle EOF. Prettier people might
want to parametrise with a reason message. Perfectionist people
will probably see five things wrong I won't notice until after
I post, or after my coffee.

What if stdin is already at end-of-file, when getchar() is invoked?
"Pickier people might want to handle EOF."

--
Chris "electric hedgehog" Dollin
"How am I to understand if you won't teach me?" - Trippa, /Falling/

Feb 16 '07 #6
santosh wrote:
Chris Dollin wrote:
[...]
void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}

Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form to be
more readable, but I may be in the minority regarding that.
There are no benefits other than readability. I'm in the group that
considers {} more readable where a statement without effect is
required. Better yet to me would be a comment, with either form.

while (getchar() != '\n')
/* do nothing */ ;

while (getchar() != '\n')
{ /* do nothing */ }
Pickier people might want to handle EOF. Prettier people might
want to parametrise with a reason message. Perfectionist people
will probably see five things wrong I won't notice until after
I post, or after my coffee.

What if stdin is already at end-of-file, when getchar() is invoked?
Then it may loop forever comparing EOF to '\n' and doing nothing. It's
also possible that EOF is reached during the loop, since input is not
necessarily terminated by a newline character.

Feb 16 '07 #7
santosh said:
>
Chris Dollin wrote:
<snip>
>>
void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}

Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form to be
more readable, but I may be in the minority regarding that.
Obviously both forms are correct; it's a style issue. Personally, I
prefer

while(...)
{
continue;
}

which is even more verbose. But Chris's version is fine and so is yours.
Some compilers, however, will issue a diagnostic message for your
version, not because it is required but because it is helpful to
newbies who plaster semicolons all over their code in sheer
desperation, to not-quite-newbies who aren't completely sure of their
syntax rules, and even to oldbies - we all tismype occasionally.
>Pickier people might want to handle EOF. [...]

What if stdin is already at end-of-file, when getchar() is invoked?
That makes you a pickier people (which is good, and it's why Chris
specifically covered his back with all those spiky bits).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 16 '07 #8
Richard Heathfield wrote:
santosh said:
Chris Dollin wrote:

<snip>
>
void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}
Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form to be
more readable, but I may be in the minority regarding that.

Obviously both forms are correct; it's a style issue. Personally, I
prefer

while(...)
{
continue;
}

which is even more verbose. But Chris's version is fine and so is yours.
Some compilers, however, will issue a diagnostic message for your
version, not because it is required but because it is helpful to
newbies who plaster semicolons all over their code in sheer
desperation, to not-quite-newbies who aren't completely sure of their
syntax rules, and even to oldbies - we all tismype occasionally.
Out of curiosity, can you name a compiler that emits this diagnostic?
I haven't encountered it in gcc, even with the -Wall and -Wextra
switches.

Feb 16 '07 #9
santosh said:

<snip>
>
Out of curiosity, can you name a compiler that emits this diagnostic?
[for the while(condition); construct]

Not off the top of my head, no.

I just tested it with gcc, and it doesn't seem to do this by default, by
which I mean my usual plethora of bizarre gcc switches.

I could test with Borland and Microsoft, but that would involve effort.
:-)

Maybe it was some lint or other (shrug), or maybe I'm just
disremembering. If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Feb 16 '07 #10
britzkrieg wrote:
Hi,

I had write a program for Windows OS and put the following line:

system("pause")

that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?

thanks a lot
"read"

Literally:

#!/bin/bash
read -p "press enter key when ready"
<whatever else>

Now if it's a modern version of bash and you're commited to being bash
specific:

read -s -n 1 -p "press any key when ready"

Silent echo, 1 char max
Feb 16 '07 #11
Christopher Layne wrote:
>that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?

thanks a lot

"read"

Literally:
Also my answer was jsut in general for bash scripts. IF you're trying to
attain the same result on both DOS/Unix platforms, follow the advice already
presented. Definitely do not use system() for this.
Feb 16 '07 #12
Christopher Layne wrote:
#!/bin/bash
read -p "press enter key when ready"
<whatever else>

Now if it's a modern version of bash and you're commited to being bash
specific:

read -s -n 1 -p "press any key when ready"

Silent echo, 1 char max
I haven't test the Christopher's solution yet. But the getchar()
solution isn't work; I dont' know what's going on, but my program get
off without I push a button.

Feb 16 '07 #13
britzkrieg wrote:
I haven't test the Christopher's solution yet. But the getchar()
solution isn't work; I dont' know what's going on, but my program get
off without I push a button.
Well, we can't see your code, so we can't see what you've done
wrong.

But I /guess/ that you did some input earlier and didn't
eat all of it, so your getchar getted a char without you
having to type any more. Solution: don't leave random input
unread. (I have a small bet with my evil twin that it's your
`scanf` that's the root of the problem.)

--
Chris "fgets!" Dollin
The "good old days" used to be much better.

Feb 16 '07 #14
Chris Dollin wrote:
santosh wrote:
.... snip ...
>>
Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form
to be more readable, but I may be in the minority regarding that.

Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.
Try:

while (side_effects) continue;

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 16 '07 #15
Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:

<snip>
>>
Out of curiosity, can you name a compiler that emits this diagnostic?

[for the while(condition); construct]

Not off the top of my head, no.
and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;
The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;

--
Ben.
Feb 16 '07 #16
Ben Bacarisse wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:

<snip>
>
Out of curiosity, can you name a compiler that emits this diagnostic?
[for the while(condition); construct]

Not off the top of my head, no.

and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;

The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;
Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.

Feb 16 '07 #17
santosh wrote:
Ben Bacarisse wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:
>
<snip>
>>
>Out of curiosity, can you name a compiler that emits this diagnostic?
>
[for the while(condition); construct]
>
Not off the top of my head, no.
and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;
The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;

Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
The compiler must behave as if comments are gone, but that only means
they have no effect noticeable from a strictly conforming program.
They may cause other effects, including the number and types of
warnings, so long as those warnings are not required by the standard.
For a related example, at least one compiler warns for

switch (i)
{
case 0:
a();
case 1:
b();
}

but does not for

switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}

(Okay, the version I'm using warns for both. That's a bug, and one
which is not present in the last official release, as far as I can
tell.)

Feb 16 '07 #18
Harald van Dijk wrote:
santosh wrote:
Ben Bacarisse wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:
>
santosh said:

<snip>
>
Out of curiosity, can you name a compiler that emits this diagnostic?

[for the while(condition); construct]

Not off the top of my head, no.
>
and neither can I, but I can confirm the exited once. Might have been
an old Borland one.
>
<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;
>
The one I remember shut up with a comment and I still often write:
>
while (condition) /* do nothing */;
Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.

The compiler must behave as if comments are gone, but that only means
they have no effect noticeable from a strictly conforming program.
They may cause other effects, including the number and types of
warnings, so long as those warnings are not required by the standard.
I personally think it's odd for the compiler to be influenced by
comments. I don't see why they should have any effect on code around
them.
For a related example, at least one compiler warns for
switch (i)
{
case 0:
a();
case 1:
b();
}

but does not for

switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}

(Okay, the version I'm using warns for both. That's a bug, and one
which is not present in the last official release, as far as I can
tell.)
How so? If it warns at all, I'd expect it to warn for both, regardless
of the intervening comment.

Feb 16 '07 #19
santosh wrote:
Harald van Dijk wrote:
santosh wrote:
Ben Bacarisse wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:

santosh said:
>
<snip>
>>
>Out of curiosity, can you name a compiler that emits this diagnostic?
>
[for the while(condition); construct]
>
Not off the top of my head, no.

and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;

The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;
>
Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
The compiler must behave as if comments are gone, but that only means
they have no effect noticeable from a strictly conforming program.
They may cause other effects, including the number and types of
warnings, so long as those warnings are not required by the standard.

I personally think it's odd for the compiler to be influenced by
comments. I don't see why they should have any effect on code around
them.
For cases such as the below, it's the only way I can think of that's
both readable and guaranteed not to affect the runtime behaviour of
any strictly conforming program.
For a related example, at least one compiler warns for
switch (i)
{
case 0:
a();
case 1:
b();
}

but does not for

switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}

(Okay, the version I'm using warns for both. That's a bug, and one
which is not present in the last official release, as far as I can
tell.)

How so? If it warns at all, I'd expect it to warn for both, regardless
of the intervening comment.
It's a bug because the behaviour of warning only for the first code
sample is what's intended, regardless of which behaviour is better.

Feb 16 '07 #20
santosh wrote:
Harald van Dijk wrote:
>santosh wrote:
>>Ben Bacarisse wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:

santosh said:
>
<snip>
>Out of curiosity, can you name a compiler that emits this diagnostic?
[for the while(condition); construct]
>
Not off the top of my head, no.
and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;
The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;
Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
The compiler must behave as if comments are gone, but that only means
they have no effect noticeable from a strictly conforming program.
They may cause other effects, including the number and types of
warnings, so long as those warnings are not required by the standard.

I personally think it's odd for the compiler to be influenced by
comments. I don't see why they should have any effect on code around
them.
>For a related example, at least one compiler warns for
switch (i)
{
case 0:
a();
case 1:
b();
}

but does not for

switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}

(Okay, the version I'm using warns for both. That's a bug, and one
which is not present in the last official release, as far as I can
tell.)

How so? If it warns at all, I'd expect it to warn for both, regardless
of the intervening comment.
These are just warnings. Actually, this does not change the generated
code at all. I suppose the compiler decides not to warn when there is a
comment before the following case, because an experienced programmer
using this behavior of switch/case generally puts a comment there, just
to inform that the absence of the break statement is intentional (and
not an obvious bug as it is generally). Exploiting this behaviour is
very uncommon practice, so best to warn the reader.

Anyway, I don't think this is a "so wise" feature of the compiler. Lots
of people may have the habit to put comments above cases, thus
forgetting a break would almost certainly pass silently.
--
R.N.
Feb 16 '07 #21
Radamanthe wrote:
santosh wrote:
Harald van Dijk wrote:
santosh wrote:
Ben Bacarisse wrote:
Richard Heathfield <rj*@see.sig.invalidwrites:

santosh said:

<snip>
Out of curiosity, can you name a compiler that emits this diagnostic?
[for the while(condition); construct]

Not off the top of my head, no.
and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;
The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;
Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
The compiler must behave as if comments are gone, but that only means
they have no effect noticeable from a strictly conforming program.
They may cause other effects, including the number and types of
warnings, so long as those warnings are not required by the standard.
I personally think it's odd for the compiler to be influenced by
comments. I don't see why they should have any effect on code around
them.
For a related example, at least one compiler warns for
switch (i)
{
case 0:
a();
case 1:
b();
}

but does not for

switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}

(Okay, the version I'm using warns for both. That's a bug, and one
which is not present in the last official release, as far as I can
tell.)
How so? If it warns at all, I'd expect it to warn for both, regardless
of the intervening comment.

These are just warnings. Actually, this does not change the generated
code at all. I suppose the compiler decides not to warn when there is a
comment before the following case, because an experienced programmer
using this behavior of switch/case generally puts a comment there, just
to inform that the absence of the break statement is intentional (and
not an obvious bug as it is generally). Exploiting this behaviour is
very uncommon practice, so best to warn the reader.

Anyway, I don't think this is a "so wise" feature of the compiler. Lots
of people may have the habit to put comments above cases, thus
forgetting a break would almost certainly pass silently.
Only comments containing the word FALLTHROUGH or FALLTHRU are supposed
to cause the warning to be skipped, not just any random comment.

Feb 16 '07 #22
In article <11*********************@a75g2000cwd.googlegroups. com>,
santosh <sa*********@gmail.comwrote:
>switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}
>How so? If it warns at all, I'd expect it to warn for both, regardless
of the intervening comment.
It was traditional for "lint" to use such comments, so I'm not
surprised that a compiler does. Presumably this was one of the
intended uses of pragmas.

-- Richard

--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 16 '07 #23
Harald van Dijk wrote:
Radamanthe wrote:
>santosh wrote:
>>Harald van Dijk wrote:
santosh wrote:
Ben Bacarisse wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>
>>santosh said:
>>>
>><snip>
>>>Out of curiosity, can you name a compiler that emits this diagnostic?
>>[for the while(condition); construct]
>>>
>>Not off the top of my head, no.
>and neither can I, but I can confirm the exited once. Might have been
>an old Borland one.
>>
><snip>
>> If so, then I am also disremembering that one way to
>>quell the warning was to add a space: while(condition) ;
>The one I remember shut up with a comment and I still often write:
>>
> while (condition) /* do nothing */;
Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
The compiler must behave as if comments are gone, but that only means
they have no effect noticeable from a strictly conforming program.
They may cause other effects, including the number and types of
warnings, so long as those warnings are not required by the standard.
I personally think it's odd for the compiler to be influenced by
comments. I don't see why they should have any effect on code around
them.

For a related example, at least one compiler warns for
switch (i)
{
case 0:
a();
case 1:
b();
}

but does not for

switch (i)
{
case 0:
a();
/* FALLTHROUGH */
case 1:
b();
}

(Okay, the version I'm using warns for both. That's a bug, and one
which is not present in the last official release, as far as I can
tell.)
How so? If it warns at all, I'd expect it to warn for both, regardless
of the intervening comment.
These are just warnings. Actually, this does not change the generated
code at all. I suppose the compiler decides not to warn when there is a
comment before the following case, because an experienced programmer
using this behavior of switch/case generally puts a comment there, just
to inform that the absence of the break statement is intentional (and
not an obvious bug as it is generally). Exploiting this behaviour is
very uncommon practice, so best to warn the reader.

Anyway, I don't think this is a "so wise" feature of the compiler. Lots
of people may have the habit to put comments above cases, thus
forgetting a break would almost certainly pass silently.

Only comments containing the word FALLTHROUGH or FALLTHRU are supposed
to cause the warning to be skipped, not just any random comment.
Well, ok.. this is not perfect though, and this could not be anyway. I
can't bet it won't ever happen to me to forget a break before a
commented case with "fallth(rough|thru)" inside it :) So it will
certainly happen to some people in this world. Too bad for him(her).
--
R.N.
Feb 16 '07 #24
"santosh" <sa*********@gmail.comwrites:
Ben Bacarisse wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:
<snip>

Out of curiosity, can you name a compiler that emits this diagnostic?

[for the while(condition); construct]

Not off the top of my head, no.

and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;

The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;

Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
The standard doesn't talk about "compilation proper". Comments vanish
in translation phase 3, which is just as much part of "compilation" as
any other phase. A compiler is perfectly free to decide whether or
not to issue an optional diagnostic based on the presence or absence
(or even the contents) of a comment.

But yes, comment deletion is commonly handled in the "preprocessor",
which is often (but not always) a separate program that runs to
completion before the phase that would issue a warning about the
semicolon. And I'd be a bit surprised if a comment really did inhibit
the warning. I find the described behavior counterintuitive. (Some
versions of lint recognize specially formatted comments as
annotations).

--
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.
Feb 17 '07 #25
santosh wrote:
Ben Bacarisse wrote:
>Richard Heathfield <rj*@see.sig.invalidwrites:
.... snip ...
>>
<snip>
>> If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;

The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;

Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.
Comments are replaced by a blank.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Feb 17 '07 #26
CBFalconer <cb********@yahoo.comwrites:
santosh wrote:
[...]
>Interesting, seeing as, logically, comments are not supposed to be
present at compilation proper.

Comments are replaced by a blank.
And thus are not present (assuming a certain intepretation of
"compilation proper").

--
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.
Feb 17 '07 #27
Keith Thompson <ks***@mib.orgwrites:
"santosh" <sa*********@gmail.comwrites:
>Ben Bacarisse wrote:
>>Richard Heathfield <rj*@see.sig.invalidwrites:
santosh said:
<snip>

Out of curiosity, can you name a compiler that emits this diagnostic?

[for the while(condition); construct]

Not off the top of my head, no.

and neither can I, but I can confirm the exited once. Might have been
an old Borland one.

<snip>
If so, then I am also disremembering that one way to
quell the warning was to add a space: while(condition) ;

The one I remember shut up with a comment and I still often write:

while (condition) /* do nothing */;
<snip>
I find the described behavior counterintuitive. (Some
versions of lint recognize specially formatted comments as
annotations).
It certainly was not lint (or similar). It is possible that this is a
false memory but the code I wrote at about that time (~10 years ago)
has a comment in every empty while.

I have tried to fond some evidence (or even the name of the compiler
concerned) and failed, so this is in danger of becoming no more than a
rumour.

--
Ben.
Feb 17 '07 #28
On 16 Feb 2007 03:04:06 -0800, "santosh" <sa*********@gmail.com>
wrote:
>Richard Heathfield wrote:
>santosh said:
Chris Dollin wrote:

<snip>
>>
void waitForRETURN(void)
{
fprintf( stderr, "press RETURN to continue:" );
fflush( stderr );
while (getchar() != '\n') {}
}

Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form to be
more readable, but I may be in the minority regarding that.

Obviously both forms are correct; it's a style issue. Personally, I
prefer

while(...)
{
continue;
}

which is even more verbose. But Chris's version is fine and so is yours.
Some compilers, however, will issue a diagnostic message for your
version, not because it is required but because it is helpful to
newbies who plaster semicolons all over their code in sheer
desperation, to not-quite-newbies who aren't completely sure of their
syntax rules, and even to oldbies - we all tismype occasionally.

Out of curiosity, can you name a compiler that emits this diagnostic?
I haven't encountered it in gcc, even with the -Wall and -Wextra
switches.
FWIW, PC-lint issues this warning:

Info 722: Suspicious use of ;

Regards
--
jay
Feb 17 '07 #29
On Fri, 16 Feb 2007 07:41:41 -0600, Chris Dollin wrote
(in article <er**********@murdoch.hpl.hp.com>):
britzkrieg wrote:
>I haven't test the Christopher's solution yet. But the getchar()
solution isn't work; I dont' know what's going on, but my program get
off without I push a button.

Well, we can't see your code, so we can't see what you've done
wrong.

But I /guess/ that you did some input earlier and didn't
eat all of it, so your getchar getted a char without you
having to type any more. Solution: don't leave random input
unread. (I have a small bet with my evil twin that it's your
`scanf` that's the root of the problem.)
Made up statistic, but worthy of consideration anyway:

90% of all programs that call scanf() have a bug rooted in the use of
scanf().

:-)
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Feb 18 '07 #30
CBFalconer <cb********@yahoo.comwrote:
Chris Dollin wrote:
santosh wrote:
Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form
to be more readable, but I may be in the minority regarding that.
Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.

Try:

while (side_effects) continue;
That's a good one; another I like is putting the semi-colon on a line of
its own. That's unusual enough to be clear. Like this:

while (side_effects--)
;

Richard
Feb 19 '07 #31
CBFalconer wrote:
Chris Dollin wrote:
>santosh wrote:
... snip ...
>>>
Is there a specific reason for doing while( ... ) {} instead of
while( ... ); other than readability? I consider the latter form
to be more readable, but I may be in the minority regarding that.

Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.

Try:

while (side_effects) continue;
I shan't /try/ it; while I can see that some would find it OK,
it looks like an abomination to me. Of course what I'd /prefer/
is `endwhile` [1] ... but C Is Not Built That Way And I Can't
Change It.

[1] Or `endrepeat`, as in `repeat A while B do C endrepeat`.

--
Chris "electric hedgehog" Dollin
"- born in the lab under strict supervision -", - Magenta, /Genetesis/

Feb 19 '07 #32
Chris Dollin <ch**********@hp.comwrites:
CBFalconer wrote:
[...]
>Try:

while (side_effects) continue;

I shan't /try/ it; while I can see that some would find it OK,
it looks like an abomination to me. Of course what I'd /prefer/
is `endwhile` [1] ... but C Is Not Built That Way And I Can't
Change It.
Of course you can change it:

#define endwhile }
#define endif }
....

(But don't expect anyone to read your code if you commit such an
abomination.)

--
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.
Feb 19 '07 #33
Chris Dollin <ch**********@hp.comwrote:
CBFalconer wrote:
Chris Dollin wrote:
Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.
Try:

while (side_effects) continue;

I shan't /try/ it; while I can see that some would find it OK,
it looks like an abomination to me. Of course what I'd /prefer/
is `endwhile` [1] ... but C Is Not Built That Way And I Can't
Change It.

[1] Or `endrepeat`, as in `repeat A while B do C endrepeat`.
If you want to hack on the Bourne shell source, you know where to find
it. Yuck.

Richard
Feb 19 '07 #34
Richard Bos wrote:
CBFalconer <cb********@yahoo.comwrote:
Chris Dollin wrote:
santosh wrote:
>
>Is there a specific reason for doing while( ... ) {} instead of
>while( ... ); other than readability? I consider the latter form
>to be more readable, but I may be in the minority regarding that.
>
Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.
Try:

while (side_effects) continue;

That's a good one; another I like is putting the semi-colon on a line
of its own. That's unusual enough to be clear. Like this:

while (side_effects--)
;
The first C coding standard I worked under mandated/suggested that. It
works pretty well, although these days I tend to use empty braces:

while (side_effects--)
{
}

Brian
Feb 19 '07 #35
Christopher Layne wrote:
britzkrieg wrote:
>Hi,

I had write a program for Windows OS and put the following line:

system("pause")

that use the "pause" command from DOS to pause the screen until I push
a button. How to get the same result in Linux's bash ?

thanks a lot

"read"

Literally:

#!/bin/bash
read -p "press enter key when ready"
<whatever else>

Now if it's a modern version of bash and you're commited to being bash
specific:

read -s -n 1 -p "press any key when ready"

Silent echo, 1 char max
If you want to exactly duplicate MS-DOS's pause command:

system("read -s -n 1 -p \"Press any key to continue . . . \"; echo;");

The extra echo command is necessary as pause adds a newline after you
press a key.

--
Simon.
Feb 19 '07 #36
Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:
>CBFalconer wrote:
Chris Dollin wrote:
>Readability. I find `;` to be more missable (and more likely to
be an accident) than {}. One's parsecage may vary.

Try:

while (side_effects) continue;

I shan't /try/ it; while I can see that some would find it OK,
it looks like an abomination to me. Of course what I'd /prefer/
is `endwhile` [1] ... but C Is Not Built That Way And I Can't
Change It.

[1] Or `endrepeat`, as in `repeat A while B do C endrepeat`.

If you want to hack on the Bourne shell source, you know where to find
it. Yuck.
I don't. Why would you think I did?

(Yes, I /know/ that the Bourne shell was writting with algolifying
macros. C isn't built that way, and I can't change it, and I
have no interest in trying to fake a better-according-to-me
syntax using macros, for all the reasons behind your and
Keith's expressions of distaste. To get a more pleasing grammar
I go to -- or implement -- other languages.)

--
Chris "electric hedgehog" Dollin
There' no hortage of vowel on Uenet.

Feb 20 '07 #37
Chris Dollin <ch**********@hp.comwrote:
Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:
CBFalconer wrote:

Try:

while (side_effects) continue;

I shan't /try/ it; while I can see that some would find it OK,
it looks like an abomination to me. Of course what I'd /prefer/
is `endwhile` [1] ... but C Is Not Built That Way And I Can't
Change It.

[1] Or `endrepeat`, as in `repeat A while B do C endrepeat`.
If you want to hack on the Bourne shell source, you know where to find
it. Yuck.

I don't. Why would you think I did?
If you want the origin for a standard hacker idiom, you know where to
find it.
(Yes, I /know/ that the Bourne shell was writting with algolifying
macros. C isn't built that way, and I can't change it, and I
have no interest in trying to fake a better-according-to-me
syntax using macros, for all the reasons behind your and
Keith's expressions of distaste.
No, you don't, because at least one of the reasons behind my distaste is
that I find the endrepeat grammar _not_ better, but overly voluble and
frequently less legible.

Richard
Feb 22 '07 #38
Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:
>Richard Bos wrote:
Chris Dollin <ch**********@hp.comwrote:

CBFalconer wrote:

Try:

while (side_effects) continue;

I shan't /try/ it; while I can see that some would find it OK,
it looks like an abomination to me. Of course what I'd /prefer/
is `endwhile` [1] ... but C Is Not Built That Way And I Can't
Change It.

[1] Or `endrepeat`, as in `repeat A while B do C endrepeat`.

If you want to hack on the Bourne shell source, you know where to find
it. Yuck.

I don't. Why would you think I did?

If you want the origin for a standard hacker idiom, you know where to
find it.
That's not an answer.
>(Yes, I /know/ that the Bourne shell was writting with algolifying
macros. C isn't built that way, and I can't change it, and I
have no interest in trying to fake a better-according-to-me
syntax using macros, for all the reasons behind your and
Keith's expressions of distaste.

No, you don't, because at least one of the reasons behind my distaste is
that I find the endrepeat grammar _not_ better, but overly voluble and
frequently less legible.
Ah. Well, it would have helped if you'd said that. Usually when
I've seem people yuck over the Bourne shell its the algolification-
via-macros that's the issue, not the intended syntax. (I'm curious
whether you've seen a language with the repeat-endrepeat syntax,
since the one I know it from is Pop11.)

Grammatical tastes differ.

Um, this is barely topical: if you're bothered to respond, I'll
take email.

--
Chris "electric hedgehog" Dollin
"He's dead, Jim, but not as we know it." Unsaid /Trek/.

Feb 22 '07 #39

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

Similar topics

11
by: Wei Li | last post by:
Hi, In my project , I has to include a head file "alloc.h". The malloc() function was wrapped in this file as : alloc.h #define malloc(a) PROJ_MALLOC(a) alloc.c void *PROJ_MALLOC(size_t...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
2
by: Sam | last post by:
In our C++ program, we are using the system call to execute another C++ program synchronously. The program executed by system runs without error and returns back a 0. Under conditions we cannot...
10
by: Harsh_forC | last post by:
hello folks, here i m inserting d very simple code tat im trying to execute.. but still gettin d same error msg,.." Not enuf memory" #include<stdlib.h> #include<stdio.h> void main(void) {
7
by: Paminu | last post by:
On a gentoo linux system i don't get any warnings when I comlpile this code: #include <stdio.h> typedef struct test { void *content; struct test_ *bob; } test_;
33
by: Chen shuSheng | last post by:
I have a code: --------------------------- #include <iostream.h> #include <stdlib.h> int main() { int max=15; char line; getline(line,max); system("PAUSE");
12
by: sunil | last post by:
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...
11
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...
44
by: climber.cui | last post by:
Hi all, Does anyone have experience on the thread-safty issue with malloc()? Some people said this function provided in stdlib.h is not thread- safe, but someone said it is thread safe. Is it...
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:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.