Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 06-06-2008, 08:54 AM   #1
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Rep: Reputation: 32
flag problem in c

I am having a problem which I am unable to resolve. My programme can be executed from the command line with several options, some with an additional argument, some not:

int defaultflag = 0;
int helpflag = 0;
int orderflag =0;
int tmaxflag = 0;
int stepflag = 0;
char *stepvalue = NULL;
char *tmaxvalue = NULL;
char *ordervalue = NULL;
int flagindex;
int flags;
opterr = 0;

while ((flags = getopt (argc, argv, "adhs:t:")) != -1)
	switch (flags)
	case 'd':
	defaultflag = 1;
	case 'o':
	orderflag = 1;
	ordervalue = optarg;
	case 's':
	stepflag = 1;
	stepvalue = optarg;
	case 't':
	tmaxflag = 1;
	tmaxvalue = optarg;
	case 'h':
	helpflag = 1;
	case '?':
	if (optopt == 't' || optopt == 's' || optopt == 'o')
	fprintf (stderr, "Option -%c requires a numerical argument.\n", optopt);
	else if (isprint (optopt))
	fprintf (stderr, "Unknown option `-%c'.\n", optopt);
	fprintf (stderr,
			"Unknown option character `\\x%x'.\n",
	return 1;
for (flagindex = optind; flagindex < argc; flagindex++)
	printf ("Non-option argument %s\n", argv[flagindex]);
The ones without additional arguments are d and h and the ones with arguments are s,t,o, where the values one has to give are used somewhere else in the programme. Now, the problem is that if I execute the programme it works fine except for the -o flag, which always gives me the line "-o requires a numerical argument", even if I give it one. What am I missing here?
Old 06-06-2008, 10:25 AM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
getopt (argc, argv, "adhs:t:")
change to
getopt (argc, argv, "adhs:t:o:")
You evidently added the -o option to your code, but neglected to add it to the options recognized by getopt(). You don't seem to have an argument handler for '-a', although it is in the list of valid args.
--- rod.
Old 06-06-2008, 10:46 AM   #3
Registered: Aug 2005
Distribution: SuSE Linux / Scientific Linux / [K|X]ubuntu
Posts: 273

Original Poster
Rep: Reputation: 32
You're absolutely right! So stupid of me to forget the list... (that's the reason why there still is a valid -a option - it used to be there in the first version). Thanks!!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
What is this Bootable Flag ? lazylogic Debian 5 02-21-2012 07:20 PM
Flag this message I have a problem with gdbm and ndbm bangusilo Linux - Newbie 2 04-29-2008 02:02 PM
About growisofs flag satimis Linux - General 1 01-14-2008 09:21 PM
bootable flag noonmid27 Linux - Hardware 1 12-21-2006 10:37 AM
bootable flag? alaios Debian 1 04-30-2004 12:37 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:31 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration