--- title: "Using exit or return | Microsoft Docs" ms.custom: "" ms.date: "11/04/2016" ms.reviewer: "" ms.suite: "" ms.technology: - "cpp-language" ms.tgt_pltfrm: "" ms.topic: "language-reference" f1_keywords: - "Exit" dev_langs: - "C++" helpviewer_keywords: - "exit function" - "return keyword [C++], using for program termination" ms.assetid: b5136c5c-2505-4229-8691-2a1d6a98760b caps.latest.revision: 8 author: "mikeblome" ms.author: "mblome" manager: "ghogen" translation.priority.ht: - "cs-cz" - "de-de" - "es-es" - "fr-fr" - "it-it" - "ja-jp" - "ko-kr" - "pl-pl" - "pt-br" - "ru-ru" - "tr-tr" - "zh-cn" - "zh-tw" --- # Using exit or return When you call **exit** or execute a `return` statement from **main**, static objects are destroyed in the reverse order of their initialization. The following example shows how such initialization and cleanup works. ## Example ``` // using_exit_or_return1.cpp #include class ShowData { public: // Constructor opens a file. ShowData( const char *szDev ) { errno_t err; err = fopen_s(&OutputDev, szDev, "w" ); } // Destructor closes the file. ~ShowData() { fclose( OutputDev ); } // Disp function shows a string on the output device. void Disp( char *szData ) { fputs( szData, OutputDev ); } private: FILE *OutputDev; }; // Define a static object of type ShowData. The output device // selected is "CON" -- the standard output device. ShowData sd1 = "CON"; // Define another static object of type ShowData. The output // is directed to a file called "HELLO.DAT" ShowData sd2 = "hello.dat"; int main() { sd1.Disp( "hello to default device\n" ); sd2.Disp( "hello to file hello.dat\n" ); } ``` In the preceding example, the static objects `sd1` and `sd2` are created and initialized before entry to `main`. After this program terminates using the `return` statement, first `sd2` is destroyed and then `sd1`. The destructor for the `ShowData` class closes the files associated with these static objects. Another way to write this code is to declare the `ShowData` objects with block scope, allowing them to be destroyed when they go out of scope: ``` int main() { ShowData sd1, sd2( "hello.dat" ); sd1.Disp( "hello to default device\n" ); sd2.Disp( "hello to file hello.dat\n" ); } ``` ## See Also [Additional Termination Considerations](../cpp/additional-termination-considerations.md)