Help about CDECL

This is a discussion on Help about CDECL within the Windows Programming forums, part of the Platform Specific Boards category; From Programming Windows 5e Code: #include <windows.h> #include <tchar.h> #include <stdio.h> int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...

  1. #1
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    212

    Help about CDECL

    From Programming Windows 5e
    Code:
    #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 szCmdLine, 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 ;
    }
    What does this CDECL mean? I found that even without CDECL, this program still compiled and run successfully. Why?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Its an instruction to the compiler on how params are removed from the stack after the function is called.

    Most Windows APIs require STDCALL, but C compilers use CDECL by default. The function you are using uses CDECL as it doesnt know the numbers of params passed to it at compile time, so it uses CDECL and relies on the calling code to balance the stack.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling convention stdcalll and cdecl call
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 07-17-2008, 04:26 AM
  2. cdecl program for VC++?
    By zopiro in forum C++ Programming
    Replies: 1
    Last Post: 05-31-2007, 01:43 AM
  3. stdcall or cdecl
    By studentc in forum C Programming
    Replies: 4
    Last Post: 02-02-2006, 04:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21