[techtalk] switch function in C (or how to read commandline args?)

jenn at simegen.com jenn at simegen.com
Sun Jul 1 10:05:41 EST 2001


Conor Daly wrote:

> I want to read a bunch of commandline args or config options from file 
> in a C program.  Now, in bash, I can use a switch structure to do stuff
> based on the *string* presented.  AFAIR, in C, you can only switch on an
> *int*.
> 
> So, how do I use a switch to process my config options? Or do I have to
> build an "if; elseif; else" ladder instead?  I have options in a config
> file that look like

I'm reading a C++ book, not a C book, but what I get is:

switch (expression) {
	case constant1: {
		foo bar baz
		break;
	}
	case constant2: {
		foo bar baz
		break;
	}

}
	
It doesn't have to be an int, but the 'case' statements need constants.

I hope this helps.


Jenn V.
-- 
     "Do you ever wonder if there's a whole section of geek culture
             you miss out on by being a geek?" - Dancer.

jenn at simegen.com     Jenn Vesperman     http://www.simegen.com/~jenn/





More information about the Techtalk mailing list