C语言错误:expected declaration or statement at end of input、编写函数求100万以内的素数

C语言错误:expected declaration or statement at end of input (中文:输入结尾应为声明或语句)
可能错误:

  1. 某一个函数或者变量没有在使用之前声明。
  2. 某个地方少了个括号。(这种情况,编译器一般会在最后一行代码报错,但错误很可能不在最后一行,要靠自己去找出来,比如下面我那个程序就是提示最后一行,不提示我的自定义函数,我也就没去看自定义函数,后来看了看自定义函数,找出来了)
  3. 所提示句子中有些函数的头文件没有加上(比如你用了math.h函数库里的函数,比如pow、sqrt这种)
  4. 自己定义的函数名和库文件中的函数名冲突。(这个也有可能)

仔细找找错误,不妨去干点别的然后再回来看看, 反应过来就能够发现错误了
我做这道题目也是一直认为自己没错
后来,缓了一会就OK了
很多时候都是自己走不出来死胡同
没有看到自定义函数的问题
因为编译器报错一直都在最后一行,希望大家也注意啊
在这里插入图片描述

后来反应过来了
自己定义函数出了问题
#include<stdio.h>
int cc(int m)
{int i;
for(i=2;i<m;i++)
{if(m%i==0)
return(0);
else
return(1);}} 

int main()
{
int n;
for(n=2;n<=1000;n++)
{
	if(cc(n)) 
		printf("%d,",n);
}
printf("\n");
return 0;
}
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2025 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "adc.h" #include "i2c.h" #include "tim.h" #include "usart.h" #include "gpio.h" #include"oled.h" #include <string.h> /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include<stdio.h> /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_ADC1_Init(); MX_TIM3_Init(); MX_USART2_UART_Init(); MX_I2C2_Init(); MX_I2C1_Init(); /* USER CODE BEGIN 2 */ HAL_Delay(20); OLED_Init(); int value =0; float voltage =0.0; int state = 0; char message[20]; char MASSAGE[20]; HAL_ADCEx_Calibration_Start(&hadc1); HAL_ADC_Start(&hadc1); HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { while (state==0){ HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12); HAL_Delay(200); if( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET){ state = state+1; while(state>1){state=0;}} HAL_GPIO_WritePin(YELLOW_GPIO_Port,YELLOW_Pin , GPIO_PIN_SET); value = HAL_ADC_GetValue(&hadc1); voltage = (value / 4095.0) * 3.3; HAL_Delay(10); float i = (value / 4095.0 )*99; __HAL_TIM_SET_COMPARE(&htim3, RED_Pin , i); HAL_Delay(20); OLED_NewFrame(); sprintf(message,"%.1f",voltage); OLED_PrintString(0, 0, message, &font16x16, OLED_COLOR_NORMAL); sprintf(MASSAGE,"%.1f",i); OLED_PrintString(0, 18, MASSAGE, &font16x16, OLED_COLOR_NORMAL); } while (state==1){ HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12); HAL_Delay(200); if( HAL_GPIO_ReadPin(GPIOB, GPIO_PIN_12) == GPIO_PIN_RESET){ state = state+1; while(state>1){state=0;} if (voltage > 0 && voltage < 1){ HAL_GPIO_WritePin(RED_GPIO_Port, RED_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(GREEN_GPIO_Port, GREEN_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(YELLOW_GPIO_Port,YELLOW_Pin , GPIO_PIN_RESET); HAL_GPIO_WritePin(BLUE_GPIO_Port,BLUE_Pin , GPIO_PIN_RESET); OLED_NewFrame(); OLED_PrintString(0, 0, "RED", &font16x16, OLED_COLOR_NORMAL); OLED_ShowFrame(); } else if (voltage > 1 && voltage < 2 ){ HAL_GPIO_WritePin(BLUE_GPIO_Port, BLUE_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(GREEN_GPIO_Port, GREEN_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(YELLOW_GPIO_Port,YELLOW_Pin , GPIO_PIN_RESET); HAL_GPIO_WritePin(RED_GPIO_Port,RED_Pin , GPIO_PIN_RESET); OLED_NewFrame(); OLED_PrintString(0, 0, "BLUE", &font16x16, OLED_COLOR_NORMAL); OLED_ShowFrame(); } else { HAL_GPIO_WritePin(YELLOW_GPIO_Port, YELLOW_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(GREEN_GPIO_Port, GREEN_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(BLUE_GPIO_Port,BLUE_Pin , GPIO_PIN_RESET); HAL_GPIO_WritePin(RED_GPIO_Port,RED_Pin , GPIO_PIN_RESET); OLED_NewFrame(); OLED_PrintString(0, 0, "YELLOW", &font16x16, OLED_COLOR_NORMAL); OLED_ShowFrame(); } } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC; PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6; if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */
最新发布
10-13
评论 5
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值