Write a c program to swap two numbers.

C Programming Language Pointer in C Language (Article) Pointer in C Language (Program)

1120

Here we used concept of function.

Here we used concept of pointer.

Here we used temp pointer variable.

Program:

<span class="com">#include</span><span class="pln"> </span><span class="str">&lt;stdio.h&gt;</span><span class="pln">
</span><span class="kwd">void</span><span class="pln"> swap</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> </span><span class="pun">*</span><span class="pln">a</span><span class="pun">,</span><span class="pln"> </span><span class="kwd">int</span><span class="pln"> </span><span class="pun">*</span><span class="pln">b</span><span class="pun">)</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
	</span><span class="kwd">int</span><span class="pln"> temp</span><span class="pun">;</span><span class="pln">
	temp </span><span class="pun">=</span><span class="pln"> </span><span class="pun">*</span><span class="pln">a</span><span class="pun">;</span><span class="pln">
	</span><span class="pun">*</span><span class="pln">a </span><span class="pun">=</span><span class="pln"> </span><span class="pun">*</span><span class="pln">b</span><span class="pun">;</span><span class="pln">
	</span><span class="pun">*</span><span class="pln">b </span><span class="pun">=</span><span class="pln"> temp</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span><span class="kwd">int</span><span class="pln"> main</span><span class="pun">()</span><span class="pln">
</span><span class="pun">{</span><span class="pln">
	</span><span class="kwd">int</span><span class="pln"> x</span><span class="pun">=</span><span class="lit">5</span><span class="pun">,</span><span class="pln">y</span><span class="pun">=</span><span class="lit">10</span><span class="pun">;</span><span class="pln">
	</span><span class="kwd">void</span><span class="pln"> swap</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> </span><span class="pun">*,</span><span class="kwd">int</span><span class="pln"> </span><span class="pun">*);</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%d %d\n"</span><span class="pun">,</span><span class="pln">x</span><span class="pun">,</span><span class="pln">y</span><span class="pun">);</span><span class="pln">
	swap</span><span class="pun">(&amp;</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> </span><span class="pun">&amp;</span><span class="pln">y</span><span class="pun">);</span><span class="pln">
	printf</span><span class="pun">(</span><span class="str">"%d %d\n"</span><span class="pun">,</span><span class="pln">x</span><span class="pun">,</span><span class="pln">y</span><span class="pun">);</span><span class="pln">
	</span><span class="kwd">return</span><span class="pln"> </span><span class="lit">0</span><span class="pun">;</span><span class="pln">
</span><span class="pun">}</span><span class="pln">
</span>

Output:

5 10
10 5

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.