<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nikhilesh2601</title>
    <description>The latest articles on DEV Community by Nikhilesh2601 (@nikhilesh2601).</description>
    <link>https://dev.to/nikhilesh2601</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F628343%2Fc0f60606-203c-4e91-8950-136a313edfee.png</url>
      <title>DEV Community: Nikhilesh2601</title>
      <link>https://dev.to/nikhilesh2601</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikhilesh2601"/>
    <language>en</language>
    <item>
      <title>Backtracking in JAVA</title>
      <dc:creator>Nikhilesh2601</dc:creator>
      <pubDate>Thu, 25 Nov 2021 13:37:36 +0000</pubDate>
      <link>https://dev.to/nikhilesh2601/backtracking-in-java-3la0</link>
      <guid>https://dev.to/nikhilesh2601/backtracking-in-java-3la0</guid>
      <description>&lt;p&gt;Backtracking is an algorithmic-technique for solving problems recursively by trying to build a solution incrementally, one piece at a time, removing those solutions that fail to satisfy the constraints of the problem at any point of time (by time, here, is referred to the time elapsed till reaching any level of the search tree)&lt;/p&gt;

&lt;p&gt;There are three types of problems in backtracking –  &lt;/p&gt;

&lt;p&gt;1)Decision Problem – In this, we search for a feasible solution.&lt;br&gt;
2)Optimization Problem – In this, we search for the best solution.&lt;br&gt;
3)Enumeration Problem – In this, we find all feasible solutions.&lt;br&gt;
4)How to determine if a problem can be solved using Backtracking?&lt;/p&gt;

&lt;p&gt;Generally, every constraint satisfaction problem which has clear and well-defined constraints on any objective solution, that incrementally builds candidate to the solution and abandons a candidate (“backtracks”) as soon as it determines that the candidate cannot possibly be completed to a valid solution, can be solved by Backtracking. However, most of the problems that are discussed, can be solved using other known algorithms like Dynamic Programming or Greedy Algorithms in logarithmic, linear, linear-logarithmic time complexity in order of input size, and therefore, outshine the backtracking algorithm in every respect (since backtracking algorithms are generally exponential in both time and space). However, a few problems still remain, that only have backtracking algorithms to solve them until now. &lt;/p&gt;

&lt;p&gt;Consider a situation that you have three boxes in front of you and only one of them has a gold coin in it but you do not know which one. So, in order to get the coin, you will have to open all of the boxes one by one. You will first check the first box, if it does not contain the coin, you will have to close it and check the second box and so on until you find the coin. This is what backtracking is, that is solving all sub-problems one by one in order to reach the best possible solution. &lt;/p&gt;

&lt;p&gt;Consider the below example to understand the Backtracking approach more formally, &lt;/p&gt;

&lt;p&gt;Given an instance of any computational problem P  and data D  corresponding to the instance, all the constraints that need to be satisfied in order to solve the problem are represented by C  . A backtracking algorithm will then work as follows: &lt;/p&gt;

&lt;p&gt;The Algorithm begins to build up a solution, starting with an empty solution set S  . S = {} &lt;/p&gt;

&lt;p&gt;Add to S  the first move that is still left (All possible moves are added to S  one by one). This now creates a new sub-tree s  in the search tree of the algorithm.&lt;br&gt;
Check if S+s  satisfies each of the constraints in C  . &lt;br&gt;
If Yes, then the sub-tree s  is “eligible” to add more “children”.&lt;br&gt;
Else, the entire sub-tree s  is useless, so recurs back to step 1 using argument S  .&lt;br&gt;
In the event of “eligibility” of the newly formed sub-tree s  , recurs back to step 1, using argument S+s  .&lt;br&gt;
If the check for S+s  returns that it is a solution for the entire data D  . Output and terminate the program. &lt;br&gt;
If not, then return that no solution is possible with the current s  and hence discard it.&lt;/p&gt;

&lt;p&gt;Difference between Recursion and Backtracking:&lt;br&gt;
In recursion, the function calls itself until it reaches a base case. In backtracking, we use recursion to explore all the possibilities until we get the best result for the problem.&lt;/p&gt;

&lt;p&gt;Pseudo Code for Backtracking :  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recursive backtracking solution. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;void findSolutions(n, other params) :&lt;br&gt;
    if (found a solution) :&lt;br&gt;
        solutionsFound = solutionsFound + 1;&lt;br&gt;
        displaySolution();&lt;br&gt;
        if (solutionsFound &amp;gt;= solutionTarget) : &lt;br&gt;
            System.exit(0);&lt;br&gt;
        return&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (val = first to last) :
    if (isValid(val, n)) :
        applyValue(val, n);
        findSolutions(n+1, other params);
        removeValue(val, n);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Finding whether a solution exists or not &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;boolean findSolutions(n, other params) :&lt;br&gt;
    if (found a solution) :&lt;br&gt;
        displaySolution();&lt;br&gt;
        return true;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for (val = first to last) :
    if (isValid(val, n)) :
        applyValue(val, n);
        if (findSolutions(n+1, other params))
            return true;
        removeValue(val, n);
    return false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let us try to solve a standard Backtracking problem, N-Queen Problem. &lt;br&gt;
The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. For example, following is a solution for 4 Queen problem. &lt;/p&gt;

&lt;p&gt;The expected output is a binary matrix which has 1s for the blocks where queens are placed. For example, following is the output matrix for the above 4 queen solution. &lt;/p&gt;

&lt;p&gt;{ 0,  1,  0,  0}&lt;br&gt;
{ 0,  0,  0,  1}&lt;br&gt;
{ 1,  0,  0,  0}&lt;br&gt;
{ 0,  0,  1,  0}&lt;/p&gt;

&lt;p&gt;Backtracking Algorithm: The idea is to place queens one by one in different columns, starting from the leftmost column. When we place a queen in a column, we check for clashes with already placed queens. In the current column, if we find a row for which there is no clash, we mark this row and column as part of the solution. If we do not find such a row due to clashes then we backtrack and return false. &lt;/p&gt;

&lt;p&gt;1) Start in the leftmost column&lt;br&gt;
2) If all queens are placed&lt;br&gt;
    return true&lt;br&gt;
3) Try all rows in the current column.  Do following for every tried row.&lt;br&gt;
    a) If the queen can be placed safely in this row then mark this [row, &lt;br&gt;
        column] as part of the solution and recursively check if placing&lt;br&gt;&lt;br&gt;
        queen here leads to a solution.&lt;br&gt;
    b) If placing the queen in [row, column] leads to a solution then return &lt;br&gt;
        true.&lt;br&gt;
    c) If placing queen doesn't lead to a solution then unmark this &lt;a href="https://dev.toBacktrack"&gt;row, &lt;br&gt;
        column&lt;/a&gt; and go to step (a) to try other rows.&lt;br&gt;
4) If all rows have been tried and nothing worked, return false to trigger &lt;br&gt;
    backtracking.&lt;/p&gt;

&lt;p&gt;**Examples for backtracking:&lt;/p&gt;

&lt;p&gt;Program-1:&lt;/p&gt;

&lt;p&gt;import java.util.Scanner;&lt;br&gt;
public class pro1 {&lt;br&gt;
public static void main(String[] args){&lt;br&gt;
Scanner scanner = new Scanner(System.in);&lt;br&gt;
System.out.println("Enter the values of m and n:");&lt;br&gt;
int m = scanner.nextInt();&lt;br&gt;
int n = scanner.nextInt();&lt;br&gt;
int result = grid(1,1, m, n);&lt;br&gt;
System.out.println("Result: "+ result);&lt;br&gt;
}&lt;br&gt;
public static int grid(int x, int y, int m, int n){&lt;br&gt;
if(x == m &amp;amp;&amp;amp; y == n){&lt;br&gt;
return 1;&lt;br&gt;
}&lt;br&gt;
else if(x &amp;lt;= m &amp;amp;&amp;amp; y &amp;lt;= n){&lt;br&gt;
return grid(x+1, y, m, n) + grid(x, y+1, m, n);&lt;br&gt;
}&lt;br&gt;
else{&lt;br&gt;
return 0;&lt;br&gt;
}&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Program-2:&lt;/p&gt;

&lt;p&gt;import java.util.Scanner;&lt;br&gt;
import java.lang.Math;&lt;br&gt;
public class pro2 {&lt;br&gt;
private static double MAX = Math.pow(10, 9) + 7;&lt;br&gt;
public static int countOrders(int n) {&lt;br&gt;
int sumTillNow = 1;&lt;br&gt;
double productTillNow = 1;&lt;br&gt;
for (int i = 2; i &amp;lt;= n; i++) {&lt;br&gt;
for (int j = 2*i - 2; j &amp;lt;= 2*i - 1; j++) {&lt;br&gt;
sumTillNow += j;&lt;br&gt;
}&lt;br&gt;
productTillNow *= sumTillNow;&lt;br&gt;
productTillNow = productTillNow % MAX;&lt;br&gt;
}&lt;br&gt;
return (int)productTillNow;&lt;br&gt;
}&lt;br&gt;
public static void main(String[] args){&lt;br&gt;
Scanner scanner = new Scanner(System.in);&lt;br&gt;
System.out.println("Enter the values of n:");&lt;br&gt;
int n = scanner.nextInt();&lt;br&gt;
System.out.println("Result : "+ countOrders(n));&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Program-3:&lt;/p&gt;

&lt;p&gt;import java.util.Scanner;&lt;br&gt;
public class pro3 {&lt;br&gt;
private static int mod = 1000000007;&lt;br&gt;
public static void main(String[] args) {&lt;br&gt;
Scanner scanner = new Scanner(System.in);&lt;br&gt;
System.out.println("Enter n and k:");&lt;br&gt;
int n = scanner.nextInt();&lt;br&gt;
int k = scanner.nextInt();&lt;br&gt;
System.out.println("Result: " + rearrangeSticks(n, k));&lt;br&gt;
}&lt;br&gt;
public static int rearrangeSticks(int n, int k) {&lt;br&gt;
int[][] dp=new int[n+1][k+1];&lt;br&gt;
return helper(n,k,dp);&lt;br&gt;
}&lt;br&gt;
public static int helper(int n, int k, int[][] dp){&lt;br&gt;
int result=0;&lt;br&gt;
if(n==k){&lt;br&gt;
return 1;&lt;br&gt;
}&lt;br&gt;
if(k==0){&lt;br&gt;
return 0;&lt;br&gt;
}&lt;br&gt;
if(dp[n][k]==0){&lt;br&gt;
dp[n][k]=(int)((1L * helper(n - 1, k - 1,dp) + 1L * helper(n - 1, k,dp) * (n - 1)) % mod);&lt;br&gt;
}&lt;br&gt;
return dp[n][k];&lt;br&gt;
}&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;Note:&lt;br&gt;
For more examples, just visit leetcode and practice..!&lt;/p&gt;

</description>
      <category>java</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Structures and Unions in C</title>
      <dc:creator>Nikhilesh2601</dc:creator>
      <pubDate>Sun, 09 May 2021 13:48:38 +0000</pubDate>
      <link>https://dev.to/nikhilesh2601/structures-and-unions-in-c-26gm</link>
      <guid>https://dev.to/nikhilesh2601/structures-and-unions-in-c-26gm</guid>
      <description>&lt;p&gt;Structures in c:&lt;/p&gt;

&lt;p&gt;Arrays allow to define type of variables that can hold several data items of the same kind. Similarly structure is another user defined data type available in C that allows to combine data items of different kinds.&lt;/p&gt;

&lt;p&gt;Structures are used to represent a record. Suppose you want to keep track of your books in a library. You might want to track the following attributes about each book &lt;/p&gt;

&lt;p&gt;Title&lt;br&gt;
Author&lt;br&gt;
Subject&lt;br&gt;
Book ID&lt;br&gt;
Defining a Structure&lt;br&gt;
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows &lt;/p&gt;

&lt;p&gt;struct [structure tag] {&lt;/p&gt;

&lt;p&gt;member definition;&lt;br&gt;
   member definition;&lt;br&gt;
   ...&lt;br&gt;
   member definition;&lt;br&gt;
} [one or more structure variables];  &lt;/p&gt;

&lt;p&gt;The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure's definition, before the final semicolon, you can specify one or more structure variables but it is optional. Here is the way you would declare the Book structure −&lt;/p&gt;

&lt;p&gt;struct Books {&lt;br&gt;
   char  title[50];&lt;br&gt;
   char  author[50];&lt;br&gt;
   char  subject[100];&lt;br&gt;
   int   book_id;&lt;br&gt;
} book;&lt;br&gt;&lt;br&gt;
Accessing Structure Members:&lt;br&gt;
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. The following example shows how to use a structure in a program &lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;struct Books {&lt;br&gt;
   char  title[50];&lt;br&gt;
   char  author[50];&lt;br&gt;
   char  subject[100];&lt;br&gt;
   int   book_id;&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;int main( ) {&lt;/p&gt;

&lt;p&gt;struct Books Book1;        /* Declare Book1 of type Book &lt;em&gt;/&lt;br&gt;
   struct Books Book2;        /&lt;/em&gt; Declare Book2 of type Book */&lt;/p&gt;

&lt;p&gt;/* book 1 specification */&lt;br&gt;
   strcpy( Book1.title, "C Programming");&lt;br&gt;
   strcpy( Book1.author, "Nuha Ali"); &lt;br&gt;
   strcpy( Book1.subject, "C Programming Tutorial");&lt;br&gt;
   Book1.book_id = 6495407;&lt;/p&gt;

&lt;p&gt;/* book 2 specification */&lt;br&gt;
   strcpy( Book2.title, "Telecom Billing");&lt;br&gt;
   strcpy( Book2.author, "Zara Ali");&lt;br&gt;
   strcpy( Book2.subject, "Telecom Billing Tutorial");&lt;br&gt;
   Book2.book_id = 6495700;&lt;/p&gt;

&lt;p&gt;/* print Book1 info */&lt;br&gt;
   printf( "Book 1 title : %s\n", Book1.title);&lt;br&gt;
   printf( "Book 1 author : %s\n", Book1.author);&lt;br&gt;
   printf( "Book 1 subject : %s\n", Book1.subject);&lt;br&gt;
   printf( "Book 1 book_id : %d\n", Book1.book_id);&lt;/p&gt;

&lt;p&gt;/* print Book2 info */&lt;br&gt;
   printf( "Book 2 title : %s\n", Book2.title);&lt;br&gt;
   printf( "Book 2 author : %s\n", Book2.author);&lt;br&gt;
   printf( "Book 2 subject : %s\n", Book2.subject);&lt;br&gt;
   printf( "Book 2 book_id : %d\n", Book2.book_id);&lt;/p&gt;

&lt;p&gt;return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;When the above code is compiled and executed, it produces the following result &lt;/p&gt;

&lt;p&gt;Book 1 title : C Programming&lt;br&gt;
Book 1 author : Nuha Ali&lt;br&gt;
Book 1 subject : C Programming Tutorial&lt;br&gt;
Book 1 book_id : 6495407&lt;br&gt;
Book 2 title : Telecom Billing&lt;br&gt;
Book 2 author : Zara Ali&lt;br&gt;
Book 2 subject : Telecom Billing Tutorial&lt;br&gt;
Book 2 book_id : 6495700&lt;br&gt;
Structures as Function Arguments&lt;br&gt;
You can pass a structure as a function argument in the same way as you pass any other variable or pointer.&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;struct Books {&lt;br&gt;
   char  title[50];&lt;br&gt;
   char  author[50];&lt;br&gt;
   char  subject[100];&lt;br&gt;
   int   book_id;&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;/* function declaration */&lt;br&gt;
void printBook( struct Books book );&lt;/p&gt;

&lt;p&gt;int main( ) {&lt;/p&gt;

&lt;p&gt;struct Books Book1;        /* Declare Book1 of type Book &lt;em&gt;/&lt;br&gt;
   struct Books Book2;        /&lt;/em&gt; Declare Book2 of type Book */&lt;/p&gt;

&lt;p&gt;/* book 1 specification */&lt;br&gt;
   strcpy( Book1.title, "C Programming");&lt;br&gt;
   strcpy( Book1.author, "Nuha Ali"); &lt;br&gt;
   strcpy( Book1.subject, "C Programming Tutorial");&lt;br&gt;
   Book1.book_id = 6495407;&lt;/p&gt;

&lt;p&gt;/* book 2 specification */&lt;br&gt;
   strcpy( Book2.title, "Telecom Billing");&lt;br&gt;
   strcpy( Book2.author, "Zara Ali");&lt;br&gt;
   strcpy( Book2.subject, "Telecom Billing Tutorial");&lt;br&gt;
   Book2.book_id = 6495700;&lt;/p&gt;

&lt;p&gt;/* print Book1 info */&lt;br&gt;
   printBook( Book1 );&lt;/p&gt;

&lt;p&gt;/* Print Book2 info */&lt;br&gt;
   printBook( Book2 );&lt;/p&gt;

&lt;p&gt;return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;void printBook( struct Books book ) {&lt;/p&gt;

&lt;p&gt;printf( "Book title : %s\n", book.title);&lt;br&gt;
   printf( "Book author : %s\n", book.author);&lt;br&gt;
   printf( "Book subject : %s\n", book.subject);&lt;br&gt;
   printf( "Book book_id : %d\n", book.book_id);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;When the above code is compiled and executed, it produces the following result &lt;/p&gt;

&lt;p&gt;Book title : C Programming&lt;br&gt;
Book author : Nuha Ali&lt;br&gt;
Book subject : C Programming Tutorial&lt;br&gt;
Book book_id : 6495407&lt;br&gt;
Book title : Telecom Billing&lt;br&gt;
Book author : Zara Ali&lt;br&gt;
Book subject : Telecom Billing Tutorial&lt;br&gt;
Book book_id : 6495700&lt;br&gt;
Pointers to Structures&lt;br&gt;
You can define pointers to structures in the same way as you define pointer to any other variable −&lt;/p&gt;

&lt;p&gt;struct Books *struct_pointer;&lt;br&gt;
Now, you can store the address of a structure variable in the above defined pointer variable. To find the address of a structure variable, place the '&amp;amp;'; operator before the structure's name as follows −&lt;/p&gt;

&lt;p&gt;struct_pointer = &amp;amp;Book1;&lt;br&gt;
To access the members of a structure using a pointer to that structure, you must use the → operator as follows −&lt;/p&gt;

&lt;p&gt;struct_pointer-&amp;gt;title;&lt;br&gt;
Let us re-write the above example using structure pointer.&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;struct Books {&lt;br&gt;
   char  title[50];&lt;br&gt;
   char  author[50];&lt;br&gt;
   char  subject[100];&lt;br&gt;
   int   book_id;&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;/* function declaration */&lt;br&gt;
void printBook( struct Books *book );&lt;br&gt;
int main( ) {&lt;/p&gt;

&lt;p&gt;struct Books Book1;        /* Declare Book1 of type Book &lt;em&gt;/&lt;br&gt;
   struct Books Book2;        /&lt;/em&gt; Declare Book2 of type Book */&lt;/p&gt;

&lt;p&gt;/* book 1 specification */&lt;br&gt;
   strcpy( Book1.title, "C Programming");&lt;br&gt;
   strcpy( Book1.author, "Nuha Ali"); &lt;br&gt;
   strcpy( Book1.subject, "C Programming Tutorial");&lt;br&gt;
   Book1.book_id = 6495407;&lt;/p&gt;

&lt;p&gt;/* book 2 specification */&lt;br&gt;
   strcpy( Book2.title, "Telecom Billing");&lt;br&gt;
   strcpy( Book2.author, "Zara Ali");&lt;br&gt;
   strcpy( Book2.subject, "Telecom Billing Tutorial");&lt;br&gt;
   Book2.book_id = 6495700;&lt;/p&gt;

&lt;p&gt;/* print Book1 info by passing address of Book1 */&lt;br&gt;
   printBook( &amp;amp;Book1 );&lt;/p&gt;

&lt;p&gt;/* print Book2 info by passing address of Book2 */&lt;br&gt;
   printBook( &amp;amp;Book2 );&lt;/p&gt;

&lt;p&gt;return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;void printBook( struct Books *book ) {&lt;/p&gt;

&lt;p&gt;printf( "Book title : %s\n", book-&amp;gt;title);&lt;br&gt;
   printf( "Book author : %s\n", book-&amp;gt;author);&lt;br&gt;
   printf( "Book subject : %s\n", book-&amp;gt;subject);&lt;br&gt;
   printf( "Book book_id : %d\n", book-&amp;gt;book_id);&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;When the above code is compiled and executed, it produces the following result &lt;/p&gt;

&lt;p&gt;Book title : C Programming&lt;br&gt;
Book author : Nuha Ali&lt;br&gt;
Book subject : C Programming Tutorial&lt;br&gt;
Book book_id : 6495407&lt;br&gt;
Book title : Telecom Billing&lt;br&gt;
Book author : Zara Ali&lt;br&gt;
Book subject : Telecom Billing Tutorial&lt;br&gt;
Book book_id : 6495700&lt;/p&gt;

&lt;p&gt;Unions in c:&lt;/p&gt;

&lt;p&gt;A union is a special data type available in C that allows to store different data types in the same memory location. You can define a union with many members, but only one member can contain a value at any given time. Unions provide an efficient way of using the same memory location for multiple-purpose.&lt;/p&gt;

&lt;p&gt;Defining a Union:&lt;br&gt;
To define a union, you must use the union statement in the same way as you did while defining a structure. The union statement defines a new data type with more than one member for your program. The format of the union statement is as follows&lt;/p&gt;

&lt;p&gt;union [union tag] {&lt;br&gt;
   member definition;&lt;br&gt;
   member definition;&lt;br&gt;
   ...&lt;br&gt;
   member definition;&lt;br&gt;
} [one or more union variables];&lt;br&gt;&lt;br&gt;
The union tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the union's definition, before the final semicolon, you can specify one or more union variables but it is optional. Here is the way you would define a union type named Data having three members i, f, and str −&lt;/p&gt;

&lt;p&gt;union Data {&lt;br&gt;
   int i;&lt;br&gt;
   float f;&lt;br&gt;
   char str[20];&lt;br&gt;
} data;&lt;br&gt;&lt;br&gt;
Now, a variable of Data type can store an integer, a floating-point number, or a string of characters. It means a single variable, i.e., same memory location, can be used to store multiple types of data. You can use any built-in or user defined data types inside a union based on your requirement.&lt;/p&gt;

&lt;p&gt;The memory occupied by a union will be large enough to hold the largest member of the union. For example, in the above example, Data type will occupy 20 bytes of memory space because this is the maximum space which can be occupied by a character string. The following example displays the total memory size occupied by the above union &lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;union Data {&lt;br&gt;
   int i;&lt;br&gt;
   float f;&lt;br&gt;
   char str[20];&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;int main( ) {&lt;/p&gt;

&lt;p&gt;union Data data;        &lt;/p&gt;

&lt;p&gt;printf( "Memory size occupied by data : %d\n", sizeof(data));&lt;/p&gt;

&lt;p&gt;return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;When the above code is compiled and executed, it produces the following result −&lt;/p&gt;

&lt;p&gt;Memory size occupied by data : 20&lt;br&gt;
Accessing Union Members&lt;br&gt;
To access any member of a union, we use the member access operator (.). The member access operator is coded as a period between the union variable name and the union member that we wish to access. You would use the keyword union to define variables of union type. The following example shows how to use unions in a program&lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;union Data {&lt;br&gt;
   int i;&lt;br&gt;
   float f;&lt;br&gt;
   char str[20];&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;int main( ) {&lt;/p&gt;

&lt;p&gt;union Data data;        &lt;/p&gt;

&lt;p&gt;data.i = 10;&lt;br&gt;
   data.f = 220.5;&lt;br&gt;
   strcpy( data.str, "C Programming");&lt;/p&gt;

&lt;p&gt;printf( "data.i : %d\n", data.i);&lt;br&gt;
   printf( "data.f : %f\n", data.f);&lt;br&gt;
   printf( "data.str : %s\n", data.str);&lt;/p&gt;

&lt;p&gt;return 0;&lt;br&gt;
}&lt;/p&gt;

&lt;p&gt;When the above code is compiled and executed, it produces the following result &lt;/p&gt;

&lt;p&gt;data.i : 1917853763&lt;br&gt;
data.f : 4122360580327794860452759994368.000000&lt;br&gt;
data.str : C Programming&lt;br&gt;
Here, we can see that the values of i and f members of union got corrupted because the final value assigned to the variable has occupied the memory location and this is the reason that the value of str member is getting printed very well.&lt;/p&gt;

&lt;p&gt;Now let's look into the same example once again where we will use one variable at a time which is the main purpose of having unions &lt;/p&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;h1&gt;
  
  
  include 
&lt;/h1&gt;

&lt;p&gt;union Data {&lt;br&gt;
   int i;&lt;br&gt;
   float f;&lt;br&gt;
   char str[20];&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;int main( ) {&lt;/p&gt;

&lt;p&gt;union Data data;        &lt;/p&gt;

&lt;p&gt;data.i = 10;&lt;br&gt;
   printf( "data.i : %d\n", data.i);&lt;/p&gt;

&lt;p&gt;data.f = 220.5;&lt;br&gt;
   printf( "data.f : %f\n", data.f);&lt;/p&gt;

&lt;p&gt;strcpy( data.str, "C Programming");&lt;br&gt;
   printf( "data.str : %s\n", data.str);&lt;/p&gt;

&lt;p&gt;return 0;&lt;br&gt;
}&lt;br&gt;
When the above code is compiled and executed, it produces the following result −&lt;/p&gt;

&lt;p&gt;data.i : 10&lt;br&gt;
data.f : 220.500000&lt;br&gt;
data.str : C Programming&lt;/p&gt;

</description>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
