The reason is an error caused by my declaration of variables after the case
Analysis of this problem:
Due to switch a few case statement in the same scope (because a case statement is label, but they belong to a switch beyond constant-like block), so if in some case the following statement variables, the scope of the object is between two curly braces, that is, the entire switch statement, other case statement can also see that such things can lead to errors. We can do this by adding curly braces to the statements that follow the case. The reason for the braces is to specify the scope of the variables we are declaring, just in this case. In fact, to write the switch-case statement more formally, we should add curly braces after the case statement.
Source code is as follows:
Analysis of this problem:
Due to switch a few case statement in the same scope (because a case statement is label, but they belong to a switch beyond constant-like block), so if in some case the following statement variables, the scope of the object is between two curly braces, that is, the entire switch statement, other case statement can also see that such things can lead to errors. We can do this by adding curly braces to the statements that follow the case. The reason for the braces is to specify the scope of the variables we are declaring, just in this case. In fact, to write the switch-case statement more formally, we should add curly braces after the case statement.
Source code is as follows:
#include <stdio.h>
int main(int argc, char *argv[])
{
int zt_num1 = 1, zt_num2 = 2;
if (argc < 2) {
printf("please input again\n");
return -1;
}
if (!argv[1]) {
printf("please input again\n");
return -1;
}
switch(*argv[1])
{
case '+':
zt_num1 = zt_num1 + zt_num2;
break;
case '$':
zt_num1 = zt_num1 - zt_num2;
break;
case '#':
int num = 3;
zt_num1 = zt_num1 + zt_num2 + num;
break;
default:
printf("please input again\n");
break;
}
printf("%d\n", zt_num1);
return 0;
}
The compilation results are as follows:
The modified code is as follows:
#include <stdio.h>
int main(int argc, char *argv[])
{
int zt_num1 = 1, zt_num2 = 2;
if (argc < 2) {
printf("please input again\n");
return -1;
}
if (!argv[1]) {
printf("please input again\n");
return -1;
}
switch(*argv[1])
{
case '+':
zt_num1 = zt_num1 + zt_num2;
break;
case '$':
zt_num1 = zt_num1 - zt_num2;
break;
case '#':
{
int num = 3;
zt_num1 = zt_num1 + zt_num2 + num;
break;
}
default:
printf("please input again\n");
break;
}
printf("%d\n", zt_num1);
return 0;
}
The compilation and operation results are:
Read More:
- How to Fix Warning: Statement lambda can be replaced with expression lambda
- How to Fix Invalid bound statement (not found) Error
- Unity “Feature `out variable declaration’ cannot be used because it is not part of the C# 4.0” error
- error: declaration may not appear after executable statement in block
- C language error: expected declaration or statement at end of input
- error: expected declaration or statement at end of input
- Error: unclassifiable statement in FORTRAN do statement
- [solution] warning: this statement may fall through [- wimplicit fallthrough]) encountered during GCC compilation
- Python switch / case statement implementation method
- Cause check of unreachable statement exception in Java
- C++ error: jump to case label crosses initialization
- SQL Error (3621): String or binary data would be truncated The statement has been terminated. */
- How to Fix Session is not Connecting (How to Diagnose it)
- [Solved] org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.zyh.springboot.
- Invalid bound statement (not found) of custom SQL in mybatisplus
- Git bash display error can not be used, how to solve
- Error c2951: template declaration can only be used at the global, namespace, or class scope. Error c2598: link specification must be at the global scope
- How to Fix char cannot be dereferenced Error
- Spring boot can’t scan XML (invalid bound statement (not found))
- error C2057: expected constant expression (Can the size of an array in C language be defined when the program is running?)