Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Is getopt() standard C? etc.

Reply
Thread Tools

Is getopt() standard C? etc.

 
 
=?iso-8859-1?q?Jos=E9_de_Paula?=
Guest
Posts: n/a
 
      01-30-2004
Is getopt() and its companions, commonly found in GNU libc and other
Unices libc, part of the C standard?

Another doubt: I have a switch inside a while loop; is there a way to
break out of the loop from the switch without using goto? I mean:

start:
while(chr = fgetc(inputfile))
{
switch(chr)
{
case 'a': case 'b':
do_one_stuff(chr);
break;
case 'c': case 'd':
do_some_other_stuff(chr);
break;
case 'e': case 'f':
do_stuff(chr);
break;
default:
goto start;
} /* end switch */
do_even_more_stuff_here();
} /* end while */

Is there a way to get out of the while (i.e., skipping the
"do_even_more_stuff_here();" part) without using this goto?

Thank in advance.

--
Quidquid latine dictum sit altum viditur

 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      01-30-2004
José de Paula <> scribbled the following:
> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?


No.

> Another doubt: I have a switch inside a while loop; is there a way to
> break out of the loop from the switch without using goto? I mean:


> start:
> while(chr = fgetc(inputfile))
> {
> switch(chr)
> {
> case 'a': case 'b':
> do_one_stuff(chr);
> break;
> case 'c': case 'd':
> do_some_other_stuff(chr);
> break;
> case 'e': case 'f':
> do_stuff(chr);
> break;
> default:
> goto start;
> } /* end switch */
> do_even_more_stuff_here();
> } /* end while */


> Is there a way to get out of the while (i.e., skipping the
> "do_even_more_stuff_here();" part) without using this goto?


You could use some sort of flag that you set before the break, and
check before the do_even_more_stuff_here(). If the flag is true,
you can then break or continue the while loop.

--
/-- Joona Palaste () ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
 
Reply With Quote
 
 
 
 
Runtime
Guest
Posts: n/a
 
      01-30-2004
In other news, José de Paula <> typed:
> Another doubt: I have a switch inside a while loop; is there a way to
> break out of the loop from the switch without using goto? I mean:
>


I'd do:

> while(chr = fgetc(inputfile))
> {


int should_break_out = 0;

> switch(chr)
> {
> case 'a': case 'b':
> do_one_stuff(chr);
> break;
> case 'c': case 'd':
> do_some_other_stuff(chr);
> break;
> case 'e': case 'f':
> do_stuff(chr);
> break;
> default:


should_break_out = 1;
break;
> } /* end switch */


if( should_break_out )
break;

> do_even_more_stuff_here();
> } /* end while */
>
> Is there a way to get out of the while (i.e., skipping the
> "do_even_more_stuff_here();" part) without using this goto?
>


--
runtime


 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      01-30-2004
José de Paula wrote:

> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?
>


No, but it is part of POSIX (which is OT in this newsgroup).

> Another doubt: I have a switch inside a while loop; is there a way to
> break out of the loop from the switch without using goto? I mean:
>
> start:
> while(chr = fgetc(inputfile))
> {
> switch(chr)
> {
> case 'a': case 'b':
> do_one_stuff(chr);
> break;
> case 'c': case 'd':
> do_some_other_stuff(chr);
> break;
> case 'e': case 'f':
> do_stuff(chr);
> break;
> default:
> goto start;
> } /* end switch */
> do_even_more_stuff_here();
> } /* end while */
>
> Is there a way to get out of the while (i.e., skipping the
> "do_even_more_stuff_here();" part) without using this goto?
>


What you really want is not break out of the while, but continue (since you
say goto start which is the start of the while loop). A continue will work
just fine as it doesnt have special meaning inside the switch.

If you really want to break out of the while loop, no real easy and elegant
way. You'll have to maintain a possibly useless separate flag and
unnecessarily check it as part of your while condition. A goto might be
cleaner and efficient. Don't be dogmatic in rejecting programming
constructs.

-nrk.

> Thank in advance.
>


--
Remove devnull for email
 
Reply With Quote
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      01-30-2004
José de Paula <> spoke thus:

> while(chr = fgetc(inputfile))


Aside from what others have said, this is incorrect: fgetc() returns
EOF if it fails (whether because of EOF or an error), and you should
test chr against it explicitly. I do hope chr is declared as an int,
by the way.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
 
Reply With Quote
 
Dan Pop
Guest
Posts: n/a
 
      01-30-2004
In <bvdqhf$gfi$> Joona I Palaste <> writes:

>José de Paula <> scribbled the following:
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?

>
>No.


But it can be implemented in standard C and used anywhere a standard C
implementation is available.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:
 
Reply With Quote
 
=?iso-8859-1?Q?Jos=E9?= de Paula
Guest
Posts: n/a
 
      01-30-2004
On 2004-01-30, nrk <> wrote:
> José de Paula wrote:
>
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?
>>

>
> No, but it is part of POSIX (which is OT in this newsgroup).
>


>><snip goto meaning "continue">


>
> What you really want is not break out of the while, but continue (since you
> say goto start which is the start of the while loop). A continue will work
> just fine as it doesnt have special meaning inside the switch.


Yes, what I want is continue, thanks. So in this case I could just use
continue? It feels counter-intuitive, because I'm used to the fact that,
syntactically (sp?), break and continue are the same thing, even though
continue doesn't have any meaning inside switch blocks.

>
> If you really want to break out of the while loop, no real easy and elegant
> way. You'll have to maintain a possibly useless separate flag and
> unnecessarily check it as part of your while condition. A goto might be
> cleaner and efficient. Don't be dogmatic in rejecting programming
> constructs.


Yeah, sure. In this case I don't see the goto as a big problem, because
the label is near the goto, and it's clear what the code does. I rather
prefer it than using the extra variable.

--
Quidquid latine dictum sit altum viditur
 
Reply With Quote
 
nrk
Guest
Posts: n/a
 
      01-30-2004
José de Paula wrote:

> On 2004-01-30, nrk <> wrote:
>> José de Paula wrote:
>>
>>> Is getopt() and its companions, commonly found in GNU libc and other
>>> Unices libc, part of the C standard?
>>>

>>
>> No, but it is part of POSIX (which is OT in this newsgroup).
>>

>
>>><snip goto meaning "continue">

>
>>
>> What you really want is not break out of the while, but continue (since
>> you say goto start which is the start of the while loop). A continue
>> will work just fine as it doesnt have special meaning inside the switch.

>
> Yes, what I want is continue, thanks. So in this case I could just use
> continue?


Yes.

> It feels counter-intuitive, because I'm used to the fact that,
> syntactically (sp?), break and continue are the same thing, even though
> continue doesn't have any meaning inside switch blocks.
>


Well, break and continue are not the same thing at all semantically. One
takes you out of the loop while the other just skips the rest of the code
for the current iteration of the loop. I agree with you on the
counter-intuitive part, as I also see break/continue as a pair that often
go together. The problem probably stems from a lazy compiler writer in the
early days who decided to re-use break for a somewhat similar purpose
instead of introducing a new keyword for use inside switch statements

See Chris Torek's excellent recent post on switch statements for a peek
under the hood:
<>

-nrk.

>>
>> If you really want to break out of the while loop, no real easy and
>> elegant
>> way. You'll have to maintain a possibly useless separate flag and
>> unnecessarily check it as part of your while condition. A goto might be
>> cleaner and efficient. Don't be dogmatic in rejecting programming
>> constructs.

>
> Yeah, sure. In this case I don't see the goto as a big problem, because
> the label is near the goto, and it's clear what the code does. I rather
> prefer it than using the extra variable.
>


--
Remove devnull for email
 
Reply With Quote
 
Nimmi Srivastav
Guest
Posts: n/a
 
      01-30-2004
José de Paula <> wrote in message news:<>...
> Is getopt() and its companions, commonly found in GNU libc and other
> Unices libc, part of the C standard?


Can someone post some working code snippet using getopt()?

Thanks,
Nimmi
 
Reply With Quote
 
=?iso-8859-1?q?Jos=E9_de_Paula?=
Guest
Posts: n/a
 
      01-30-2004
Em Fri, 30 Jan 2004 15:01:42 -0800, Nimmi Srivastav escreveu:

> José de Paula <> wrote in message news:<>...
>> Is getopt() and its companions, commonly found in GNU libc and other
>> Unices libc, part of the C standard?

>
> Can someone post some working code snippet using getopt()?
>

As you wish:

char ch, *flag1, *flag2;
while ( (ch = getopt(argc, argv, "o:f:h")) != -1)
{
switch (ch)
{
case 'o':
flag1 = optarg;
break;
case 'f':
flag2 = optarg;
break;
case 'h':
usage(stdout, EXIT_SUCCESS, argv[0]);
break;
case '?':
default:
usage(stderr, EXIT_FAILURE, argv[0]);
break;
}
}

Here optarg is a global variable defined by libc; it contains the argument
for the option. If you are under a Unix system (*BSD, Linux et al.), man 3
getopt should enlighten you. However, as pointed elsewhere in this thread,
getopt() is not portable outside Unix systems.

--
Quidquid latine dictum sit altum viditur

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
standard libraries don't behave like standard 'libraries' Sriram Srinivasan Python 13 11-12-2009 06:05 PM
What are the standard network functions provided in standard C? disappearedng@gmail.com C Programming 5 06-10-2008 08:57 PM
How to redirect a "system" standard output and standard error to avariable (Linux) Venks Ruby 5 12-06-2007 12:21 AM
add pexpect to the standard library, standard "install" mechanism. funkyj Python 5 01-20-2006 08:35 PM
How standard is the standard library? steve.leach Python 1 04-18-2005 04:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57