This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

declaration may not appear after executable statement in block

Hello, I was tryning to use MDK-ARM 5 I find some difficulties in its configuration.
This code is basically used to turn on the led that is connected to the pin 12 of the port D on the STM32F407VG. The problem is when I try to bild it, on error is appeared that says :

main.c(20): error: #268: declaration may not appear after executable statement in block GPIO_InitTypeDef GPIO_InitStructure;

#include "stm32f4xx.h"                  // Device header
#include "stm32f4xx_rcc.h"              // Keil::Device:StdPeriph Drivers:RCC
#include "stm32f4xx_gpio.h"             // Keil::Device:StdPeriph Drivers:GPIO

void GPIO(void);

int main(){

        GPIO();

        while(1){
                GPIO_SetBits(GPIOD, GPIO_Pin_12);
        }
}


void GPIO(){
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

        GPIO_InitTypeDef GPIO_InitStructure;

        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
        GPIO_Init(GPIOD, &GPIO_InitStructure);
}


Can you tell me guys what could be the problem with that. Thank you.

  • You can move the declaration “GPIO_InitTypeDef GPIO_InitStructure;” to the top of GPIO() function or enable “C99 Mode” in C/C++ options.

  • It's now working.Thank you. It turns out that the keil has a a very hard configuration. Each time I solve a problem a new one comes out.

  • Those you seem to have encountered so far appear to be from sloppy coding style. Figure how much fun you'd have if your employer made you run your code through Lint or MISRA before you could commit it to the project?

  • It's important to remember that the C language standard itself has a large framework of rules about what is allowed and what isn't. So even if Keil is an ARM company and you use an ARM-written compiler, the outcome of compiling source code will still to a huge part depend on the C/C++ language standards. Which is the big reason why we have standards - to make sure people get expected results for code that fulfills the standards and doesn't uses depend on any of the vendor-specific features mentioned in the standard or any vendor-specific language extension.

    Prio 1 when learning C is to find some good books about the language. But a follow-up step is to spend the time and read the actual language standard. A basic knowledge of C or some similar programming language is needed for it to be meaningful to read the C/C++ standards. But the standards contains important notes that are not available in any books intended to teach the languages. The language standards are the contract that promises what the compiler will do with different source code constructs.