ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
The general form is
Code:
switch (variable) {
case value:
do something;
break;
case other value:
do something else;
break;
case third value: /* this value */
case fourth value: /* or this value */
do something;
break;
default:
break;
}
where "value" is an integer (you can enclose a character [not a string] in single quotes and "something" is a function, arithmetic, or whatever.
Here's an example for processing command line arguments with getopt()
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#ifndef TRUE
# define TRUE 1
#endif
#ifndef FALSE
# define FALSE 0
#endif
int main (int argc, char *argv [])
{
int c; /* general-purpose */
int error = FALSE; /* error flag */
int vopt = FALSE; /* verbose option */
time_t t0 = (time_t) 0; /* start time */
time_t t1 = (time_t) 0; /* finish time */
FILE *in;
/* process the command line arguments */
while ((c = getopt (argc, argv, "?v")) != EOF) {
switch (c) {
case '?':
error = TRUE;
break;
case 'v':
vopt = TRUE;
break;
default:
(void) fprintf (stderr, "getopt() bug\n");
exit (EXIT_FAILURE);
}
}
/* any errors in the arguments, or a '?' entered...*/
if (error) {
(void) fprintf (stderr, "usage: %s [-v] argument...\n",
argv [0]);
exit (EXIT_FAILURE);
}
Yes, although a switch/case blocks appear to be a series of if/else if... blocks, they are really just a single goto statement with a series of labels. That's why the case statements need to have a compiled-in constant.
ta0kira
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.