logo

C'deki çıkış () işlevi

çıkış() işlevi Programda hemen çağrılan bir işlemi veya işlevi sonlandırmak için kullanılır. Bu, programda çıkış() işlevi oluştuğunda, sürece ait herhangi bir açık dosyanın veya işlevin hemen kapatıldığı anlamına gelir. Exit() işlevi, C'nin standart kütüphane işlevidir ve aşağıdaki şekilde tanımlanır: stdlib.h başlık dosyası. Yani mevcut programı zorla sonlandıran ve programdan çıkmak için kontrolü işletim sistemine aktaran fonksiyon diyebiliriz. Çıkış(0) işlevi, programın herhangi bir hata mesajı olmadan sonlandırıldığını belirler ve ardından çıkış(1) işlevi, programın yürütme sürecini zorla sonlandırdığını belirler.

C'deki çıkış () işlevi

Exit() fonksiyonunun önemli noktaları

C programlamada çıkış fonksiyonunun ana noktaları aşağıdaki gibidir:

  1. Exit() fonksiyonunu kullanırken stdlib.h başlık dosyasını dahil etmemiz gerekmektedir.
  2. Çıkış () işleviyle karşılaşıldığında programın normal çalışmasını sonlandırmak için kullanılır.
  3. Çıkış () işlevi, kayıtlı atexit() işlevini kayıtlarının tersi sırayla çağırır.
  4. Yazılmamış ara belleğe alınmış verilerle okuma veya yazma gibi tüm açık akış verilerini temizlemek veya temizlemek için çıkış() işlevini kullanabiliriz.
  5. Bir ana öğeye veya başka bir işleve veya dosyaya bağlı tüm açık dosyaları kapattı ve tmpfile işlevi tarafından oluşturulan tüm dosyaları kaldırabilir.
  6. Kullanıcı çıkış işlevini birden fazla kez çağırırsa veya çıkış ve quick_exit işlevini çağırırsa programın davranışı tanımsızdır.
  7. Çıkış işlevi iki bölüme ayrılmıştır: çıkış(0) ve çıkış(1).

çıkış() işlevinin sözdizimi

 void exit ( int status); 

çıkış() işlevin dönüş türü yoktur.

dize kadar yüzer

int durumu: Ana işleme döndürülen çıkış fonksiyonunun durum değerini temsil eder.

Örnek 1: for döngüsünde çıkış() işlevini kullanan program

C programlama dilinde işlemin normal sonlandırılması için çıkış (0) fonksiyonunu gösterecek bir program oluşturalım.

c'deki dizi dizisi
 #include #include int main () { // declaration of the variables int i, num; printf ( &apos; Enter the last number: &apos;); scanf ( &apos; %d&apos;, &amp;num); for ( i = 1; i<num; 0 6 i++) { use if statement to check the condition ( i="=" ) * exit () with passing argument show termination of program without any error message. exit(0); else printf (' 
 number is %d', i); } return 0; < pre> <p> <strong>Output</strong> </p> <pre> Enter the last number: 10 Number is 1 Number is 2 Number is 3 Number is 4 Number is 5 </pre> <h3>There are two types of exit status in C</h3> <p>Following are the types of the exit function in the C programming language, as follows:</p> <ol class="points"> <li>EXIT_ SUCCESS</li> <li>EXIT_FAILURE</li> </ol> <p> <strong>EXIT_SUCCESS</strong> : The EXIT_ SUCCESS is the exit() function type, which is represented by the exit(0) statement. Where the &apos;0&apos; represents the successful termination of the program without any error, or programming failure occurs during the program&apos;s execution.</p> <p> <strong>Syntax of the EXIT SUCCESS</strong> </p> <pre> exit (EXIT_SUCCESS); </pre> <p> <strong>Example 1: Program to demonstrate the usage of the EXIT_SUCCESS or exit(0) function</strong> </p> <p>Let&apos;s create a simple program to demonstrate the working of the exit(0) function in C programming.</p> <pre> #include #include int main () { printf ( &apos; Start the execution of the program. 
&apos;); printf (&apos; Exit from the program. 
 &apos;); // use exit (0) function to successfully execute the program exit (0); printf ( &apos;Terminate the execution of the program.
 &apos;); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Start the execution of the program. Exit from the program. </pre> <p> <strong>Example 2: Program to use the EXIT_SUCCESS macro in the exit() function</strong> </p> <p>Let&apos;s create a C program to validate the whether the character is presents or not.</p> <pre> #include #include int main () { // declaration of the character type variable char ch; printf(&apos; Enter the character: &apos;); scanf (&apos; %c&apos;, &amp;ch); // use if statement to check the condition if ( ch == &apos;Y&apos;) { printf(&apos; Great, you did it. &apos;); exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program } else { printf (&apos; You entered wrong character!! &apos;); } return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Enter the character: Y Great, you did it. </pre> <p> <strong>EXIT_FAILURE</strong> : The EXIT_FAILURE is the macro of the exit() function to abnormally execute and terminate the program. The EXIT_FAILURE is also represented as the exit(1) function. Whether the &apos;1&apos; represents the abnormally terminates the program and transfer the control to the operating system.</p> <p> <strong>Syntax of the EXIT_FAILURE</strong> </p> <pre> exit (EXIT_FAILURE); </pre> <p> <strong>Example 1: Let&apos;s create a program to use the EXIT_FAILURE or exit(1) function</strong> </p> <pre> #include #include int main () { int num1, num2; printf (&apos; Enter the num1: &apos;); scanf (&apos;%d&apos;, &amp;num1); printf (&apos; 
 Enter the num2: &apos;); scanf (&apos;%d&apos;, &amp;num2); if (num2 == 0) { printf (&apos; 
 Dividend cannot be zero. &apos;); // use EXIT_FAILURE exit(1); } float num3 = (float)num1 / (float)num2; printf (&apos; %d / %d : %f&apos;, num1, num2, num3); // use the EXIT_SUCCESS exit(0); } </pre> <p> <strong>Output</strong> </p> <pre> Enter the num1: 20 Enter the num2: 6 20 / 6 : 3.333333 2nd Run Enter the num1: 20 Enter the num2: 6 Dividend cannot be zero </pre> <p> <strong>Example 2: Let&apos;s create another program to use the EXIT_FAILURE to terminate the C program.</strong> </p> <pre> #include #include int main () { // declare the data type of a file FILE *fptr = fopen ( &apos;javatpoint.txt&apos;, &apos;r&apos; ); // use if statement to check whether the fptr is null or not. if ( fptr == NULL) { fprintf ( stderr, &apos;Unable to open the defined file 
&apos; ); exit ( EXIT_FAILURE); // use exit function to check termination } // use fclose function to close the file pointer fclose (fptr); printf ( &apos; Normal termination of the program. &apos;); return 0; } </pre> <p> <strong>Output</strong> </p> <pre> Unable to open the defined file. </pre> <hr></num;>

C'de iki tür çıkış durumu vardır

C programlama dilinde çıkış fonksiyonunun türleri aşağıdaki gibidir:

  1. ÇIKIŞ_ BAŞARI
  2. EXIT_FAILURE

EXIT_SUCCESS : EXIT_SUCCESS, çıkış(0) ifadesiyle temsil edilen çıkış() işlev türüdür. Burada '0', programın herhangi bir hata olmadan başarıyla sonlandırıldığını veya programın yürütülmesi sırasında programlama hatasının meydana geldiğini temsil eder.

ÇIKIŞ BAŞARISININ sözdizimi

 exit (EXIT_SUCCESS); 

Örnek 1: EXIT_SUCCESS veya çıkış(0) işlevinin kullanımını gösteren program

C programlamada çıkış(0) fonksiyonunun çalışmasını göstermek için basit bir program oluşturalım.

 #include #include int main () { printf ( &apos; Start the execution of the program. 
&apos;); printf (&apos; Exit from the program. 
 &apos;); // use exit (0) function to successfully execute the program exit (0); printf ( &apos;Terminate the execution of the program.
 &apos;); return 0; } 

Çıktı

 Start the execution of the program. Exit from the program. 

Örnek 2: EXIT_SUCCESS makrosunu çıkış() işlevinde kullanacak program

en güzel gülümseme

Karakterin mevcut olup olmadığını doğrulamak için bir C programı oluşturalım.

 #include #include int main () { // declaration of the character type variable char ch; printf(&apos; Enter the character: &apos;); scanf (&apos; %c&apos;, &amp;ch); // use if statement to check the condition if ( ch == &apos;Y&apos;) { printf(&apos; Great, you did it. &apos;); exit(EXIT_SUCCESS); // use exit() function to terminate the execution of a program } else { printf (&apos; You entered wrong character!! &apos;); } return 0; } 

Çıktı

 Enter the character: Y Great, you did it. 

EXIT_FAILURE : EXIT_FAILURE, programı anormal şekilde yürütmek ve sonlandırmak için çıkış() işlevinin makrosudur. EXIT_FAILURE aynı zamanda çıkış(1) işlevi olarak da temsil edilir. '1'in, programı anormal şekilde sonlandırıp kontrolü işletim sistemine aktardığını temsil edip etmediği.

EXIT_FAILURE'un sözdizimi

sistem yazılımı
 exit (EXIT_FAILURE); 

Örnek 1: EXIT_FAILURE veya çıkış(1) fonksiyonunu kullanacak bir program oluşturalım.

 #include #include int main () { int num1, num2; printf (&apos; Enter the num1: &apos;); scanf (&apos;%d&apos;, &amp;num1); printf (&apos; 
 Enter the num2: &apos;); scanf (&apos;%d&apos;, &amp;num2); if (num2 == 0) { printf (&apos; 
 Dividend cannot be zero. &apos;); // use EXIT_FAILURE exit(1); } float num3 = (float)num1 / (float)num2; printf (&apos; %d / %d : %f&apos;, num1, num2, num3); // use the EXIT_SUCCESS exit(0); } 

Çıktı

 Enter the num1: 20 Enter the num2: 6 20 / 6 : 3.333333 2nd Run Enter the num1: 20 Enter the num2: 6 Dividend cannot be zero 

Örnek 2: C programını sonlandırmak için EXIT_FAILURE'ı kullanacak başka bir program oluşturalım.

 #include #include int main () { // declare the data type of a file FILE *fptr = fopen ( &apos;javatpoint.txt&apos;, &apos;r&apos; ); // use if statement to check whether the fptr is null or not. if ( fptr == NULL) { fprintf ( stderr, &apos;Unable to open the defined file 
&apos; ); exit ( EXIT_FAILURE); // use exit function to check termination } // use fclose function to close the file pointer fclose (fptr); printf ( &apos; Normal termination of the program. &apos;); return 0; } 

Çıktı

 Unable to open the defined file.