Thursday, February 4, 2016

Difference between structure and union



Both structure and union are user defined data types!
 Structure is a user defined data type where we can store many related different data types at a single place in memory!for example if we want to store the student in memory then we have to store roll number name and group and grade and so on .  This can be accomplished using struct data type.

struct variable name
{
  data type data member1;
  data type data member2;
  data type data member3;
-------
}variables;

example:
struct student
{
  int rollno;
 char name[20];
 char group[10];
 float grade;
}s1,s2,s3;  /* s1 s2 s3 are student1 student2 and student3*/

the student data type occupies 36 bytes[2+20+10+4];

The same we can do with union data type
union variable name
{
  data type data member1;
  data type data member2;
  data type data member3;
-------

}variables;

example:
union student
{
  int rollno;
 char name[20];
 char group[10];
 float grade;
}s1,s2,s3;  /* s1 s2 s3 are student1 student2 and student3*/

the only difference is using struct it takes 36 bytes of memory. but with union it takes only 20 bytes of memory. union data type takes and fixes the size to largest data type available in struct! in the above declaration  name data member takes 20 bytes.

















Difference between structure and union! Struct and union data type , size of struct and size of union  , user defined data types, enum data types, data types classification, arrays structures union boolean data types. program using struct and union student data base program using struct and union

Friday, January 29, 2016

Arrays in C

   /* arrays in c */

Before going to discuss about arrays lets go through some sample program

main()
{
  int a,b;
  printf("\n enter any two values");
  scanf("%d%d",&a,&b);
  printf("the given two values are a=%d\tb=%d",a,b);
}

output:
    enter any two values
     10 20
   the given two values are a=10   b=20

well this program good for reading two values
so what if we want to read 50 variables from keyboard ?
one solution is intialising 50 variables like a,b,c,d,,,,,,,, and reading varibales of
keyoard from scanf function.
But declaring 50 variables is looking complex

Arrays:
     main()
     {
        int a[10];
         a[0]=101;
         a[1]=102;
         a[2]=103;
         a[3]=104;
         a[4]=105;
         a[5]=106;
         a[6]=107;
         a[7]=108;
         a[8]=109;
         a[9]=110;
       printf("\n%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d",a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9]);
     }
 
     output:101   102 103 104 105 106 107 108 109


main()
{
   int a[10];/* a is an array which occupies (10*2)bytes of memory */
   int i,n;
   printf("enter your range");
   scanf("%d",&n);/* no of elements */
   printf("enter %d elements",n);
   for(i=0;i<10;i++)
   {
      scanf("%d",&a[i]);
    }
   printf("\n\nentered elements are");
   for(i=0;i<10;i++)
   {
      printf("\t%d",a[i]);
   }
}

output:
    enter your range
    10
    enter 10 elements
    1 2 3 4 5 6 7 8 9 10


    entered elements are 1 2 3 4 5 6 7 8 9 10
 
/* program to calculate the total marks and average marks for give six subjects*/

  main()
  {
    int a[6],i,total=0;
     float avg;
     printf("\n enter your six subject marks");
     for(i=0;i<6;i++)
      {
        scanf("%d",&a[i]);
      }
     for(i=0;i<6;i++)
     {
      total=total+a[i];
     }
     avg=total/6.0;
    printf("\n total marks is %d",total);
    printf("\average marks is %f",avg);
  }
 
output:
    enter your six subject marks
        90 90 90 80 100 90
       total marks is 540
       average marks is 90










arrays in c, derived data type array, array declaration, array initialization , sample program of arrays linear search using arrays binary search using arrays, calculating using total and average marks of a student,matrix multiplication
     


 








       

Pointers in C

Hi frnds! In this tutorial we are going to discuss about pointers.
   Simply a pointer is variable which holds the address of another variable.
   Then What is the differnce between the a normal variable and pointer variable?
   What happens when we declare the variable like

      int a=34;
       
  The above declaration tells the compiler that 'a' is a integer variable and reserves two bytes of memory for 'a'.
   Now the question is where the 'a' is in memory?
   
      well do u remember the scanf function?

    scanf("%d",&a);
   
in scanf fucntion there is a symbol '&' nothing but it represents the address of the variable 'a' in memory(for example 1024 address)
 
While using pointers we use * and &

 declaration of pointer variable:
      datatype * variable=value;
     here datatype is int float or char or even void data type
     
     for example int *a;
     here 'a' is a integer pointer i.e, it can store only integer address
     
    int *b;
    int c=10;
    b=&c;
     now b conatins the address of c.
   
    /*pointers in c*/
     
     main()
     {
      int *p;
      int q=20;/*pointer variable declaration*/
      p=&q;
      printf("\nvalue of q is %d ",q);
      printf("\nvalue of q is %d ",*(&q));
      printf("\nvalue of q is %d ",*p);
      printf("\nAddress of q is %u ",p);
      printf("\nAddress of q is %u ",&q);
     }
    output:
        value of q is 20
        value of q is 20
        value of q is 20
        address of q is 1190
        address of q is 1190


/*  swapping of two numbers using pointers*/

    void swap(int *,int *);
    void main()
    {
       int a=5,b=6;
       printf("before swapping a=%d\tb=%d",a,b);
      swap(&a,&b);
      printf("after swapping a=%d\tb=%d",a,b);
   }
    void swap(int *p,int *q)
   {
     int temp;
   temp=*p;
   *p=*q;
   *q=temp;
   printf("\m after swapping a=%d\tb=%d",a,b);
   }

  output:
   before swapping a=5      b=6
    after swapping a=6      b=5
 
     

Pointers in c, c programming using pointers, pointer declaration, pointer initialization void pointer integer pointer character pointer float pointer swapping of two numbers using pointers. Advantages of pointers. pointer arithmetic copy of one string to another string using pointers reverse of a string using pointers



   

Tuesday, January 12, 2016

FIle Handling in C

Good Morning friends! So today topic file handling in c! 

Before going to file lets execute the following c program
 
main()
{
   int a,b,c;
  clrscr();
  printf("enter any any two values");
  scanf("%d%d",&a,&b);
  c=a+b;
  printf("addition of %d and %d is %d",a,b,c);
 getch();
}

output: enter any two numbers
5
3
addition of 5 and 3 is 8
 
lets execute again!
enter any two values
13
12
addition of 13 and 12 is 25
every thing is ok ! but wat about previous input and output value?



so this is the problem while executing c program using  standard input output devices.

So if want to store input data and output data permanently then we have to go for files.


got it?

So lets move out attention to files.
So if we want to store input data and output data permanently then we have to go for files.


got it?

A file is a collection of data stored on a disk.
Simple to say file handling deals with creation of text documents, editing text, and deletion of text documents. 
For example consider creation of notepad documents, copy of one notepad document to another document etc
So to do operations on file we have some library function. 
fopen()-->to open a text file
getc()-->to read a single character from a file where the pointer is pointing
putc()-->to put a single character in a file where the pointer is pointing
getw()-->to read a single integer value from a file where the pointer is pointing
putw()---> to put a single integer in a file where the pointer is pointing
fprintf()-->to display different types of data on to the screen at a time
fscnaf()-->to read different types of data from the key board in a single function
fclose()--->to close the open file

Hope you  all understand wat am saying?

lets go in depth

fopen():
  
   fopen() function is used to open an existing file or to create the new file if not exist

   Note the every file program in c begins with:

   FILE *fp;
  where fp is a pointer pointing to the some location of the file.

 now we write fopen() syntax

fp=fopen("file name","mode");

example:
 main()
{
 FILE *fp;
fp=fopen("hello.txt","w");/*opening file */
-----
-------
------
fclose(fp);/*closing file pointer*/
}

seeing above example one more thing discuss
mode:

   a mode is nothing but it says what type of action to be taken on file
  r--->to open a file in read mode.(make sure the opened file must exist other wise error returns)