Tag Archives: C language

Use keil software to report syntax error near ‘;

Using keil software to report syntax error near ‘; ”

This is the first time through CSDN upload their own learning situation, novice report.

Today, when using keil to write C, I used a piece of such code

    ?Define uint unsigned int; void delay (uint MS) { uint a; while (MS --)// software delay module for (a = 0; a & lt; 114; a + +); }

    When compiling, a syntax error near ';' was given, and the following MS and a both reported an error that no identifier was defined.
    From the code above, it can be seen that there is an obvious error with the ";" sign after define, but I found that the line with syntax error near ';' error is void delay (uint MS).
    I was hoodwinked at that time, looking for a long time, I found that there was an extra "; to commemorate this stupid operation.

Two dimensional array and pointer to one dimensional array

First, the previous two-dimensional array:

int a[3][4]={1,2,3,4,5,6,7,8,9,1,2,3};

How to operate a pointer for a two-dimensional array like this:
the first concept is that a two-dimensional array has rows and columns.

    the first point is that the * operator accesses the column, and the second point is that the [] subscript operator also accesses the column.

For example, a, * a, a [2], * (a + 2)
A: A is the address of the first row, while * a is the address of the first row and zero column, which is the address of the first row and zero column.
A [2]: the subscript accesses the column of the row, that is, the address of two rows and 0 column.
* (a + 2): a + 2 accesses the address at the beginning of the second line, and a * sign accesses the address at column 0 of the second line.

Pointer to one-dimensional array: int (* P) [4];
when this pointer P points to a two-dimensional array, every P + 1 skips four elements and points directly to the next line.

C++ error: jump to case label crosses initialization

A compiler, the compiler error error: jump to case label [- fpermissive], the error: crosses initialization of 'XXXX' </ code>, to simple combing the related content

I. Problem code

int main()
{
  int test = 2;
  switch(test)
  {
    case 1:
      int i = 1;  
      cout << i;
      break;
    case 2:
      cout << i;  
      break;
    default:
      cout << "error" << endl;
  }
}

//test.cpp: In function 'int main()':
//test.cpp: error: jump to case label [-fpermissive]
//   case 2:
//        ^
//test.cpp: error:   crosses initialization of 'int i'
//    int b = 1;
//test.cpp: error: jump to case label [-fpermissive]
//   default:
//   ^
//test.cpp:11:8: error:   crosses initialization of 'int i'
//    int b = 1;

As can be seen from the above code, since there is no separate block in switch to qualify the declaration period of variable I, the scope of the variable is the initialization point to the end of switch. In this case, the compiler will report an error because we are not sure whether this variable will be used in other cases and whether it was initialized before it was used. For example, if test has a value of 2 and case 2 is executed directly, an undefined variable will cause an exception. This is a compiler error crosses initialization </ code>.
After inspection, it is found that the compiler will report an error
no matter whether the other branches contain defined variables or not, as long as the variables are not braced in the case.

int main()
{
  int test = 2;
  switch(test)
  {
    case 1:
      int i = 1; 
      cout << i;  
      break;
    case 2:
      cout << 3; 
      break;
    default:
      cout << "error" << endl;
  }
}

//test.cpp: In function 'int main()':
//test.cpp: error: jump to case label [-fpermissive]
//   case 2:
//        ^
//test.cpp: error:   crosses initialization of 'int i'
//    int i = 1;
//test.cpp: error: jump to case label [-fpermissive]
//   default:
//   ^
//test.cpp: error:   crosses initialization of 'int i'
//    int i = 1;

The code of case 1 is enclosed with {}, and the scope of variable I is clearly set to avoid access by other cases
2. The scope of variable I is put outside the switch, and every case in the switch can be accessed
The
switch statement is a kind of goto statement, so goto has the same properties. The following goto statement will not be executed, variable I will definitely be defined, but will report the same error as above. This means that there can be no variables between goto and the tag. Variables must appear before the goto or after the tag.

int main()
{
    if(0)
    {
        goto end;
    }

    int i = 1;

    end:
       cout << i;
}

//test.cpp: In function 'int main()':
//test.cpp error: jump to label 'end' [-fpermissive]
//   end:
//   ^
//test.cpp error:   from here [-fpermissive]
//          goto end;
//               ^
//test.cpp: error:   crosses initialization of 'int i'
//     int i = 1;

In the above example, it is possible to initialize a variable before the goto tag or after the end tag

c++ compilation error prompt [Error] name lookup of’i’ changed for ISO’for’ scoping

In VC 6, the scope of I is the function scope, and the variable I can be used outside of the for loop, that is:
for (int I = 0; i < n; + + I) {
//…
}
cout< < i< < endl;
can be passed by
and
for (int I = 0; i < n; + + I) {
//…
}
int i = 5;
compilation error.
In DEV C++, the scope of I is limited to the for loop, that is:
for (int I = 0; i < n; + + I) {
//…
}
int i = 5;
can be passed by
and
for (int I = 0; i < n; + + I) {
//…
}
cout< < i< < endl;
compilation error.
In VS.net, both pass, but if I is used outside of the for loop, warnings are given.

C error C2143 syntax error missing before ‘type’

1. Problem descriptionThe problem display is shown in the figure: All ides are VS2010.

Source code display: Source code is a Fahrenheit to Celsius temperature of the program.

#include <stdio.h>

//void Fahr_Celsius()
int main()
{
	//int fahr, celsius;
	int lower, upper, step;
	lower = 0;
	upper = 300;
	step = 20;
	printf("Hello world!\n");
	int fahr, celsius;
	fahr = lower;
	while (fahr<=upper)
	{
		celsius = 5 * (fahr - 32)/9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	}
	return 0;
}

2. Cause of the problem

In C, all variables must be declared before they are used. Declarations are usually placed at the beginning, before any executable statement. Declare the properties used to describe variables, which consist of a type name and a variable scale.

Here because the variable declaration statement on line 12 is placed after the executable statement.

3. Problem solving

Place all variable declarations before all executable statements. The modified code is as follows:

#include <stdio.h>

int main()
{
	int fahr, celsius;
	int lower, upper, step;
	lower = 0;
	upper = 300;
	step = 20;
	printf("Hello world!\n");
	//int fahr, celsius;
	fahr = lower;
	while (fahr<=upper)
	{
		celsius = 5 * (fahr - 32)/9;
		printf("%d\t%d\n", fahr, celsius);
		fahr = fahr + step;
	}
	
	return 0;

}

4. Problem summary
Although this problem is a stipulation of C language, it mainly appears in the compilation stage, which is interpreted by the compiler. This problem will not appear in VS2017, but will appear in VS2010.