can we use switch-case statement with strings in c?
Solution 1
No, you can't. Switch is intended to compare numeric types, and for extension char types. Instead you should use the strcmp function, included in string header:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char * argv[]) {
if (argc != 4) {
puts("Incorrect usage");
return 1;
}
/* You should check the number of arguments */
char * op = argv[1];
int a = atoi(argv[2]);
int b = atoi(argv[3]);
/* You should check correct input too */
if (strcmp(op, "+") == 0)
printf("%d + %d = %d\n", a, b, a + b);
else if (strcmp(op, "-") == 0)
printf("%d - %d = %d\n", a, b, a - b);
/* Add more functions here */
return 0;
}
Solution 2
No you can't. The case labels of a switch
need to be compile time evaluable constant expressions with an integral type.
But int
literals like '+'
satisfy that requirement. (As do enum
values for that matter.)
Some folk like to use implementation-defined multi-character literals (e.g. 'eax'
) as case
labels as they claim it helps readability, but at that point, you're giving up consistent behaviour across different platforms.
If you need to branch on the value of a NUL-terminated char
array, then use an if
block.
Solution 3
There are two cases to the answer ..
Firstly 6.8.4.2 (switch
case)
The controlling expression of a switch statement shall have integer type
Secondly 6.8.4.2 (the case
statements)
The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion
Long story short - you can't use string literal like that. Neither in switch controlling expression nor in case
.
You can do the string comparisons using strcmp
and then do the if-else
conditioning. The context on which you ask this, you can simply pass the character +
(argv[2][0]
) instead of passing the whole literal. That way you will be passing char
to the switch
expression and then work accordingly.
Solution 4
Nope, that's not possible.
Quoting C11
, chapter §6.8.4.2
The controlling expression of a
switch
statement shall have integer type.
Solution 5
in your case, you don't seem to need a string but rather the first (and only character) of the string passed in the switch
statement, in that case that's possible using character literal (which has integer type) in the case
statements:
if (strlen(c)==1)
{
switch(c[0]){
case '+': printf(a+b);
break;
...
}
}
some good other alternatives are described in best way to switch on a string in C when the string has multiple characters.
aTechieSmile
Updated on June 04, 2022Comments
-
aTechieSmile almost 2 years
int a = 0 , b = 0; char* c = NULL; int main(int argc , char ** argv){ c = argv[2]; a = atoi(argv[1]); b = atoi(argv[3]); switch(c){ case "+": printf(a+b); break; } printf("\n\n"); return 0; }
-
Bathsheba over 6 yearsI'm sure you know this, but in C,
'+'
is anint
type. -
aTechieSmile over 6 yearsyes !! i've tried this and its worked!! but is this only the way for doing this stuff??
-
Jean-François Fabre over 6 years" In C, a character literal is treated as int type where as in C++, a character literal is treated as char type" but I wasn't aware of that difference, just looked that up. Good to know. who said that C++ was a subset of C?
-
Bathsheba over 6 years@RuchiM.Mewada: There are others but they are putting up an unnecessary fight with the language. This is by far the best way: although I prefer
!strcmp
tostrcmp == 0
. Still gets an upvote though. -
alk over 6 yearsStill, compared to a switch performance can become an issue here. At least it's not performing equal for each value.
-
aTechieSmile over 6 years@alk: yes for the same code I've mentioned above...in case of if(!(strcmp(c == "*"))...it gives me 123 for pf(%s,c) instead of * ...can anyone explain this? is this taking * as an arithmetic multi oprtr or dereference optr??
-
aTechieSmile over 6 yearsthanks to all of u!! I understood and fixed it!! tq