如何使用Windows MessageBox()C函数?

问题描述 投票:1回答:1

谁能告诉我如何在C语言中显示一个可以打印变量的消息框?

我是说像这样。

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

main()
{
    int x = 5;
    MessageBox(0, "Variable x is equal to %d", "Variable", 0); 
    /* Where do I specify the variable so that 5 will display?*/

    getch();
}

就像这样

          Variable

 Variable x is equal to 5.

先谢谢你!

c windows messagebox
1个回答
5
投票

MessageBox 本身不支持 printf 像格式化。你必须使用 snprintf 为此。

char buf[1024];
snprintf(buf, 1024, "Variable x is equal to %d", x);

MessageBox(0, buf, "Variable", 0);
© www.soinside.com 2019 - 2024. All rights reserved.