There is a bad message. The printf function cannot be used in windows programs.
The good news is that you can still use sptrinf and other functions of the series to display text.
The following describes the definitions of printf and sprintf.
[Cpp]
Int printf (const char * szFormat ,...);
Int sprintf (char * szBuffer, const char * szFormat ,...);
Int printf (const char * szFormat ,...);
Int sprintf (char * szBuffer, const char * szFormat,...); In the console, You can output:
[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Printf ("the sum of % I and % I is % I \ n", 5, 3, 5 + 3 );
System ("pause ");
Return 0;
}
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Printf ("the sum of % I and % I is % I \ n", 5, 3, 5 + 3 );
System ("pause ");
Return 0;
}
Sprintf is used below, which is equivalent to the above:
[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Char buffer [100];
Sprintf (buffer, "the sum of % I and % I is % I", 5, 3, 5 + 3 );
Puts (buffer );
System ("pause ");
Return 0;
}
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Char buffer [100];
Sprintf (buffer, "the sum of % I and % I is % I", 5, 3, 5 + 3 );
Puts (buffer );
System ("pause ");
Return 0;
}
Unicode:
If UNICODE is not defined, then:
Typedef char TCHAR
If UNICODE is defined:
Typedef wchar_t TCHAR
The biggest difference between wchar_t and char is that one occupies two bytes and the other occupies one byte:
[Cpp]
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Wchar_t p [] = L "hello ";
Cout <wcslen (p) <endl;
Cout <sizeof (p) <endl; // OUTPUT 12
System ("pause ");
Return 0;
}
# Include <iostream>
# Include <string>
Using namespace std;
Int main (){
Wchar_t p [] = L "hello ";
Cout <wcslen (p) <endl;
Cout <sizeof (p) <endl; // OUTPUT 12
System ("pause ");
Return 0;
}
The following is an example of Output Using MessageBoxPrintf: (GetSystemMetric is very important)
[Cpp]
# Include <windows. h>
# Include <tchar. h>
# Include <stdio. h>
Int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat ,...){
TCHAR szBuffer [1024];
Va_list pArgList;
Va_start (pArgList, szFormat );
_ Vsntprintf (szBuffer, sizeof (szBuffer)/sizeof (TCHAR), szFormat, pArgList );
Va_end (pArgList );
Return MessageBox (NULL, szBuffer, szCaption, 0 );
}
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR sc1_line, int iCmdShow ){
Int cxScreen, cyScreen;
CxScreen = GetSystemMetrics (SM_CXSCREEN );
CyScreen = GetSystemMetrics (SM_CYSCREEN );
MessageBoxPrintf (TEXT ("ScrnSize"), TEXT ("The screen is % I pixels wide by % I pixels high"), cxScreen, cyScreen );
Return 0;
}
# Include <windows. h>
# Include <tchar. h>
# Include <stdio. h>
Int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat ,...){
TCHAR szBuffer [1024];
Va_list pArgList;
Va_start (pArgList, szFormat );
_ Vsntprintf (szBuffer, sizeof (szBuffer)/sizeof (TCHAR), szFormat, pArgList );
Va_end (pArgList );
Return MessageBox (NULL, szBuffer, szCaption, 0 );
}
Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR sc1_line, int iCmdShow ){
Int cxScreen, cyScreen;
CxScreen = GetSystemMetrics (SM_CXSCREEN );
CyScreen = GetSystemMetrics (SM_CYSCREEN );
MessageBoxPrintf (TEXT ("ScrnSize"), TEXT ("The screen is % I pixels wide by % I pixels high"), cxScreen, cyScreen );
Return 0;
}
The result is as follows:
Author: zh634455283