Showing posts with label C Language Tutorials. Show all posts
Showing posts with label C Language Tutorials. Show all posts

Saturday, 31 January 2015

C language Tutorials-Variables and Data Types



The Data what be store can be classify as follows.


--> Variable is just a memory location which is capable of storing some data.

--> Datatype represent characteristic properties of a variable i.e the type of value to store and the amount of memory required.



--> So, the datatype provides a "C" language are as follows.
1) char (1 byte)
2) int (2 bytes)
3) float (4 bytes)
4) double (8 bytes)

--> The Keywords short, long, signed, unsigned are referred as Type Qualifiers. Because they specify/qualify the properties of datatype.

--> The Keywords short, long are in general referred as Size Qualifiers.

--> The Keywords signed, unsigned are referred as Sign Qualifiers.

--> When we don't specify any qualifiers, then by default it is considered as short and signed.

signed char ( 1 byte ):


     1   1   1   1   1   1   1 

        MSB <---- 7     6        5       4       3       2      1      0  ---->LSB

Range:  -128  to  +127

unsigned char( 1 byte ):

Range: 0 to 255

--> When we have n bits, the range of signed type will be -2n-1 to +2n-1-1 and for unsigned type will be 0 to 2n-1

Example:-

signed char ---> 8 bits ---> -27 to +27-1
                                    ---> -128 to +127

unsigned char ----> 8 bits ---> 0 to 28-1
                                                 0 to 255

short signed int ----> 2 bytes ---> 16 bits ---> -215 to 215-1
                                                                         -32768 to 32767

short unsigned int ----> 2 bytes ---> 16 bits ---> 0 to 216-1
                                                                             0 to 65535

long signed int ----> 4 bytes ---> 32 bits ---> -231 to 231-1
                                                                        -2147483648 to 2147483647

long unsigned int ---> 4 bytes ---> 32 bits ---> 0 to 232 -1
                                                                           0 to 4294967296



float ( 4 bytes):

            Here the data will be stored in Mantissa and Exponent format.

--> Here we can store a maximum of 6 digits only in the functional part.

--> Similarly double type required 8 bytes.

--> Memory will not be allocated for a datatype.

--> Memory will allocated only for a variable.

--> So, we can't stored data in a datatype, but we can store data only in a variable

--> A Variable can't be access unless we declare it.

--> The syntax for declaring a variable is as follows.

              datatype <variable name> [= value]

--> When we declare a variable, memory will be allocated for the corresponding number of bytes and initially it contains some dummy value and is referred as garbage value.

Example:- Declaration:
                     
---> We can also specify the value to be placed in a variable at the time of declaration itself and it is referred as initialization.

Example:- Initialization:

---> The value contained in a  variable can be changed/modified as required the assigning the necessary value and it is referred as assignment

Example:-  Assignment:

---> When we are required to display a direct value or the value contained in a variable or the result contained in a variable or the result of an expression, we should specify the type specifiers in  the format.

---> Some of the type specifiers are as follows.


%c                          --------->  Character

%i (or) %d             --------->  integer

%f                          --------->  float

%g                         --------->   double

%li (or) %ld           --------->  long integer

%lf                         --------->  long float

%lg                        --------->  long double 

%u                         --------->  unsigned


---> The symbol % represents unknown value or blank, which will be filled later

---> The symbol next do % specify the type of value to be filled in that blank

---> The value to be filled in that blank should be specified after the format.

Example:-

               #include<stdio.h>
               #include<conio.h>
               void main( )
               {
                        int x;
                        x = 56;
                        printf("%i", x);
                }

Output:-

          56       


---> The output is to be displayed in the most possible stylish way.







           

Tuesday, 6 January 2015

C Language Programs to Print The Patterns in C language, Pattern Programs in C


C language programs for the following pattern



Pattern1:

          1  1  1  1
          2  2  2  2
          3  3  3  3
          4  4  4  4

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
printf("%d ",i);
}
}
}




Pattern2:

           1  2  3  4
           1  2  3  4
           1  2  3  4
           1  2  3  4

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
printf("%d ",j);
}
}
}




Pattern3:

          1  0  0  0  0
          0  1  0  0  0
          0  0  1  0  0
          0  0  0  1  0
          0  0  0  0  1

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
if(i==j)
{
printf("1 ");
}
else
{
printf("0 ");
}
}
}
}




Pattern4:

            1
            0  1
            0  0  1
            0  0  0  1
            0  0  0  0  1

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
if(i==j)
{
printf("1 ");
}
else if(i>j)
{
printf("0 ");
}
else
{
printf("  ");
}
}
}
}




Pattern5:

              1  0  0  0  0
                  1  0  0  0
                      1  0  0
                          1  0
                              1

Program:

#include<stdio.h>
#include<conio.h>
void pattern(int);
void main()
{
      int n;
      clrscr(); //clear the screen contents
      printf("\nEnter N value: ");
      scanf("%d",&n);
      pattern(n);
      getch();
}
void pattern(int n)
{
int i,j;
for(i=1;i<=n;i++)
{ printf("\n");
for(j=1;j<=n;j++)
{
if(i==j)
{
printf("1 ");
}
else if(i<j)
{
printf("0 ");
}
else
{
printf("  ");
}
}
}
}






Thanks for visiting............

Saturday, 6 December 2014

C Program for Heap Sort Algorithm

Heap Sort:

         The Heap Sort Algorithm is an improved version  of the selection sort. The heap sort selects an element from the unsorted portion of the list, but it is the largest element. Because heap is a tree structure. A Heap is tree structure in which the root contains the largest element in the tree.

Program:

#include<stdio.h>
#include<conio.h>
void arrange(int *, int);
void hs(int *, int, int);
int main()
{
             int a[20],i,j,n,tmp,k;
             clrscr( );
             printf("\nEnter Number of elements:");
             scanf("%d",&n);
             printf("\nEnter %d of elements:",i);
            for(i=0; i<n; i++)
            {
                   scanf("%d",&a[i]);
                   arrange(a,i);
            }
            j=n;
            for(i=0; i<j; i++)
           {
                   tmp=a[0];
                  a[0]=a[n];
                  a[n]=tmp;
                  n--;
                  hs(a,0,n);
           }
           printf("\nSorting list in ascending order:");
           n=j;
           for(i=0; i<n; i++)
          {
               printf("\n%d ",a[i]);
          }
           getch( );
}
void arrange(int *a, int i)
{
          int tmp;
          tmp=a[i];
          while((i>0)&&(a[i/2]<tmp))
         {
                  a[i]=a[i/2];
                  i=i/2;
          }
          a[i]=tmp;
}
void hs(int *a, int i, int n)
{
          int tmp,j;
          tmp=a[i];
          j=i*2;
          while(j<=n)
         {
               if((j<n)&&(a[j]<a[j+1]))
               {
                        j++;
               }
               if(a[j]<a[j/2])
               {
                       break;
               }
               a[j/2]=a[j];
               j=j*2;
          }
          a[j/2]=tmp;
}

Output:

Enter Number of elements:5

Enter 5 of elements:4
1
20
5
9

Sorting list in ascending order:
1
4
5
9
20








C Program for Selection Sort Algorithm

Selection Sort:

             A Selection sort algorithm is one of Internal Sorting technique. It sorts the list by selecting the largest element in the unsorted list and places it at the appropriate position.In Each position, the largest element in the list is placed at appropriate position. If there are 'n' elements in the list, we require (n-1) passes.


Program:

#include <stdio.h>
#include<conio.h>
int main()
{
            int a[100], n, i, j, k,temp;
            clrscr();
            printf("\nEnter Number of elements:");
            scanf("%d", &n);
            printf("\nEnter %d of Elements:", n);
            for( i = 0 ; i < n ; i++ )
           {
                 scanf("%d", &a[i]);
           }
           for( i = 0 ; i < n - 1; i++ )
          {
                 k = i;
                 for ( j = i + 1 ; j < n ; j++ )
                {
                 if ( a[k] > a[j] )
                 k = j;
                 }
                if ( k != i )
                {
                 temp = a[i];
                 a[i] = a[k];
                 a[k] = temp;
                 }
           }
           printf("\nSorted list in ascending order:");
           for ( i = 0 ; i < n ; i++ )
           {
                    printf("\n%d", a[i]);
           }
           getch( );
}


Output:

Enter Number of Elements:4

Enter 4 of Elements:0
3
2
10

Sorted list in ascending order:
0
2
3
10




C Program for Insertion Sort Algorithm

Insertion Sort:

                  Insertion Sort is a Technique which inserts an element at an appropriate location by comparing each element with the corresponding elements at its left and moves the rest of the elements of the given array.

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
int a[20],i,n,j,temp;
printf("\nEnter N Value:");
scanf("%d",&n);
printf("\nEnter Elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n;i++)
{
j=i;
while(j>0&&a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
j--;
}
}
printf("\nSorted Elements are:");
for(i=0;i<n;i++)
{
printf("\n%d",a[i]);
}
}

Output:

Enter N Value:5

Enter Elements:9
3
6
10
20

Sorted Elements are:
3
6
9
10
20











Friday, 5 December 2014

C Program for Linear Search Algorithm

Linear Search:

                    The linear search method is mainly applicable for searching elements with an unordered list.  In the linear Search method each element of the list is compared with the key in a sequential order. Linear Search is also called as Sequential Search.

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,val,a[20],j=0;
clrscr();
printf("\nEnter the Number of Elements:");
scanf("%d",&n);
printf("\nEnter %d of elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the Search element:");
scanf("%d",&val);
for(i=0;i<n;i++)
{
if(a[i]==val)
{
printf("\nElement Found");
}
else
{
j=j+1;
}
}
if(i==j)
{
printf("\nElement not found");
}
getch();

}

Output:

Enter the Number of Elements:4

Enter 4 of elements:6
2
1
9

Enter the Search element:10

Element not found



C Program for Binary Search Algorithm

Program:

#include<stdio.h>
#include<conio.h>
void main( )
{
    int a[20],val,i,n,low,high,mid;
    clrscr( );
    printf("\nEnter the Number of Elements:");
    scanf("%d",&n);
    printf("\nEnter the %d of elements:",n);
    for(i=0;i<n;i++)
    {
    scanf("%d",&a[i]);
    }
    printf("\nThe Elements in the array are :");
    for(i=0;i<n;i++)
    {
    printf("\n%d",a[i]);
    }
    low=0;
    high=n-1;
    printf("\nEnter the Search Element:");
    scanf("%d",&val);
    while(low<=high)
   {
  mid=(low+high)/2;
  if(a[mid]==val)
  {
   printf("\nElement found");
   break;
  }
  else if(a[mid]>val)
  {
   low=mid-1;
  }
  else if(a[mid]<val)
  {
   low=mid+1;
  }
  else
  {
   printf("\nElement not found");
  }
    }
    if(low>high)
    {
    printf("\nElement not found");
    }
}


Output:

Enter the Number of Elements:5

Enter the 5 of elements:1
2
3
4
5

The Elements in the array are :
1
2
3
4
5

Enter the Search Element:5

Element found




C Program for Binary Search using Functions

Binary Search:

Binary Search Technique is implemented on sorted list of elements. It is faster than linear search, hence this method is efficient when the number of elements are large.

Program:

#include<stdio.h>
#include<conio.h>
int binarysearch(in[],int,int,int);
void main( )
{
             int a[20],val,i,n,low,high,bsf;
             clrscr( );
             printf("\nEnter the Number of Elements:");
             scanf("%d",&n);
             printf("\nEnter the %d of elements:",n);
             for(i=0;i<n;i++)
             {
                     scanf("%d",&a[i]);
             }
             printf("\nThe Elements in the array are :");
             for(i=0;i<n;i++)
             {
                     printf("\n%d",a[i]);
             }
             low=0;
             high=n-1;
             printf("\nEnter the Search Element:");
             scanf("%d",&val);
             bsf=binarysearch(a,val,low,high);
             if(bsf == -1)
             {
                        printf("\nElement is found");
             }
             else
             {
                        printf("\nElement not found");
             }
             getch( );
}
int binarysearch(int a[ ], int val, int low, int high)
{
            int mid;
            while(low<=high)
            {
                   mid=(low+high)/2;
                   if(a[mid]==val)
                   {
                            return mid;
                   }
                   else if(a[mid]>val)
                   {
                            low=mid-1;
                   }
                   else if(a[mid]<val)
                   {
                            low=mid+1;
                   }
                   else
                   {
                            return -1;
                   }
             }
             return -1;
}

Output:


Enter the Number of Elements:5

Enter the 5 of elements:
1
2
3
4
5

The Elements in the array are :
1
2
3
4
5

Enter the Search Element:10

Element not found
             




Saturday, 6 September 2014

C language Tutorials-1



--> 1 Byte  =  8 bits

--> 1 Kilo Byte  =  2^10 Bytes = 1024 bytes

--> 1 Mega byte = 2^10 KB = (2^10)*(2^10) bytes = 2^20 bytes

--> 1 Giga Byte = 2^10 MB = (2^10)*(2^10)*(2^10) bytes = 2^30 bytes

--> A variable just storage a location which is capable of storage some table.

-->The rules for naming a variable are else following.

--> The Variable name should start with
         1) Alphabets and it can be followed by any number of  alphabets or digits without any other                     special symbols except "___"(underscore).
        2) The Variable names should not match with the keywords
        3) Variable names should be unique i.e , no two variables should have same name within a program.
--> There are some words provided by "C" language which has a free defined meaning and are referred as key words (or) reserved words
-->  There are 32 Keywords in C language. The keywords provided by "C" languages are as follows

             Auto, break, case, char, const, continue, do, default, double, else, enum, extern, far, for, float, goto, int, if, long, near, return, register, struct, static, short, signed, switch, typedef, union, unsigned, void, while


--> "C" language is a case-sensitive language i.e, it consider both upper case and lower case symbols are different.

--> A program is set of sequence of instruction which perform a free defined task.

--> Each and Every statement should be terminated with a semicolon(;).

--> A "C" Program is a combination of functions with a special function called main()  from which they execution start
        ( ) --> Parenthesis
       { } --> Braces
        [ ] --> Sub-script
   
For Example:
                      main( )
                     {
                             f(3);
                             f(2);
                             f(1);
                      }
                      f1(arg list)
                      {
                               statements;
                      }
                      f2(arg list)
                      {
                                statements;
                      }
                      f3(arg list)
                      {
                                statements
                      }



Tuesday, 19 August 2014

Introduction to C Language



Introduction:

                          A language represents set of symbols along with the rules to be followed while using these symbols.
                                                Language = Symbols + Rules

--> The rules are referred as "SYNTAX".
-->A Program represents set of sequence of instructions (or) commands (or) Statements which perform a pre-defined task.
--> A computer can understand only 0's(zero) and 1's(one). So, any symbol what we store in a computer should be converted ti 0's and 1's.
--> But it is not possible to convert a symbol directly to 0's and 1's. So, each and every symbol is given a unique identification, which is a number and is referred as "ASCII"(American Standard Code for Information Interchange) value.
--> All together we have 256 Symbols and so, 256 ASCII values ranging from 0,1,2,-------255.

ASCII Values for A-Z:

A - 65
B - 66
C - 67
D - 68
E - 69
F - 70
G - 71
H - 72
I - 73
J - 74
K - 75
L - 76
M - 77
N - 78
O - 79
P - 80
Q - 81
R - 82
S - 83
T - 84
U - 85
V - 86
W - 87
X - 88
Y - 89
Z - 90

ASCII values for a-z:

a - 97
b - 98
c - 99
d - 100
e - 101
f - 102
g - 103
h - 104
i - 105
j - 106
k - 107
l - 108
m - 109
n - 110
o - 111
p - 112
q - 113
r - 114
s - 115
t - 116
u - 117
v - 118
w - 119
x - 120
y - 121
z - 122

ASCII for for 0-9:

0 - 48
1 - 49
2 - 50
3 - 51
4 - 52
5 - 53
6 - 54
7 - 55
8 - 56
9 - 57