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)



 

Friday, September 26, 2014

LOOPS IN C

                                            DIFFERNT TYPES LOOPS IN C


  What is a loop?
     When we want to execute certain statements repeatedly, loops are used i.e., the statements which must be executed repeatedly must be placed inside loop(body section).

For example if we want to print "c programming" 10 times on screen?

    main()
{
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
    printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
  printf("\n c programing");
}

if we use the above strategy in printing name times it takes lot of time and burden to programmer.
In this situation we use loops. Basically there are three types of loops
i) for loop   ii) while loop   iii)do-while loop

Loops are categorized into two types.
a) entry control loop (while, for loop)    b) exit control loop(do-while loop)

a) Entry controlled loop: When using this loop the condition is tested first and if condition is true then body inside the loop will be executed.  If condition fails execution comes out from the loop and the statements immediately after the loop will be executed.

i) while loop:
    
     while(test expression)
        {
                       body of the loop;
         }


/* write a C program to print c programming on the screen */
 

  main()
{
     int i=1;
     while(i<=10)
     {
        printf("\n c programming");
        }
}


  output:  c programming
               c programming
               c programming
               c programming
               c programming
               c programming
               c programming
               c programming               
               c programming
               c programming

/* write a c program to print numbers from 1 to 10 using while loop */


  main()
    {
       int i;
        while(i<=10)
        {
           printf("\t %d",i);
         }
  }

output: 1     2     3     4     5     6     7     8    9   10






(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.


 

Tuesday, September 23, 2014

Conditional or ternary operator in C

/* Biggest of two numbers using i) if else statement and ii) ternary operator */

Before going to discuss about ternary operator lets check the below program

/* biggest of two numbers *?
main()
{
   int a,b;
   printf("\n enter any two numbers");
   scanf("%d%d",&a,&b);
   if(a>b)
   {
      printf("%d is big",a);
    }
   else
   {
     printf("%d is big",b);
   }
 }

output: enter any two numbers 10 20
20 is big


In the above program we assigend a value  to 10 and b value to 20
Here 'if' is called decision making statement i.e., based on the decision either 'if' part or 'else' part will be executed.
Here 'else' part will be executed because condition is false(0). So in output we see like "20 is big"
(Note: if condition is true(1) 'if' part will be executed)

Now the same thing can be performed using tenary(condional) operator.   (?  :)



/* biggest of two numbers */
main()
{
   int a,b,big;
   printf("\n enter any two numbers");
   scanf("%d%d",&a,&b);
 
   big=(a>b)?a:b;

   printf("%d is big",big);
}

output:   enter any two numbers 10 20
 20 is big

In the above program output is printed as "20 is big"

We can divide a ternary operator in c into three parts
first part conditiona part(here a>b?)
second part positive part(true part) here a   (equivalent to if part)
third part negative part(false part) here b     (equivalent to else part)
 In the above part a is assigned a value 10 and b is assigned a value 20.
and big=(a>b)?a:b;

In evaluating this expression the compiler starts from left and move to right.
first conditional part will be evaluated i.e., a>b?  ( here condition is false)
then compiler skips the second part amd moves to third part (since condition is false, the compiler moves to thrid part) i.e., big is assigned a value to b.
So output printed is "20 is big"



NOTE: if else statement can be replaced by ternay operator.




c programming, if statments, else if statements, nested if, elsed if ladder, arrays,strings, strucutres, unions, userdefiend data types, bit wise operators, ternary operator, conditional operator, file handling in c, fseek function, ftell fucntion, rewind function, logical operators , switch statements, is ternary statement is equlivalent to if else statements,biggest of two numbers, poitner in c, switch statement in c, decision making in c, control strcutures in c. biggest of three numbers

Saturday, September 13, 2014

printing plain strings with the help of back slash characters


how to print something on screen using C printf() function?

consider a simple code given below-

main()  /* program begins here */
{
  printf("hello this is my first program");
}

if we execute this code we get output on screen as

output : 
hello this is my first program

In c if we want to print something on screen we have to use printf() function which is available in standard library function stdio.h

whatever we give in printf() function in between two inverted comas that is printed same(except some special symbols).

main()
{
  printf("hai Programmers");
}

output: 
hai Programmers


remember that every printf() function in C must be terminated by a semicolon symbol(;).

Now my task is i want to give a tab space in between two fields hi and programmers

main()
{
   printf("hi\tprogrammers");/
}

output:
hi    programmers.

\t has a special meaning in c which gives one tab space when it is encountered by a C compiler 


now my task is i want to print hi in one line and programmers in second line

main()
{
  printf("hi");
printf("\n programmers");
}
output:
  hi
  programmers

\n has a special meaning in c which moves cursor to next line and prints next word or line in next line  when it is encountered by a C compiler 




you may get doubt why i cant print two words in two lines using two printf() statements without using \n symbol. let me explain what happens
main()
{
 printf("hi");
printf("programmers");
}

output:
hiprogrammers

what happens here is after printing first word hi then cursor is at location immediately after hi. so when second printf() function is executed then second word programmers printed immediately after hi.

note:   \t is used for allocating one tab space between two words.
           \n is used to move the cursor next line and prints other lines.
           \a is used to give a beep sound  
            let us compare two things for example in word document and C editor
            

                         action                                   command or key                                  command or key

                                                                      (MSWORD)                                            (C output )
                         to move to new line              press enter key                                             \n
                        to give one tab space             press Tab key                                               \t


lets close our session with this program

main()
{
  printf("hi");
  printf("\n hello\t programmers");
}

output

hi
hello    programmers







Any queries or comments?

(c programming, c programming variable intialisation, datatypes in c, integer data types in c, float data types in c,int,float,char,data range, decision making, looping, entry control looops, exit control loops, for,while,do while , if else , nested if , switch, functions, recursive functions, nested loops, tower of hanoi, pointers, strcutres , unions , enum, files in c, binar files, random access in files, fseek,ftell, rewind, copy of one file to other file, merging, continue, break, goto, realtional operators, bitwise operators, logical operators, shift operators procedural languages.
back slash characters,printf statement.