What is pragma in C? Or how will you execute functions before and after main function in C program?

C Programming Language >   C Pre-processor >   Introduction about Pre-processor  

Short Question

655


Answer:

#pragma is a pre-processor macro in C. It is used to call a function before and after main function in a C program.


#include <stdio.h>

void function1( );
void function2( );

#pragma startup function1
#pragma exit function2

int main( )
{
 </p>
                                          
                                        <hr>
                                        
                              <div style="text-align:center">
                              <a href="#list" class="btn btn-info btn-sm main-gradient-tutorials">Go to Question List</a>
                              </div>
                              
                              <div class = "shadow p-3 mt-2" style = "border-radius: 10px;">
                               <div class='col m-3'><p> <i class='fa fa-info-circle'></i> <small>This Particular section is dedicated to <b>Question & Answer</b> only. If you want learn more about <b>C Programming Language</b>. Then you can visit below links to get more depth on this subject.  </small></p></div><div class='col m-3'><div class="row">
     <div class="col bg-light m-2 shadow btn btn-success"> <a href="https://www.atnyla.com/syllabus/c-programming-language/1"> C Syllabus </a> </div>
     <div class="col bg-light m-2 shadow btn btn-success">   <a href="https://www.atnyla.com/tutorial/about-c-tutorial/1/152">C Tutorial </a> </div>  
     <div class="col bg-light m-2 shadow btn btn-success"> <a href="https://www.atnyla.com/fundamentals-of-c-language/1/20">C Chapterwise Programs </a>  </div> 
     <div class="col bg-light m-2 shadow btn btn-success"> <a href="https://www.atnyla.com/general-knowledge/88/158">C Chapterwise MCQs </a> </div>
     <div class="col bg-light m-2 shadow btn btn-success"> <a href="https://www.atnyla.com/qa/fundamentals-of-c-language/1/20">C Chapterwise Q & A </a> </div>

    <div class="col bg-light m-2 shadow btn btn-success"> <a href="https://www.youtube.com/watch?v=I_DwOci2mtE&list=PL2Bn5-JgUDvfFxrUDJg0E9FqoxLjth5fo" rel="nofollow">C Free Videos </a> </div>
</div>
<hr></div><hr style='height:3px;border-width:0;color:gray;background-color:gray'>                              </div>
                                 
                              <br>
                             <div class="shadow p-3 mb-5 bg-body rounded">
                               <a href="/tutorial/about-c-tutorial/1/152" class="btn btn-sm btn-default"># C Tutorials</a> 
                               <a href="/tutorial/java/0/0" class="btn btn-sm btn-default"># JAVA Tutorials</a>
                               <a href="/tutorial/about-html5-tutorial/2/233" class="btn btn-sm btn-default"># HTML Tutorials</a>
                           
                              <a href="https://www.atnyla.com/tutorial/about-tutorial/6/395" class="btn btn-sm btn-default"># Computer Fundamental</a> 
                               <a href="https://www.atnyla.com/tutorial/about-dsa/3/295" class="btn btn-sm btn-default"># Data Structure</a>
                               <a href="https://www.atnyla.com/tutorial/about-dbms-tutorial/13/713" class="btn btn-sm btn-default"># DBMS Tutorials</a>
                           
                              <a href="https://www.atnyla.com/tutorial/about-sql-tutorial/5/355" class="btn btn-sm btn-default">SQL</a> 
                               <a href="https://www.atnyla.com/tutorial/about-c-tutorial/8/468" class="btn btn-sm btn-default"># C# Language</a>
                               <a href="https://www.atnyla.com/tutorial/about-r-tutorial/7/409" class="btn btn-sm btn-default"># R Language</a>
                            
                              <a href="https://www.atnyla.com/tutorial/about-php-tutorial/12/604" class="btn btn-sm btn-default"># PHP</a> 
                               <a href="https://www.atnyla.com/tutorial/about-python-tutorial/11/597" class="btn btn-sm btn-default"># Python</a>
                               <a href="https://www.atnyla.com/tutorial/about-vue-js-tutorial/14/765" class="btn btn-sm btn-default"># Vue JS</a>
                             </div>
                             
                             <hr>
                             <h3>Join Our telegram group to ask Questions </h3>
                             <p> Click below button to join our groups. </p>
                             <div class="row">
                                 <div class="col m-2 bg-light shadow btn btn-success">
                                   <a href="https://t.me/atnyla"  rel="nofollow" target="_blank"> Ask Question - CSE </a>
                                 </div>  
                                  <div class="col m-2 bg-light shadow btn btn-success">
                                    <a href="https://t.me/ComputerLanguageWithRumman"  rel="nofollow" target="_blank"> Programming Language - PDF</a>
                                </div> 
                            </div>
                                        
                              <br>
                               
                            </div> 
                        </div>
                    </div>
                   <div class="col-lg-5">
                
                            <ul class="list-group" >
                                <li id="list" class="list-group-item d-flex justify-content-between align-items-center main-gradient-tutorials mb-2"><strong>Related Program List</strong></li>
                                         <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/how-will-you-override-an-existing-macro-in-c-question/36/665">
                  <h5 class="mb-1" style="color:#194757;">665. &nbsp;How will you override an existing macro in C?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <p>To override an existing macro, we need to undefine existing macro using &ldquo;#undef&rdquo;. Then, we need to define same macro again with new value.</p>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/introduction-about-pre-processor/1/376">
                    <small># Tutorial: Introduction about Pre-processor</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/1/36">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: C Pre-processor</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/how-to-check-whether-macro-is-defined-or-not-in-a-c-program-question/36/666">
                  <h5 class="mb-1" style="color:#194757;">666. &nbsp;How to check whether macro is defined or not in a C program?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <p>#ifdef pre-processor directive checks whether particular macro is defined or not in a C program.</p>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/introduction-about-pre-processor/1/376">
                    <small># Tutorial: Introduction about Pre-processor</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/1/36">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: C Pre-processor</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/what-is-the-difference-between-memcpy-and-strcpy-functions-in-c-question/52/667">
                  <h5 class="mb-1" style="color:#194757;">667. &nbsp;What is the difference between memcpy() and strcpy() functions in C?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <ul>
<li>memcpy() function is used to copy a specified number of bytes from one memory to another. Whereas, strcpy() function is used to copy the contents of one string into another string.</li>
<li>memcpy() function acts on memory rather than value. Whereas, strcpy() function acts on value rather than memory.</li>
</ul>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/all-string-functions/1/261">
                    <small># Tutorial: All String Functions</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/1/52">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: String  functions in C </small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/what-is-the-difference-between-memcpy-and-memmove-functions-in-c-question/29/668">
                  <h5 class="mb-1" style="color:#194757;">668. &nbsp;What is the difference between memcpy() and memmove() functions in C?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <ul>
<li>memcpy()&nbsp; function is is used to copy a specified number of bytes from one memory to another.</li>
<li>memmove() function is used to copy a specified number of bytes from one memory to another or to overlap on same memory.</li>
<li>Difference between memmove() and memcpy() is, overlap can happen on memmove(). Whereas, memory overlap won&rsquo;t happen in memcpy() and it should be done in non-destructive way.</li>
</ul>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/introduction-to-string/1/216">
                    <small># Tutorial: Introduction to String</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/1/29">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: String in C Language</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/is-there-any-inbuilt-library-function-in-c-to-remove-leading-and-trailing-spaces-from-a-string-how-will-you-remove-them-in-c-question/30/669">
                  <h5 class="mb-1" style="color:#194757;">669. &nbsp;Is there any inbuilt library function in C to remove leading and trailing spaces from a string? How will you remove them in C?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer: <ul>
<li>There is no inbuilt function to remove leading and trailing spaces from a string in C. We need to write our own function to remove them.</li>
<li>We need to check first non-space character in given string. Then, we can copy that string from where non space character is found. Then, we can check whether any spaces are available in copied string from the end of the string. If space is found, we can copy &lsquo;\0&rsquo; in that space until any character is found. Because, &lsquo;\0&rsquo;indicates the end of the string. Now, we have removed leading and trailing spaces in a given string.</li>
<li></li>
</ul>                  </div>
                </div>
                        <div class="d-flex w-100 justify-content-between">
                <a style="color:#9e9d9d;" href="/tutorial/predefined-functions/1/391">
                    <small># Tutorial: Predefined Functions</small> 
                </a>
            </div>
           
                        <a style="color:#9e9d9d;" href="/qa/-/1/30">
                <small><i class="fa fa-code" aria-hidden="true"></i> Question: Function in C Language</small>
            </a>
                                   <span class="float-end" style="color:#9e9d9d"> <small><i class="fa fa-check" aria-hidden="true"></i> Short </small></span>  
                            </div>
      
            <div class="list-group divStyleQuestion" style="border:1px solid #e0e0e0;padding:8px">
            <div class="d-flex w-100 justify-content-between">
              <a href="/qanswer/what-is-the-difference-between-strcpy-and-strncpy-functions-in-c-question/29/670">
                  <h5 class="mb-1" style="color:#194757;">670. &nbsp;What is the difference between strcpy() and strncpy() functions in C?</h5>
              </a>
            </div>
                            <div class="card">
                  <div class="card-body">
                     Answer:  
<ul class="list-group">
<li class="list-group-item">  strcpy( ) function copies whole content of one string into another string. Whereas, strncpy( ) function copies portion of contents of one string into another string.</li>
<li class="list-group-item">If destination string length is less than source string, entire/specified source string value won&rsquo;t be copied into destination string in both cases.</li>
</ul>

<hr>

<p>Here's a comparison between the <code>strcpy()</code> and <code>strncpy()</code> functions in C:</p>
<table class = "table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Aspect</th>
<th><code>strcpy()</code></th>
<th><code>strncpy()</code></th>
</tr>
</thead>
<tbody>
<tr>
<td>Function</td>
<td>Copies a string from source to destination.</td>
<td>Copies a specified number of characters from source to destination.</td>
</tr>
<tr>
<td>Signature</td>
<td><code>char* strcpy(char* destination, const char* source);</code></td>
<td><code>char* strncpy(char* destination, const char* source, size_t num);</code></td>
</tr>
<tr>
<td>Null Terminator</td>
<td>Always adds a null terminator at the end of the copied string.</td>
<td>Adds null terminator only if the number of characters copied is less than the specified length <code>num</code>.</td>
</tr>
<tr>
<td>Buffer Safety</td>
<td>No built-in mechanism to prevent buffer overflow.</td>
<td>Provides some buffer safety by specifying the maximum number of characters to copy.</td>
</tr>
<tr>
<td>Length</td>
<td>Copies the entire string until the null terminator is found.</td>
<td>Allows specifying the maximum number of characters to copy, which can be smaller or larger than the source string length.</td>
</tr>
<tr>
<td>Return Value</td>
<td>Returns a pointer to the destination string.</td>
<td>Returns a pointer to the destination string.</td>
</tr>
</tbody>
</table>
<p>In summary, the main differences between <code>strcpy()</code> and <code>strncpy()</code> are that <code>strcpy()</code> copies the entire string until the null terminator, whereas <code>strncpy()</code> allows specifying the maximum number of characters to copy. Additionally, <code>strcpy()</code> always adds a null terminator, while <code>strncpy()</code> only adds a null terminator if the number of characters copied is less than the specified length. However, it's important to note that <code>strncpy()</code> does not automatically null-terminate the destination string if the specified length is equal to or greater than the length of the source string. Care should be taken to handle null termination correctly when using <code>strncpy()</code>.</p>

<p>The <code>strcpy()</code> function in C is used to copy a string from one character array (source) to another (destination). It stands for "string copy." Here is the general syntax of the <code>strcpy()</code> function:</p>


<pre class = "prettyprint"><xmp>
char* strcpy(char* destination, const char* source);

Parameters:

  • destination: A pointer to the destination character array where the string will be copied.
  • source: A pointer to the source character array, which contains the string to be copied.

Return Value:

  • The strcpy() function returns a pointer to the destination character array (destination).

Example Usage:


#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    strcpy(destination, source);

    printf("Copied string: %s\n", destination);

    return 0;
}

In the above example, the strcpy() function is used to copy the contents of the source array to the destination array. After the strcpy() function is called, the destination array will contain the same string as the source array, "Hello, World!". The resulting string is then printed using printf().

It's important to note that when using strcpy(), you need to ensure that the destination array is large enough to accommodate the copied string to avoid buffer overflow and memory corruption.