-->

program to read data stored in a datafile and print on screen using fread().

using codeblocks
---------------------------------------------------------------------------------------------------
 program to read data stored in a datafile and print on screen using fread()(previous question).
--------------------------------------------------------------------------------------------------------------
//program to store and read employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                     //  pointer for file declaration
char choice;                                  //identifier declaration
printf('-----------------------');
printf("reading data\n");
k=fopen("employee.txt","r");//file opening

while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
                                                                //As it becomes less than 1, it stops
{
    printf("salary=%f and name=%s\n",var.salary,var.name);//prints the struct value
}
fclose(k);                                                //closing of file.
getch();
return 0;
}

--------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------

//program to store and read employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                     //  pointer for file declaration
char choice;                                  //identifier declaration
printf('-----------------------');
printf("reading data\n");
k=fopen("employee.txt","r");//file opening

while((fread(&var,sizeof(var),1,k))==1)// fread() reads the records till it is true.
                                                                //As it becomes less than 1, it stops
{
    printf("salary=%f and name=%s\n",var.salary,var.name);//prints the struct value
}
fclose(k);                                                //closing of file.
getch();
return 0;
}

//note: fread() does not accept EOF()/feof() function.

program to store/write employee name and salary in a datafile using fwrite()

using codeblocks
-------------------------------------------------------------------------------
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.name);
fwrite(&var,sizeof(var),1,k); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}








//or
/*
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.name);
fprintf(k,"%f %s\n",var.salary,var.name); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}
*/

-----------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.name);
fwrite(&var,sizeof(var),1,k); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}








//or
/*
//program to store employee name and salary using fwrite()
#include <stdio.h>
#include <stdlib.h>
struct employee
{
    char name[50];
    float salary;
}var;
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char choice;                                                    //identifier declaration
                                                            //       "             "
k=fopen("employee.txt","w");//file opening
do
{
printf("\nenter salary\n");
scanf("%f",&var.salary);
printf("enter  name\n");           // getting inputs
scanf("%s",var.name);
fprintf(k,"%f %s\n",var.salary,var.name); //writing data to data file
printf("want to continue (y/n):=");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
getch();
return 0;
}
*/

Program to store and read roll,name and percentage of some students to /from a data file using same program.

using codeblocks
--------------------------------------------------------------------------------
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
printf("we are going to write /store data\n");
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\n");//prints message
choice=getche();                      //gets a character                                  //getting a character from user
}while(choice!='n');        //writing repeats as you enter 'y'                                    // validating the input
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                                                            //closing data file
printf("-----------------------------------------------\n");
printf("read records are:\n");
k=fopen("student.txt","r");//file opening
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                                           //closing data file
getch();
return 0;
}

Program to read roll,name and percentage of some students from a data file

using codeblocks
-----------------------------------------------------------------------------------------
//reading data from a data file
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                          //       "             "
float per;
k=fopen("student.txt","r");          //opening of file in write mode
printf("records are\n");
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}


---------------------------------------------------------------------------------------------------
using turboc++
----------------------------------------------------------------------------------------------------------
//reading data from a data file
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                          //       "             "
float per;
k=fopen("student.txt","r");          //opening of file in write mode
printf("records are\n");
while((fscanf(k,"%d %f %s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
printf("roll=%d per=%f name=%s\n",roll,per,name);   //printing on screen
}
printf("\n reading process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}

Program to store roll,name and percentage of some students in a data file

using codeblocks
--------------------------------------------------------------------------------------------------
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\n");//prints message
choice=getche();                      //gets a character                                 
}while(choice!='n');        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                       
getch();
return 0;
}

//or
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                   //       "             "
float per;
k=fopen("student.txt","w");                 //opening of file in write mode
printf("press ctrl+z to exit\n");//message to terminate the program
printf("first enter roll then percentage then name\n");
while((scanf("%d%f%s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
fprintf(k,"%d %f %s\n",roll,per,name);   //writing data to data file
}
printf("\n writing process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}


---------------------------------------------------------------------------------------------------
using turboc++
----------------------------------------------------------------------------------------------------------
//to store roll,name and percentage of some students
#include<stdio.h>       //header file
#include<conio.h>
int main()
{
FILE *k;                                                                    //  pointer for file declaration
char name[30],choice;                                                    //identifier declaration
int roll;                                                              //       "             "
float per;
k=fopen("student.txt","w");//file opening
do
{
printf("\nenter roll\n");   //data input
scanf("%d",&roll);
printf("enter percentage\n");
scanf("%f",&per);
printf("enter  name\n");           // getting inputs
scanf("%s",name);
fprintf(k,"%d %f %s\n",roll,per,name); //writing data to data file
printf("want to continue (y/n)\n");//prints message
choice=getche();                      //gets a character                                 
}while(choice!='n');        //writing repeats as you enter 'y'                             
printf("\n writing process completed successfully\n");
fclose(k);                            //closing of file                       
getch();
return 0;
}

//or
#include<stdio.h>
#include<conio.h>
int main()
{
FILE *k;                                     //  pointer for file declaration
char name[30],choice;                        //identifier declaration
int roll;                                   //       "             "
float per;
k=fopen("student.txt","w");                 //opening of file in write mode
printf("press ctrl+z to exit\n");//message to terminate the program
printf("first enter roll then percentage then name\n");
while((scanf("%d%f%s",&roll,&per,name))!=EOF)//inputs goes until the file ends(End of File)
{
fprintf(k,"%d %f %s\n",roll,per,name);   //writing data to data file
}
printf("\n writing process completed successfully\n");
fclose(k);                                       //closing data file
getch();
return 0;
}

Program to store and read data stored in a data file using fprintf() and fscanf() function

using codeblocks
-------------------------------------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
#include<conio.h>
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
       fprintf(p,"%d\n",i);                        // write the entered integer in that file
    }
    fclose(p);                              //closes the file
    getch();
    printf("data/numbers stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data1.txt","r");               // openes the file in read only mode
    while((fscanf(p,"%d",&i))!=EOF)    //read an integer using fscanf() from opened file util it ends.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((fscanf(p,"%d",&i))!=EOF)             //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/
--------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------
//using fprintf() to store data(one integer) and fscanf()() to read (one integer)that data from an opened //file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
#include<conio.h>
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
       fprintf(p,"%d\n",i);                        // write the entered integer in that file
        
    }
    fclose(p);                              //closes the file
    getch();
    printf("data/numbers stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data1.txt","r");               // openes the file in read only mode
    while((fscanf(p,"%d",&i))!=EOF)    //read an integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/

program to read that stored integers using getw

using codeblocks
-----------------------------------------------------------------------------
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}

----------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------------------------
#include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    getch();
    return 0;
}

Program to store integers in a data file

using codeblocks
------------------------------------------------------------------------------------------------------
//using putw() to store data(one integer) from an opened file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
         putw(i,p);                // write the entered integer in that file
            }
    fclose(p);                              //closes the file
getch();
return 0;
}
--------------------------------------------------------------------------------------------
usnig turboc++
--------------------------------------------------------------------------------------------------
//using putw() to store data(one integer) from an opened file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
#include<conio.h>
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
         putw(i,p);                // write the entered integer in that file
            }
    fclose(p);                              //closes the file
getch();
return 0;
}


program to store a number and to read that using putw and getw

using codeblocks
---------------------------------------------------------------------------------------------------------------
//using putw() to store data(one integer) and getw() to read (one integer)that data from an opened file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
       // fprintf(p,"%d\n",i);                        // write the entered integer in that file
        putw(i,p);
    }
    fclose(p);                              //closes the file
    printf("data/numbers stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)    //read an integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/
-----------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//using putw() to store data(one integer) and getw() to read (one integer)that data from an opened file
#include <stdio.h>
#include<stdlib.h>                       // header file for system("cls")
#include<conio.h>
int main()

{

    FILE *p;                               //pointer declaration
    char ch;
    int i;
    system("cls");                         //clears the screen/console
    p=fopen("data1.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    for(i=0;i<=40;i++)             //executes the loop
    {
       // fprintf(p,"%d\n",i);                        // write the entered integer in that file
        putw(i,p);
    }
    fclose(p);                              //closes the file
    printf("data/numbers stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)    //read an integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
   getch();
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
p=fopen("data1.txt","r");               // openes the file in read only mode
    while((i=getw(p))!=EOF)              //read an  integer using getw from opened file.
    {

        printf("%d,",i);                //prints that integer on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/

Program to store data/character in an opened file then read that character and print on screen in same program.

using codeblocks
----------------------------------------------------------------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened //file
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file
    }
    fclose(p);                              //closes the file
    printf("data stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");               // openes the file in read only mode
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
//note: The second part (reading file), we can include in
//different file rather in same file as written here.
// But we have to know its data file name.
//i mean to say, the code would be :

/*#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");               // openes the file in read only mode
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
*/
------------------------------------------------------------------------------------------------------------
using turboc++
---------------------------------------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened //file
#include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file
    }
    fclose(p);                              //closes the file
    printf("data stored successfully!!!\n");
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");               // openes the file in read only mode
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
     getch();
    return 0;
}

Program to read data/character from opened file.

using codeblocks
----------------------------------------------------------------------------------
//using getc() to read (one character)that data from an opened
 #include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");           // openes the file in read only mode. We should know the file name.
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
    return 0;
}
-------------------------------------------------------------------------------------------
using turboc++
-----------------------------------------------------------------------------------------------------------

//using getc() to read (one character)that data from an opened
 #include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    printf("now we are going to read that stored data\n");
    p=fopen("data.txt","r");           // openes the file in read only mode. We should know the file name.
    while((ch=getc(p))!=EOF)               //read a character using getc from opened file.
    {
        putchar(ch);                      //prints that character on screen
    }
    fclose(p);                            //closes the file
getch();
    return 0;
}

Program to store data/character in a datafile

using codeblocks
--------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened file
#include <stdio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file of pointer 'p'.
    }
    fclose(p);                              //closes the file
  }
-------------------------------------------------------------------------------------------------
using turbo c++
----------------------------------------------------------------------------------------
//using putc() to store data(one character) and getc() to read (one character)that data from an opened file
#include <stdio.h>
#include<conio.h>
int main()
{
    FILE *p;                               //pointer declaration
    char ch;
    p=fopen("data.txt","w");               //file opening in write mode
    printf("first we store/write data in an opened file\n");
    printf("press ctrl+z to exit\n");
    while((ch=getchar())!=EOF)              //gets one character until you press ctrl+z/terminate the input
    {
        putc(ch,p);                        // write the entered character in that file of pointer 'p'.
    }
    fclose(p);                              //closes the file
getch();
  }

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

using codeblocks
------------------------------------------------------------
//using typedef and function to print sum of two feet and inches
#include <stdio.h>
 typedef struct               //creating a new tag called feet_inch and supplying struct property to it.
{
    int feet1,feet2;          //members
    int inch1,inch2;
    int total_feet,total_inch;
}feet_inch;                  //works like tag; but is a variable
void sum( feet_inch var, feet_inch var1);   //function prototype.
int main()
{
    feet_inch var, var1;       // creating two variables
    printf("enter first feet and inch\n");
    scanf("%d%d",&var.feet1,&var.inch1);//gets input
    printf("enter second feet and inch\n");
    scanf("%d%d",&var1.feet2,&var1.inch2);//gets input
    sum(var,var1);                         //calling with parameter var and var1
    return 0;
}
void sum( feet_inch var,feet_inch var1)
{
 feet_inch total;                          // creating third variable to store total
total.total_feet=var.feet1+var1.feet2+(var.inch1+var1.inch2)/12;//finds total feet
total.total_inch=(var.inch1+var1.inch2)%12;                //finds total inch
printf("total feet=%d and total inch=%d\n",total.total_feet,total.total_inch);//prints output
}

//note: Let's not declare function at the top like we do in functions. If it is then it would not work.
-----------------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//using typedef and function to print sum of two feet and inches
#include <stdio.h>
#include<conio.h>
 typedef struct               //creating a new tag called feet_inch and supplying struct property to it.
{
    int feet1,feet2;          //members
    int inch1,inch2;
    int total_feet,total_inch;
}feet_inch;                  //works like tag; but is a variable
void sum( feet_inch var, feet_inch var1);   //function prototype.
int main()
{
    feet_inch var, var1;       // creating two variables
    printf("enter first feet and inch\n");
    scanf("%d%d",&var.feet1,&var.inch1);//gets input
    printf("enter second feet and inch\n");
    scanf("%d%d",&var1.feet2,&var1.inch2);//gets input
    sum(var,var1);                         //calling with parameter var and var1
   getch();
    return 0;
}
void sum( feet_inch var,feet_inch var1)
{
 feet_inch total;                          // creating third variable to store total
total.total_feet=var.feet1+var1.feet2+(var.inch1+var1.inch2)/12;//finds total feet
total.total_inch=(var.inch1+var1.inch2)%12;                //finds total inch
printf("total feet=%d and total inch=%d\n",total.total_feet,total.total_inch);//prints output
}

//note: Let's not declare function at the top like we do in functions. If it is then it would not work.


program to understand typedef

using codeblocks
------------------------------------------------------------------------------------------
//understanding typedef
#include <stdio.h>
int main()
{
    typedef int second_type;// creates another data type named second_type. It gets all property of 'int'
    second_type a,b,c;      // second_type is working as integer data type.
    printf("enter a,b,and c\n");
    scanf("%d%d%d",&a,&b,&c);  //gets input
    printf("a=%d,b=%d and c=%d",a,b,c);//prints output
    return 0;
}

------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------

//understanding typedef
#include <stdio.h>
#include<conio.h>
int main()
{
    typedef int second_type;// creates another data type named second_type. It gets all property of 'int'
    second_type a,b,c;      // second_type is working as integer data type.
    printf("enter a,b,and c\n");
    scanf("%d%d%d",&a,&b,&c);  //gets input
    printf("a=%d,b=%d and c=%d",a,b,c);//prints output
   getch(); 
  return 0;
}

Program to understand typedef for struct.

using codeblocks
----------------------------------------------------------------------------------------
//understanding typedef with struct
#include <stdio.h>
typedef struct                   // typedef for struct to assign its property to access
{
    char name[40];              //members
    char add[40];
    int roll;
}access;                         // it is being tag (struct). It has got all property of struct
    access first;                 //variable of tag access. we can also put inside main function.
int main()
{
    printf("enter name\n");
    gets(first.name);            //gets input
    printf("enter add\n");
    gets(first.add);
    printf("enter roll\n");
    scanf("%d",&first.roll);
    printf("name=%s , address=%s and roll=%d\n",first.name,first.add,first.roll);//displays output.
    return 0;
}
-------------------------------------------------------------------------------------
Or
--------------------------------------------------------------------------------
//understanding typedef with struct
#include <stdio.h>
typedef struct   tag                // typedef for struct to assign its property to access via 'tag'
{
    char name[40];              //members
    char add[40];
    int roll;
}access;                         // it is being tag (struct). It has got all property of struct
    access first;                 //variable of tag access. we can also put inside main function.
int main()
{
    printf("enter name\n");
    gets(first.name);            //gets input
    printf("enter add\n");
    gets(first.add);
    printf("enter roll\n");
    scanf("%d",&first.roll);
    printf("name=%s , address=%s and roll=%d\n",first.name,first.add,first.roll);//displays output.
    return 0;
}

//note: putting a tag doesnot make any different. We have to use same method as given in above //example.
//Mostly, the tag is unimportant.

---------------------------------------------------------------------------------------------------
using turboC++
-------------------------------------------------------------------------

//understanding typedef with struct
#include <stdio.h>
#include<conio.h>
typedef struct                   // typedef for struct to assign its property to access
{
    char name[40];              //members
    char add[40];
    int roll;
}access;                         // it is being tag (struct). It has got all property of struct
    access first;                 //variable of tag access. we can also put inside main function.
int main()
{
    printf("enter name\n");
    gets(first.name);            //gets input
    printf("enter add\n");
    gets(first.add);
    printf("enter roll\n");
    scanf("%d",&first.roll);
    printf("name=%s , address=%s and roll=%d\n",first.name,first.add,first.roll);//displays output.
    getch();
    return 0;
}

program to copy a string to another using pointer passed to function

using codeblocks
--------------------------------------------------------------------------------------------------------------
//program to copy a string to another using pointer passed to function// program title
#include <stdio.h>                   //header file
#include <string.h>                   // header file to handle string
void string_length(char [],char[]);    // function prototype with character array
int main()
{
    char string[50],string1[50];            // character string
    puts("enter string1 and string2\n");
    gets(string);              //gets string
    gets(string1);
    string_length(string,string1);     //calling of function with string as parameter
                               // here, string is passed  as reference to formal parameter
    return 0;
}
void string_length(char *strings,char *strings1)// this accepts string with address from main function
{
     puts(strings);
     puts(strings1);
     printf("after copying\n");
     strcpy(strings,strings1);        // copies string
     puts(strings);          //displays string
}
//note: we can also use char string[] with void string_length in body line. It works.
//once you try.It runs.
//but we can not pass string as pointer in prototype.
// i.e. void string_length(char *[]);
--------------------------------------------------------------------------------------------
using turboc++
------------------------------------------------------------------------------------------
//program to copy a string to another using pointer passed to function// program title
#include <stdio.h>                   //header file
#include <string.h>                   // header file to handle string
#include<conio.h>
void string_length(char [],char[]);    // function prototype with character array
int main()
{
    char string[50],string1[50];            // character string
    puts("enter string1 and string2\n");
    gets(string);              //gets string
    gets(string1);
    string_length(string,string1);     //calling of function with string as parameter
                               // here, string is passed  as reference to formal parameter
getch();    
return 0;
}
void string_length(char *strings,char *strings1)// this accepts string with address from main function
{
     puts(strings);
     puts(strings1);
     printf("after copying\n");
     strcpy(strings,strings1);        // copies string
     puts(strings);          //displays string
}
//note: we can also use char string[] with void string_length in body line. It works.
//once you try.It runs.
//but we can not pass string as pointer in prototype.
// i.e. void string_length(char *[]);

program to reverse a string using pointer. Pass pointer to function.

using codeblocks
-----------------------------------------------------------------------------------------------------
//program to find reverse of string using pointer passed to function// program title
#include <stdio.h>                   //header file
#include <string.h>                   // header file to handle string
void string_length(char []);    // function prototype with character array
int main()
{
    char string[50];            // character string
    puts("enter string\n");
    gets(string);              //gets string
    string_length(string);     //calling of function with string as parameter
                               // here, string is passed  as reference to formal parameter
    return 0;
}
void string_length(char *strings)// this accepts string with address
{
     strrev(strings);        // supplies length
     puts(strings);          //displays string
}
//note: we can also use char string[] with void string_length in body line. It works.
//once you try.It runs.
//but we can not pass string as pointer in prototype.
// i.e. void string_length(char *[]);
---------------------------------------------------------------------------------------

using turboC++
------------------------------------------------------------------------------------------------
//program to find reverse of string using pointer passed to function// program title
#include <stdio.h>                   //header file
#include <string.h>                   // header file to handle string
#include<conio.h>
void string_length(char []);    // function prototype with character array
int main()
{
    char string[50];            // character string
    puts("enter string\n");
    gets(string);              //gets string
    string_length(string);     //calling of function with string as parameter
                               // here, string is passed  as reference to formal parameter
   getch();
    return 0;
}
void string_length(char *strings)// this accepts string with address
{
     strrev(strings);        // supplies length
     puts(strings);          //displays string
}
//note: we can also use char string[] with void string_length in body line. It works.
//once you try.It runs.
//but we can not pass string as pointer in prototype.
// i.e. void string_length(char *[]);

program to get length of a string using pointer

using codeblocks
----------------------------------------------------------------------------------------------------------
//program to find length of string using pointer passed to function// program title
#include <stdio.h>                   //header file
#include <string.h>                   // header file to handle string
void string_length(char []);    // function prototype with character array.compiler supports this. You                                                      //can also write char string[50].
int main()
{
    char string[50];            // character string
    puts("enter string\n");
    gets(string);              //gets string
    string_length(string);     //calling of function with string as parameter
                               // here, string is passed  as reference to formal parameter
    return 0;
}
void string_length(char *strings)// this accepts string with address
{
   int length;
   length=strlen(strings);        // supplies length
   printf("total length=%d\n",length);//displays length
}

//note: we can also use char string[] with void string_length in body line. It works.
//once you try.It runs.
//but we can not pass string as pointer in prototype.
// i.e. void string_length(char *[]);

------------------------------------------------------------------------------------------------------
using turbo c++
-----------------------------------------------------------------------------------------------------
//program to find length of string using pointer passed to function// program title
#include <stdio.h>                   //header file
#include <string.h>                   // header file to handle string
#include<conio.h>
void string_length(char []);     // function prototype with character array.compiler supports this. You                                                      //can also write char string[50].

int main()
{
    char string[50];            // character string
    puts("enter string\n");
    gets(string);              //gets string
    string_length(string);     //calling of function with string as parameter
                               // here, string is passed  as reference to formal parameter
   getch();
    return 0;
}
void string_length(char *strings)// this accepts string with address
{
   int length;
   length=strlen(strings);        // supplies length
   printf("total length=%d\n",length);//displays length
}

//note: we can also use char string[] with void string_length in body line. It works.
//once you try.It runs.
//but we can not pass string as pointer in prototype.
// i.e. void string_length(char *[]);


Program to understand pointer passed to function.

using codeblocks
----------------------------------------------------------------------------------------------
//program to pick the greatest number using pointer passed to function
#include <stdio.h>            //header file
void max_number(int a[],int *);
int main()
{
    int a[100];                 //array declaration
    int i;
    int total;                   //variables declaration
    printf("enter value of total\n");
    scanf("%d",&total);              //input of total value of array
    printf("enter elements of array\n");
    for(i=0;i<total;i++)                   //execution of loop
    {
        printf("enter value for location=%d\t",i);
        scanf("%d",a+i);                  //input of element using array. here array acts as pointer.
                                          //It gets input using address starting from location 0.
    }
    max_number(a,&total);                //calling the function with passing of array and pointer
    return 0;
}
void max_number(int a[],int*p)
{
    int i,max;
    max=*(a+0);                          //It stores value stored in location 0

    for(i=0;i<*p;i++)                    //value of pointer being used
    {
       if(max<*(a+i))                   //comparison takes place
       {
         max=*(a+i);                    //assigning value
       }
    }
    printf("max value=%d",max);       //display of maximum value

}

----------------------------------------------------------------------------------------------------------
using turbo c++
------------------------------------------------------------------------------------------------------------------
//program to pick the greatest number using pointer passed to function
#include <stdio.h>            //header file
#include<conio.h>
void max_number(int a[],int *);
int main()
{
    int a[100];                 //array declaration
    int i;
    int total;                   //variables declaration
    printf("enter value of total\n");
    scanf("%d",&total);              //input of total value of array
    printf("enter elements of array\n");
    for(i=0;i<total;i++)                   //execution of loop
    {
        printf("enter value for location=%d\t",i);
        scanf("%d",a+i);                  //input of element using array. here array acts as pointer.
                                          //It gets input using address starting from location 0.
    }
    max_number(a,&total);                //calling the function with passing of array and pointer
   getch();
    return 0;
}
void max_number(int a[],int*p)
{
    int i,max;
    max=*(a+0);                          //It stores value stored in location 0

    for(i=0;i<*p;i++)                    //value of pointer being used
    {
       if(max<*(a+i))                   //comparison takes place
       {
         max=*(a+i);                    //assigning value
       }
    }
    printf("max value=%d",max);       //display of maximum value

}

program to count total number of words,space,consonants in a string using pointer

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

//program to reverse a string using pointer//comment of program
#include <stdio.h>                         //header file
#include<ctype.h>
int main()                              //main function
{
    char name[40];                     //character declaration
    char *p;                          //pointer declaration.
    int length,vowel=0,conso=0,space=0,words=1;
    puts("enter string\n");           //display of message
    gets(name);                      // gets data/string
    length=strlen(name);             //stores length of string
    p=name+length;        // assigning total address as in given string; it becomes total taken from variable length starting from 0.
                      //p=name takes address location 0 to pointer.
    while(p>=name)                 //the value of 'p' decreases from location 15 until it reaches 0.
    {
        if(*p==' ')                 //prints the value stored in particular location
        {
            space++;
        }

        else if(tolower(*p)=='a' ||tolower(*p)=='e' || tolower(*p)=='i' || tolower(*p)=='o' || tolower(*p)=='u')
        {
            vowel++;
        }
        else if(tolower(*p)!='a' ||tolower(*p)!='e' || tolower(*p)!='i' ||
                tolower(*p)!='o' || tolower(*p)!='u' ||
                *p!=' ' || *p!='.')
        {
            conso++;
        }
        p--;
    }
    printf("total vowel=%d,total consonants=%d,total spaces=%d,total words=%d",vowel,conso,space,space+1);
    return 0;
}

----------------------------------------------------------------------------------------------------------
using turbo c++
----------------------------------------------------------------------------------------------------
//program to reverse a string using pointer//comment of program
#include <stdio.h>                         //header file
#include<ctype.h>
#include<conio.h>
int main()                              //main function
{
    char name[40];                     //character declaration
    char *p;                          //pointer declaration.
    int length,vowel=0,conso=0,space=0,words=1;
    puts("enter string\n");           //display of message
    gets(name);                      // gets data/string
    length=strlen(name);             //stores length of string
    p=name+length;        // assigning total address as in given string; it becomes total taken from variable length starting from 0.
                      //p=name takes address location 0 to pointer.
    while(p>=name)                 //the value of 'p' decreases from location 15 until it reaches 0.
    {
        if(*p==' ')                 //prints the value stored in particular location
        {
            space++;
        }

        else if(tolower(*p)=='a' ||tolower(*p)=='e' || tolower(*p)=='i' || tolower(*p)=='o' || tolower(*p)=='u')
        {
            vowel++;
        }
        else if(tolower(*p)!='a' ||tolower(*p)!='e' || tolower(*p)!='i' ||
                tolower(*p)!='o' || tolower(*p)!='u' ||
                *p!=' ' || *p!='.')
        {
            conso++;
        }
        p--;
    }
    printf("total vowel=%d,total consonants=%d,total spaces=%d,total words=%d",vowel,conso,space,space+1);
getch();    
return 0;
}

program to understand pointer's pointer

using codeblocks
----------------------------------------------------------------------------------
//understanding pointer's pointer
#include <stdio.h>

int main()
{
    int **p,*p1;                  //two pointer declaration
    int x=5;
    p1=&x;                        //stores address of x to pointer p1
    printf("first address p1=%d\n",p);//prints that first address
    p=&p1;                         //stores address of pointer by pointer
    printf("address p=%d\n",p);//prints that pointer's address
    return 0;
}
--------------------------------------------------------------------------------

using turboc++
----------------------------------------------------------------------------

//understanding pointer's pointer
#include <stdio.h>
#include <conio.h>

int main()
{
    int **p,*p1;                  //two pointer declaration
    int x=5;
    p1=&x;                        //stores address of x to pointer p1
    printf("first address p1=%d\n",p);//prints that first address
    p=&p1;                         //stores address of pointer by pointer
    printf("address p=%d\n",p);//prints that pointer's address
   getch();
    return 0;
}


Program to find sum and average of 10 numbers using pointer.

using codeblocks
--------------------------------------------------------------------
//program to print sum and average of 10 numbers using pointer. //comment of program
#include <stdio.h>            //header file
int main()                   //main function
{
    int *p;                   //pointer declaration
    int a[20],i,k,sum=0;          //variable declaration
    int total;
    printf("enter total number\n");
    scanf("%d",&total);            // gets total value of array.
    p=a;                       //stores base address
    for(i=0;i<total;i++)      //runs from 0 to one less than total
    {
        scanf("%d",p+i);   //gets value for given address. This input goes upto last value of address
        sum=sum+*(p+i);   // finds sum
    }
    printf("the sum=%d\n",sum);          // prints sum
    printf("the average =%f\n",(float)sum/total);//prints average
    return 0;
}

--------------------------------------------------------------------------
using turbo c++
------------------------------------------------------------------------------------
//program to print sum and average of 10 numbers using pointer. //comment of program
#include <stdio.h>            //header file
#include<conio.h>
int main()                   //main function
{
    int *p;                   //pointer declaration
    int a[20],i,k,sum=0;          //variable declaration
    int total;
    printf("enter total number\n");
    scanf("%d",&total);            // gets total value of array.
    p=a;                       //stores base address
    for(i=0;i<total;i++)      //runs from 0 to one less than total
    {
        scanf("%d",p+i);   //gets value for given address. This input goes upto last value of address
        sum=sum+*(p+i);   // finds sum
    }
    printf("the sum=%d\n",sum);          // prints sum
    printf("the average =%f\n",(float)sum/total);//prints average
   getch();
    return 0;
}

---------------------------------------------------------------------------------------------------------
or
----------------------------------------------------
//program to print sum and average of 10 numbers using pointer. //comment of program
#include <stdio.h>            //header file
int main()                   //main function
{
    int a[20],i,sum=0;          //variable declaration
    int total;
    printf("enter total number\n");
    scanf("%d",&total);            // gets total value of array.
    for(i=0;i<total;i++)      //runs from 0 to one less than total
    {
        scanf("%d",a+i);   //gets value for given address , starting from 0. This input goes upto last value of address
        sum=sum+*(a+i);   // finds sum
    }
    printf("the sum=%d\n",sum);          // prints sum
    printf("the average =%f\n",(float)sum/total);//prints average
    return 0;
}