Is getopt() standard C? etc.

  • Thread starter =?iso-8859-1?q?Jos=E9_de_Paula?=
  • Start date
?

=?iso-8859-1?q?Jos=E9_de_Paula?=

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.
 
J

Joona I Palaste

José de Paula said:
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.
 
R

Runtime

In other news said:
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;
 
N

nrk

José de Paula said:
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.
 
C

Christopher Benson-Manica

José de Paula said:
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.
 
?

=?iso-8859-1?Q?Jos=E9?= de Paula

No, but it is part of POSIX (which is OT in this newsgroup).
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.
 
N

nrk

José de Paula said:
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:
<[email protected]>

-nrk.
 
N

Nimmi Srivastav

José de Paula said:
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
 
?

=?iso-8859-1?q?Jos=E9_de_Paula?=

Em Fri, 30 Jan 2004 15:01:42 -0800, Nimmi Srivastav escreveu:
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.
 
S

Sandeep Sharma

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

Even thought getopt is not part of the standard C library, it is used
quite extensively in the industry for parsing command line arguments.
If you don't have access to the source code, here's one site that has
an implementation for getopt:
http://www.utexas.edu/ftp/source/utilities/getopt/getopt.c

Here's some additional information:
http://www.rahul.net/cgi-bin/userbin/man?topic=getopt&section=3

Hope that helps!
Sandeep
 
?

=?iso-8859-1?q?Jos=E9_de_Paula?=

Em Fri, 30 Jan 2004 15:21:18 -0800, Sandeep Sharma escreveu:
Even thought getopt is not part of the standard C library, it is used
quite extensively in the industry for parsing command line arguments.
If you don't have access to the source code, here's one site that has
an implementation for getopt:
http://www.utexas.edu/ftp/source/utilities/getopt/getopt.c
Thank you, but the Source is with me, Luke! :) The problem is that if I
want to write code that is portable across different operating systems
I'll have to include the wheel (i.e., an implementation of getopt()) with
it.
Hope that helps!

It does, thank you.
 
C

CBFalconer

José de Paula said:
Em Fri, 30 Jan 2004 15:21:18 -0800, Sandeep Sharma escreveu:
Thank you, but the Source is with me, Luke! :) The problem is
that if I want to write code that is portable across different
operating systems I'll have to include the wheel (i.e., an
implementation of getopt()) with it.

And that includes asking any questions beyond "is it standard"
about it on c.l.c. The following is a perfectly standards
compliant getopt:

int getopt(char *p)
{
if (p) return (int)p & 3;
return 0;
} /* getopt */

and somehow I suspect that is not what you are talking about.
 
D

Dan Pop

In said:
Thank you, but the Source is with me, Luke! :) The problem is that if I
want to write code that is portable across different operating systems
I'll have to include the wheel (i.e., an implementation of getopt()) with
it.

Why is that a problem? Just call it mygetopt() and you have a getopt
with the same semantics on all the platforms supporting standard C.

The problems are caused by non-standard functions that cannot be
implemented in pure standard C, but this is not getopt's case.

Dan
 
R

Randy Howard

Why is that a problem? Just call it mygetopt() and you have a getopt
with the same semantics on all the platforms supporting standard C.

The problems are caused by non-standard functions that cannot be
implemented in pure standard C, but this is not getopt's case.

I've been doing that for a very long time, only I call it my_getopt().
It has worked on every platform i've tried, in strict conforming mode,
even Windows. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top