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

Is getopt() standard C? etc.

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

Nov 14 '05 #1
14 3829
José de Paula <jo***********@ig.com.br> 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 (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Insanity is to be shared."
- Tailgunner
Nov 14 '05 #2
In other news, José de Paula <jo***********@ig.com.br> 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
Nov 14 '05 #3
nrk
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
Nov 14 '05 #4
José de Paula <jo***********@ig.com.br> 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.
Nov 14 '05 #5
In <bv**********@oravannahka.helsinki.fi> Joona I Palaste <pa*****@cc.helsinki.fi> writes:
José de Paula <jo***********@ig.com.br> 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: Da*****@ifh.de
Nov 14 '05 #6
On 2004-01-30, nrk <ra*********@devnull.verizon.net> 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
Nov 14 '05 #7
nrk
José de Paula wrote:
On 2004-01-30, nrk <ra*********@devnull.verizon.net> 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:
<bu********@enews3.newsguy.com>

-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
Nov 14 '05 #8
José de Paula <jo***********@ig.com.br> wrote in message news:<pa****************************@ig.com.br>...
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
Nov 14 '05 #9
Em Fri, 30 Jan 2004 15:01:42 -0800, Nimmi Srivastav escreveu:
José de Paula <jo***********@ig.com.br> wrote in message news:<pa****************************@ig.com.br>...
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

Nov 14 '05 #10
José de Paula <jo***********@ig.com.br> wrote in message news:<pa****************************@ig.com.br>...
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/uti...etopt/getopt.c

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

Hope that helps!
Sandeep
Nov 14 '05 #11
Em Fri, 30 Jan 2004 15:21:18 -0800, Sandeep Sharma escreveu:
José de Paula <jo***********@ig.com.br> wrote in message news:<pa****************************@ig.com.br>...
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/uti...etopt/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.

--
Quidquid latine dictum sit altum viditur

Nov 14 '05 #12
José de Paula wrote:
Em Fri, 30 Jan 2004 15:21:18 -0800, Sandeep Sharma escreveu:
José de Paula <jo***********@ig.com.br> wrote in message

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/uti...etopt/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.


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #13
In <pa****************************@ig.com.br> =?iso-8859-1?q?Jos=E9_de_Paula?= <jo***********@ig.com.br> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #14
In article <bv**********@sunnews.cern.ch>, Da*****@cern.ch says...
In <pa****************************@ig.com.br> =?iso-8859-1?q?Jos=E9_de_Paula?= <jo***********@ig.com.br> writes:
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.


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. :-)

--
Randy Howard
2reply remove FOOBAR

Nov 14 '05 #15

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

Similar topics

6
by: David Bear | last post by:
I'm stumped. Trying to follow the docs and .. failure. here's the args >>> args '-Middwb@mainex1.asu.edu -AKHAAM@prlinux+898 -CA --D2003-08-20-09:28:13.417 -Ff -Hprlinux...
3
by: Dominik Kaspar | last post by:
I tried to use getopt and copied the example from: http://www.python.org/doc/current/lib/module-getopt.html but nothing is working... getopt.GetoptError doesn't seem to exist and when i run the...
3
by: Don Low | last post by:
Hi, I'm going over a script that demonstrates the getopt function. I include the script here: #! /usr/bin/python import sys, getopt, string
1
by: M.N.A.Smadi | last post by:
hi; I have a perl script that I need to port to python. The script takes input from the command line. Is there a standard way of processing command line arguments based on the -flag preceeding...
1
by: Shaun Jackman | last post by:
I'd like to call getopt with one set of arguments, and once that's completely done call it again with an entirely different set of arguments. I've found though that at least with the getopt...
18
by: k_over_hbarc | last post by:
What is the correct format of getopt() and how does it work? I looked at the man page; it doesn't really clarify things. What's a program that uses it, maybe I could figure it out from that. ...
4
by: pinkfloydhomer | last post by:
I want to be able to do something like: myscript.py * -o outputfile and then have the shell expand the * as usual, perhaps to hundreds of filenames. But as far as I can see, getopt can only...
1
by: Daniel Mark | last post by:
Hello all: I am using getopt package in Python and found a problem that I can not figure out an easy to do . // Correct input D:\>python AddArrowOnImage.py --imageDir=c:/ // Incorrect...
14
by: Markus Mayer | last post by:
Hi. Any chances that there is a similar thing to *nix's getopt() under windows? (It doesn't actually have to be too nifty, the requirement is just that it is stable.) Regards, Markus
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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,...
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
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.