What are the alternatives to switch-case statements in C?2019 Community Moderator ElectionWhat is the difference between #include <filename> and #include “filename”?Why was the switch statement designed to need a break?With arrays, why is it the case that a[5] == 5[a]?What does “static” mean in C?What is the effect of extern “C” in C++?What is the difference between const int*, const int * const, and int const *?Improve INSERT-per-second performance of SQLite?What does the C ??!??! operator do?What is “:-!!” in C code?stdlib.h not working as intended with atoi() function
What is the tangent at a sharp point on a curve?
10 year ban after applying for a UK student visa
When did hardware antialiasing start being available?
UK Tourist Visa- Enquiry
label a part of commutative diagram
Symbolism of 18 Journeyers
Does the Shadow Magic sorcerer's Eyes of the Dark feature work on all Darkness spells or just his/her own?
What is the difference between something being completely legal and being completely decriminalized?
Why is there so much iron?
Unfrosted light bulb
Animating wave motion in water
Why are there no stars visible in cislunar space?
Hackerrank All Women's Codesprint 2019: Name the Product
Knife as defense against stray dogs
Why is this tree refusing to shed its dead leaves?
Jem'Hadar, something strange about their life expectancy
What is 管理しきれず?
How do you justify more code being written by following clean code practices?
Gauss brackets with double vertical lines
Did Nintendo change its mind about 68000 SNES?
What is it called when someone votes for an option that's not their first choice?
is this saw blade faulty?
What will the Frenchman say?
Weird lines in Microsoft Word
What are the alternatives to switch-case statements in C?
2019 Community Moderator ElectionWhat is the difference between #include <filename> and #include “filename”?Why was the switch statement designed to need a break?With arrays, why is it the case that a[5] == 5[a]?What does “static” mean in C?What is the effect of extern “C” in C++?What is the difference between const int*, const int * const, and int const *?Improve INSERT-per-second performance of SQLite?What does the C ??!??! operator do?What is “:-!!” in C code?stdlib.h not working as intended with atoi() function
Here's my code below:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
switch ( c )
case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;
c
add a comment |
Here's my code below:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
switch ( c )
case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;
c
12
Why do you need an alternative? What's wrong with the code you posted?
– UnholySheep
Mar 6 at 22:30
3
You could use a series of if else statements or you could lookup a set of values in a table.
– MoDJ
Mar 6 at 22:35
1
You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than theswitch
statement.
– user3386109
Mar 6 at 22:37
1
Again, you need to state what is wrong with your current code and what you are trying to accomplish.
– Michael Dorgan
Mar 6 at 22:50
add a comment |
Here's my code below:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
switch ( c )
case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;
c
Here's my code below:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
switch ( c )
case 's':
params.s = atoi( optarg );
break;
case 'E':
params.E = atoi( optarg );
break;
case 'b':
params.b = atoi( optarg );
break;
case 't':
trace_file = optarg;
break;
case 'v':
verbosity = 1;
break;
c
c
edited Mar 6 at 22:31
Blastfurnace
14k54259
14k54259
asked Mar 6 at 22:27
Swarnava ChatterjeeSwarnava Chatterjee
1
1
12
Why do you need an alternative? What's wrong with the code you posted?
– UnholySheep
Mar 6 at 22:30
3
You could use a series of if else statements or you could lookup a set of values in a table.
– MoDJ
Mar 6 at 22:35
1
You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than theswitch
statement.
– user3386109
Mar 6 at 22:37
1
Again, you need to state what is wrong with your current code and what you are trying to accomplish.
– Michael Dorgan
Mar 6 at 22:50
add a comment |
12
Why do you need an alternative? What's wrong with the code you posted?
– UnholySheep
Mar 6 at 22:30
3
You could use a series of if else statements or you could lookup a set of values in a table.
– MoDJ
Mar 6 at 22:35
1
You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than theswitch
statement.
– user3386109
Mar 6 at 22:37
1
Again, you need to state what is wrong with your current code and what you are trying to accomplish.
– Michael Dorgan
Mar 6 at 22:50
12
12
Why do you need an alternative? What's wrong with the code you posted?
– UnholySheep
Mar 6 at 22:30
Why do you need an alternative? What's wrong with the code you posted?
– UnholySheep
Mar 6 at 22:30
3
3
You could use a series of if else statements or you could lookup a set of values in a table.
– MoDJ
Mar 6 at 22:35
You could use a series of if else statements or you could lookup a set of values in a table.
– MoDJ
Mar 6 at 22:35
1
1
You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the
switch
statement.– user3386109
Mar 6 at 22:37
You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the
switch
statement.– user3386109
Mar 6 at 22:37
1
1
Again, you need to state what is wrong with your current code and what you are trying to accomplish.
– Michael Dorgan
Mar 6 at 22:50
Again, you need to state what is wrong with your current code and what you are trying to accomplish.
– Michael Dorgan
Mar 6 at 22:50
add a comment |
1 Answer
1
active
oldest
votes
But to answer the question, you could use a bunch of if/else statements:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
if ( c == 's' )
params.s = atoi( optarg );
else if ( c == 'E' )
params.E = atoi( optarg );
else if ( c == 'b' )
params.b = atoi( optarg );
else if ( c == 't' )
trace_file = optarg;
else if ( c == 'v' )
verbosity = 1;
else
// always have some kind of default clause
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033177%2fwhat-are-the-alternatives-to-switch-case-statements-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
But to answer the question, you could use a bunch of if/else statements:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
if ( c == 's' )
params.s = atoi( optarg );
else if ( c == 'E' )
params.E = atoi( optarg );
else if ( c == 'b' )
params.b = atoi( optarg );
else if ( c == 't' )
trace_file = optarg;
else if ( c == 'v' )
verbosity = 1;
else
// always have some kind of default clause
add a comment |
But to answer the question, you could use a bunch of if/else statements:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
if ( c == 's' )
params.s = atoi( optarg );
else if ( c == 'E' )
params.E = atoi( optarg );
else if ( c == 'b' )
params.b = atoi( optarg );
else if ( c == 't' )
trace_file = optarg;
else if ( c == 'v' )
verbosity = 1;
else
// always have some kind of default clause
add a comment |
But to answer the question, you could use a bunch of if/else statements:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
if ( c == 's' )
params.s = atoi( optarg );
else if ( c == 'E' )
params.E = atoi( optarg );
else if ( c == 'b' )
params.b = atoi( optarg );
else if ( c == 't' )
trace_file = optarg;
else if ( c == 'v' )
verbosity = 1;
else
// always have some kind of default clause
But to answer the question, you could use a bunch of if/else statements:
while ( ( c = getopt( argc, argv, "s:E:b:t:vh" ) ) != -1 )
if ( c == 's' )
params.s = atoi( optarg );
else if ( c == 'E' )
params.E = atoi( optarg );
else if ( c == 'b' )
params.b = atoi( optarg );
else if ( c == 't' )
trace_file = optarg;
else if ( c == 'v' )
verbosity = 1;
else
// always have some kind of default clause
answered Mar 6 at 23:12
binkybinky
45227
45227
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033177%2fwhat-are-the-alternatives-to-switch-case-statements-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
12
Why do you need an alternative? What's wrong with the code you posted?
– UnholySheep
Mar 6 at 22:30
3
You could use a series of if else statements or you could lookup a set of values in a table.
– MoDJ
Mar 6 at 22:35
1
You could use an array of function pointers indexed by the character. But there's really no reason to use anything other than the
switch
statement.– user3386109
Mar 6 at 22:37
1
Again, you need to state what is wrong with your current code and what you are trying to accomplish.
– Michael Dorgan
Mar 6 at 22:50