预期的声明说明符或"......"之前'('令牌?

Kev*_*ong 7 c gcc

#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);
}
Run Code Online (Sandbox Code Playgroud)

TDM-GCC报道:

| line | Message
|  08  | error: expected declaration specifiers or '...' before '(' token
|  21  | error: expected declaration specifiers or '...' before '(' token
Run Code Online (Sandbox Code Playgroud)

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

Sha*_*our 7

看起来RGB是一个宏,如果你重命名错误将消失的功能.还Set_Color需要返回一个你定义的值来返回一个int但是你在功能结束时没有返回任何东西.

如果您尝试使用Set_Color没有显式返回值的值,那么根据C99草案标准部分函数定义12段,这将是未定义的行为:6.9.1

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

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