How to use the Windows MessageBox() C function?

点点圈 提交于 2021-02-16 20:16:07

问题


Can someone tell me how I can display a message box in C that can print variables?

I mean like this:

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

To look like this:

          Variable

 Variable x is equal to 5.

Thanks in advance!


回答1:


MessageBox itself doen't support printf like formatting. You'll have to use snprintf for that:

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

MessageBox(0, buf, "Variable", 0);


来源:https://stackoverflow.com/questions/21327458/how-to-use-the-windows-messagebox-c-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!