C语言 错误:字符串常量前的预期声明说明符或“...”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36606697/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 10:23:30  来源:igfitidea点击:

error: expected declaration specifiers or '…' before string constant

c

提问by Wouter

Does anybody know what is wrong with this piece of code? i can't see to find the issue among the comparable questions.

有人知道这段代码有什么问题吗?我看不到在可比问题中找到问题。

The code is written in C, and i keep getting this error. I do add -D SET_MIN_TEMP=5 -D Set_MAX_TEMP=30to the gcc compile line to make sure the ifndefs should be false...

代码是用 C 编写的,我一直收到这个错误。我确实添加-D SET_MIN_TEMP=5 -D Set_MAX_TEMP=30到 gcc 编译行以确保 ifndefs 应该是假的...

#ifndef CONFIG_H
#define CONFIG_H


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdint.h>

#ifndef RUN_AVG_LENGTH
    #define RUN_AVG_LENGTH 5
#endif

#ifndef SET_MIN_TEMP
    printf("please set SET_MIN_TEMP \n");
#endif

#ifndef SET_MAX_TEMP
    printf("please set SET_MAX_TEMP \n");
#endif

typedef uint16_t sensor_id_t;
typedef uint16_t room_id_t;
typedef double sensor_value_t;
typedef time_t sensor_ts_t;     // UTC timestamp as returned by time() - notice that the size of time_t is different on 32/64 bit machine

typedef struct {
  sensor_id_t id;
  sensor_value_t value;
  sensor_ts_t ts;
} sensor_data_t;

typedef struct {
    sensor_id_t sensor_id;
    room_id_t room_id;
    double running_avg[5];
    sensor_ts_t timestamp;
} sensor_node_t;


#endif // CONFIG_H

回答by mame98

You can not use a function call (printf) outside a function. You should take a look at #errorif you want to report errors at compilation...

不能在函数printf外使用函数调用 ( )。#error如果你想在编译时报告错误,你应该看看......

See here

这里