A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Error: switch quantity not an integer in C
Q:

Error: switch quantity not an integer in C

0

Error: switch quantity not an integer in C

The switch statement only works with integral type of values/variables, integral types like integer, character.

The error switch quantity not an integer occurs if the value/variables passed in the switch statement is not either integer or character.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Example:

#include <stdio.h>

int main(void) {
	
	float  choice = 2.0f;
	
	switch(choice){
	    case 1:
	        printf("Case 1\n");
	        break;
	    case 2:
	        printf("Case 2\n");
	        break;
	    case 3:
	        printf("Case 3\n");
	        break;
	    case 4:
	        printf("Case 4\n");
	        break;
	    default:
	        printf("Case default\n");
	}
	
	return 0;
}

Output

prog.c: In function ‘main’:
prog.c:7:9: error: switch quantity not an integer
  switch(choice){
         ^~~~~~
prog.c:5:9: warning: variable ‘choice’ set but not used [-Wunused-but-set-variable]
  float  choice = 2.0f;
         ^~~~~~

How to fix?

Use only integral variables/values with the switch statement. In this example, we changed type of choice variable from float to int.

Correct code:

#include <stdio.h>

int main(void) {
	
	int choice = 2;
	switch(choice){
	    case 1:
	        printf("Case 1\n");
	        break;
	    case 2:
	        printf("Case 2\n");
	        break;	    
	    case 3:
	        printf("Case 3\n");
	        break;	    
	    case 4:
	        printf("Case 4\n");
	        break;	    
	    default:
	        printf("Case default\n");
	}
	
	return 0;
}

Output

Case 2

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now