C语言 '(' 标记之前的预期声明说明符或 '...'?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20860201/
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:34:10  来源:igfitidea点击:

Expected declaration specifiers or '...' before '(' token?

cgcc

提问by Kevin Dong

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

/// Global Variables
HANDLE ConsoleHandle;

int RGB (int R, int G, int B); // line 8
int Set_Color (int RGB_Fore, int RGB_Back);

int main (void)
{
  // Get Handle
  ConsoleHandle = GetStdHandle(STD_OUTPUT_HANDLE); 
  char Str [32] = "Happy New Year.\n";
  printf("%s", Str);
  system("pause>nul");
  return 0;
}

int RGB (int R, int G, int B) // line 21
{
  return (R*4 + G*2 + B);
}

int Set_Color (int RGB_Fore, int RGB_Back)
{
  SetConsoleTextAttribute(ConsoleHandle, RGB_Fore*8 + RGB_Back);
}

The TDM-GCC reported:

TDM-GCC 报告说:

| line | Message
|  08  | error: expected declaration specifiers or '...' before '(' token
|  21  | error: expected declaration specifiers or '...' before '(' token

Why? How to solve this problem? Thanks

为什么?如何解决这个问题呢?谢谢

采纳答案by Shafik Yaghmour

Looks like RGBis a macro, if you rename the function that error will go away. Also Set_Colorneeds to return a value you have it defined to return an intbut you fall of the end of the function without returning anything.

看起来RGB是一个宏,如果你重命名这个错误就会消失的函数。还Set_Color需要返回一个你定义为返回的值,int但是你在函数的末尾没有返回任何东西。

If you attempted to use the value of Set_Colorwithout an explicit return that would be undefined behavioras per the C99 draft standard section 6.9.1Function definitionsparagraph 12:

如果您尝试在Set_Color没有显式返回的情况下使用 的值,则根据 C99 草案标准部分功能定义12段将是未定义行为6.9.1

If the } that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined.

如果到达终止函数的 },并且调用者使用了函数调用的值,则行为未定义。

and this is undefinedin C++regardless of whether you attempt to use the return value or not.

无论您是否尝试使用返回值,这在C++ 中都是未定义的