这个C声明代码有什么问题?

时间:2012-05-23 07:42:19

标签: c windows console tui

#include <stdlib.h>
#include <Windows.h>
#include <Tchar.h>

HANDLE wHnd;    // Handle to write to the console.
HANDLE rHnd;    // Handle to read from the console.

int _tmain(int argc, _TCHAR* argv[]) {

    // Set up the handles for reading/writing:
    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
    rHnd = GetStdHandle(STD_INPUT_HANDLE);

    // Change the window title:
    SetConsoleTitle(TEXT("Win32 Console Control Demo"));

    // Set up the required window size:
    SMALL_RECT windowSize = {0, 0, 79, 49};

    // Change the console window size:
    SetConsoleWindowInfo(wHnd, TRUE, &windowSize);

}

报告了一些类似的错误:

'SMALL_RECT' : illegal use of this type as an expression  
missing ';' before identifier 'windowSize'

1 个答案:

答案 0 :(得分:3)

您正在使用仅支持现在古老的C90标准的MS C编译器。必须在函数体的顶部声明所有变量。

int _tmain(int argc, _TCHAR* argv[])
{    
    // Set up the required window size:
    SMALL_RECT windowSize = {0, 0, 79, 49};

    // Set up the handles for reading/writing:
    wHnd = GetStdHandle(STD_OUTPUT_HANDLE);
    rHnd = GetStdHandle(STD_INPUT_HANDLE);

    // Change the window title:
    SetConsoleTitle(TEXT("Win32 Console Control Demo"));

    // Change the console window size:
    SetConsoleWindowInfo(wHnd, TRUE, &windowSize);
}

痛苦,不是吗?!