-->

Program to show use of pointer.

using codeblocks
----------------------------------------------------------------------------------------

//understanding pointer                //comment of program
#include <stdio.h>                     //header file
int main()
{
    int *pointer;                      //pointer declaration
    int x=4;                           //value assignment
    pointer=&x;                        //assigning address to that pointer
    printf("the address of x is=%d\n",pointer);//display of that address
    printf("the  value of x=%d",*pointer);    //display of value stored in that address. we display that             using indirection operator
    return 0;
}

------------------------------------------------------------------------------------
using tuboc++
--------------------------------------------------------------------------------------
//understanding pointer                //comment of program
#include <stdio.h>                     //header file
#include<conio.h>
int main()
{
    int *pointer;                      //pointer declaration
    int x=4;                           //value assignment
    pointer=&x;                        //assigning address to that pointer
    printf("the address of x is=%d\n",pointer);//display of that address
    printf("the  value of x=%d",*pointer);    
//display of value stored in that address. we display that using indirection operator
   getch();
    return 0;
}

program to show total bytes used by union ,using sizeof().

using codeblocks
-------------------------------------------------------------------------
//sizeof  union with some data
#include <stdio.h>                         //header file
union det                                  //union tag
{
    char name[30];                      //first member
    char address[70];                   //second member
    int roll_no;                        //third member
}acc;                                   //data variable
int main()
{
    printf("total  SIZE=%d",sizeof(acc));//prints total bytes occupied by variable 'acc'.
    return 0;
}

//note: it finds the sum of all members with the largest one and other.
-------------------------------------------------------------------------------------------------------

using turbo c++
----------------------------------------------------------------------------------------------
//sizeof  union with some data
#include <stdio.h>                         //header file
#include<conio.h
union det                                  //union tag
{
    char name[30];                      //first member
    char address[70];                   //second member
    int roll_no;                        //third member
}acc;                                   //data variable
int main()
{
    printf("total  SIZE=%d",sizeof(acc));//prints total bytes occupied by variable acc.
    getch();
    return 0;
}



program to initialize union

using codeblocks
----------------------------------------------------------------------------------

This does not work!
---------------------------------

//initializing  union with some data
#include <stdio.h>                         //header file
#include<string.h>
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc={"rajan","KTM"};                   //initializing data
int main()
{
    printf("the name is ");
    puts(acc.name);                    //puts data on screen i.e. name
    printf("the address is\n");
    puts(acc.address);                  //puts data on screen but does not get displayed on screen
    return 0;
}
// we have tried to access both members at same time.
//so, We get same data "rajan" two times. We do not get "KTM".
----------------------------------------------------------------------------------------------------------------

This works!
-------------------------------------------------------

//initializing  union with some data
#include <stdio.h>                         //header file
#include<string.h>                         //handles strings
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;                                //data variable
int main()
{
    strcpy(acc.name,"Rajan");         //data initialization and being copied to 'name'
    printf("the name is ");
    puts(acc.name);                    //puts data on screen i.e. name
    strcpy(acc.address,"KTM");         //data initialization and being copied to 'address'
    printf("the address is\n");
    puts(acc.address);                  //puts data on screen but does not get displayed on screen
    return 0;
}
// we have  accessed one member at a  time.
//so, We get different data "rajan" then ktm.
---------------------------------------------------------------------------------------------------------------
using turbo c++
----------------------------------------------------------------------------------------------
//initializing  union with some data
#include <stdio.h>                         //header file
#include<string.h>                         //handles strings
#include<conio.h>                        //header file
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;                                //data variable
int main()
{
    strcpy(acc.name,"Rajan");         //data initialization and being copied to 'name'
    printf("the name is ");
    puts(acc.name);                    //puts data on screen i.e. name
    strcpy(acc.address,"KTM");         //data initialization and being copied to 'address'
    printf("the address is\n");
    puts(acc.address);                  //puts data on screen but does not get displayed on screen
    getch();
     return 0;
}
// we have  accessed one member at a  time.
//so, We get different data "rajan" then ktm.


program show use of union with input and output.

using codeblocks
-----------------------------------------------------------------------------------------------------------
//understanding union with simple input and output
#include <stdio.h>                         //header file
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;
int main()
{
    printf("enter name\n");
    gets(acc.name);                    //gets input for name
    printf("the name is ");
    puts(acc.name);                    //puts data on screen
    printf("enter address\n");
    gets(acc.address);                  //gets address
    printf("the address is ");
    puts(acc.address);                  //puts data on screen
    return 0;
}

Note:-
// we can input one member at a time so, We have input first member only then second member and so on.
// If we input input all members at same time, It would get crashed and give unexpected result
------------------------------------------------------------------------------------------------------
using turbo c++
--------------------------------------------------------------------------------------------------------
//understanding union with simple input and output
#include <stdio.h>                         //header file
#include<conio.h>                      //header file to hold output
union det                                  //union tag
{
    char name[70];                      //first member
    char address[70];                   //second member
}acc;
int main()
{
    printf("enter name\n");
    gets(acc.name);                    //gets input for name
    printf("the name is ");
    puts(acc.name);                    //puts data on screen
    printf("enter address\n");
    gets(acc.address);                  //gets address
    printf("the address is ");
    puts(acc.address);                  //puts data on screen
    getch();                                  //holds output
    return 0;
}
// we can input one member at a time so, We have input first member only then second member and so on.
// If we input input all members at same time, It would get crashed and give unexpected result


program to find sum of two distances measured in feet and inch using function.


using codeblocks
---------------------------------------------------------------------------------------
//program to find sum of two distances measured in feet and inch using function.
//passing struct to function
//understanding struct to a function
#include<stdio.h>                           //header file
struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
void sum( struct distance d1,struct distance d2);//function prototype/declaration
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    sum(d1,d2);                               //calls the function
    return 0;
}
void sum(struct distance d1,struct distance d2) //function body line
{
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
}
---------------------------------------------------------------------------------------------------------
or
-----------------------------------------------------------------------------------------
//understanding struct to a function
#include<stdio.h>                           //header file
void sum( d1,d2);//function prototype/declaration with variables
struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    sum(d1,d2);                                            //calls the function
    return 0;
}
void sum(struct distance d1,struct distance d2) //function body line
{
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
}
-------------------------------------------------------------------------------------------------
using turbo c++
------------------------------------------------------------------------------------------------
//understanding struct to a function
#include<stdio.h>                           //header file
#include<conio.h>                       //header file to hold output
struct distance                             //struct tag
{
    int inch;                               //first member
    int feet;                               //second member
}d1,d2,tot;                                  //variables to access members
void sum( struct distance d1,struct distance d2);//function prototype/declaration
int main()
{
    printf("enter first inch and feet\n");
    scanf("%d%d",&d1.inch,&d1.feet);          //gets data
    printf("enter first inch and feet\n");
    scanf("%d%d",&d2.inch,&d2.feet);          //gets data
    sum(d1,d2);                               //calls the function
getch();    
return 0;
}
void sum(struct distance d1,struct distance d2) //function body line
{
    tot.inch=(d1.inch+d2.inch)%12;               //returns left part of inch
    tot.feet=d1.feet+d2.feet+(d1.inch+d2.inch)/12;//returns total feet
    printf("total inch=%d feet=%d\n",tot.inch,tot.feet);//prints result
}

Program to understand concept of nested structure of struct.

using codeblock
--------------------------------------------------------------------------------------
//understanding nested struct concept         //comment abt. program
#include <stdio.h>                            //header file to handle input and output
struct college                                //struct tag
{
    char name[50];                            //first member named name
    char address[50];                         //second member named address
};
struct student                               //second struct tag
{
char name[50];                              // member1
char stream[40];                            //member 2
struct college first;                       //acts as a member. It is a variable of first tag college
};
struct student info;
int main()
{
    printf("enter college name\n");
    gets(info.first.name);                 //accessing member of first tag for input
    printf("enter college address\n");
    gets(info.first.address);             //accessing member of first tag for input
    printf("enter student name\n");
    gets(info.name);                        //accessing member of second tag for input
    printf("enter student's stream\n");
    gets(info.stream);                      //accessing member of second tag for input
    printf("------------------------\n");
    printf(" college name is\n");
    puts(info.first.name);                   //display of data
    printf("college address is\n");
    puts(info.first.address);                 //display of data
    printf("student name is\n");
    puts(info.name);                           //display of data
    printf("student's stream is\n");
    puts(info.stream);                        //display of data
    return 0;
}
------------------------------------------------------------------------------------------------------------------

using turbo c++
---------------------------------------------------------------------------
//understanding nested struct concept         //comment abt. program
#include <stdio.h>                            //header file to handle input and output
#incluse<conio.h>
struct college                                //struct tag
{
    char name[50];                            //first member named name
    char address[50];                         //second member named address
};
struct student                               //second struct tag
{
char name[50];                              // member1
char stream[40];                            //member 2
struct college first;                       //acts as a member. It is a variable of first tag college
};
struct student info;
int main()
{
    printf("enter college name\n");
    gets(info.first.name);                 //accessing member of first tag for input
    printf("enter college address\n");
    gets(info.first.address);             //accessing member of first tag for input
    printf("enter student name\n");
    gets(info.name);                        //accessing member of second tag for input
    printf("enter student's stream\n");
    gets(info.stream);                      //accessing member of second tag for input
    printf("------------------------\n");
    printf(" college name is\n");
    puts(info.first.name);                   //display of data
    printf("college address is\n");
    puts(info.first.address);                 //display of data
    printf("student name is\n");
    puts(info.name);                           //display of data
    printf("student's stream is\n");
    puts(info.stream);                        //display of data
  getch();    
return 0;
}

Program read 10 students data (roll, name,class and marks in 5 subjects) and display that using struct with percentage.

using codeblocks
--------------------------------------------------------------------------------------------------------------------------------
//Program read 10 students data (roll, name,class and marks in 5 subjects) and display that using //struct with percentage.                      //comment abt. program
#include <stdio.h>                                 //header file
struct data                                        //struct tag named data
{
    char name[80];                                //member1 of struct
    char address[80];                             //member2 of struct
    int roll;                                     //member3 of struct
    int english,maths,physics,chemistry,nepali,total;   //marks member declaration
    float percentage;                       //member for total and percentage

}access[100];                 //'access' is a variable to access members. This can hold maximum 100 members.it is called array of struct.
int main()
{
   int n,i;                                         //variable declaration
   printf("enter total value of n\n");
   scanf("%d",&n);                                  //accepts total value
   for(i=0;i<n;i++)
   {                                                 //execution of loop with inputs
    puts("enter roll number \n");
   scanf("%d",&access[i].roll);                      //gets data in different member
   puts("enter name \n");
   scanf("%s",access[i].name);                       //gets name
   puts("enter address \n");
   scanf("%s",access[i].address);                    //gets address
   printf("enter english marks\n");
   scanf("%d",&access[i].english);                  //gets respective marks
   printf("enter maths marks\n");
   scanf("%d",&access[i].maths);
   printf("enter Physics marks\n");
   scanf("%d",&access[i].physics);
   printf("enter chemistry marks\n");
   scanf("%d",&access[i].chemistry);
   printf("enter nepali marks\n");
   scanf("%d",&access[i].nepali);
   access[i].total=access[i].english+access[i].maths+access[i].physics+access[i].chemistry+access[i].nepali;/finds total and stores
      access[i].percentage=(float)access[i].total/5;               //finds percentage and stores in respective member
   }
   printf("entered data are\n");                  //loop execution to display data
   for(i=0;i<n;i++)
   {
   printf("roll=%d,name=%s,address=%s, english=%d,maths=%d,physics=%d,chemistry=%d,nepali=%d\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali);
   }                                                //displays data

   printf(" data with total and percentage are\n");     // displays data with total and percentage
  for(i=0;i<n;i++)
   {
   printf("roll=%d,name=%s,address=%s, english=%d,maths=%d,physics=%d,chemistry=%d,nepali=%d, total=%d,percentage=%f\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali,access[i].total,access[i].percentage);
   }
    return 0;

}
-------------------------------------------------------------------------------------------------------------------------
using turbo c++
---------------------------------------------------------------------------------------
// program to enter detail of some  students       //comment abt. program
#include <stdio.h>                                 //header file
#include<conio.h>
struct data                                        //struct tag named data
{
    char name[80];                                //member1 of struct
    char address[80];                             //member2 of struct
    int roll;                                     //member3 of struct
    int english,maths,physics,chemistry,nepali,total;   //marks member declaration
    float percentage;                       //member for total and percentage

}access[100];                 //'access' is a variable to access members. This can hold maximum 100 members.it is called array of struct.
int main()
{
   int n,i;                                         //variable declaration
   printf("enter total value of n\n");
   scanf("%d",&n);                                  //accepts total value
   for(i=0;i<n;i++)
   {                                                 //execution of loop with inputs
    puts("enter roll number \n");
   scanf("%d",&access[i].roll);                      //gets data in different member
   puts("enter name \n");
   scanf("%s",access[i].name);                       //gets name
   puts("enter address \n");
   scanf("%s",access[i].address);                    //gets address
   printf("enter english marks\n");
   scanf("%d",&access[i].english);                  //gets respective marks
   printf("enter maths marks\n");
   scanf("%d",&access[i].maths);
   printf("enter Physics marks\n");
   scanf("%d",&access[i].physics);
   printf("enter chemistry marks\n");
   scanf("%d",&access[i].chemistry);
   printf("enter nepali marks\n");
   scanf("%d",&access[i].nepali);
   access[i].total=access[i].english+access[i].maths+access[i].physics+access[i].chemistry+access[i].nepali;/finds total and stores
      access[i].percentage=(float)access[i].total/5;               //finds percentage and stores in respective member
   }
   printf("entered data are\n");                  //loop execution to display data
   for(i=0;i<n;i++)
   {
   printf("roll=%d,name=%s,address=%s, english=%d,maths=%d,physics=%d,chemistry=%d,nepali=%d\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali);
   }                                                //displays data

   printf(" data with total and percentage are\n");     // displays data with total and percentage
  for(i=0;i<n;i++)
   {
   printf("roll=%d,name=%s,address=%s, english=%d,maths=%d,physics=%d,chemistry=%d,nepali=%d, total=%d,percentage=%f\n",access[i].roll,access[i].name,access[i].address,access[i].english
          ,access[i].maths,access[i].physics,access[i].chemistry,access[i].nepali,access[i].total,access[i].percentage);
   }
   getch();                              //holds the output.
    return 0;
}