Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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);
}

The TDM-GCC reported:

| 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

like image 947
Kevin Dong Avatar asked Dec 31 '13 16:12

Kevin Dong


1 Answers

Looks like RGB is a macro, if you rename the function that error will go away. Also Set_Color needs to return a value you have it defined to return an int but you fall of the end of the function without returning anything.

If you attempted to use the value of Set_Color without an explicit return that would be undefined behavior as per the C99 draft standard section 6.9.1 Function definitions paragraph 12:

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 undefined in C++ regardless of whether you attempt to use the return value or not.

like image 181
Shafik Yaghmour Avatar answered Oct 04 '22 08:10

Shafik Yaghmour