Thursday, July 26, 2012

C Program to Calculate Square Root Of a Number

#include<stdio.h>
#include<conio.h>
float sqroot(float );
void main()
{
    float n,a;
    scanf("%f",&n)
    a=sqroot(n);
    getch();
}
    float sqroot(float m)
{
      float i=0;
    float x1,x2;
    while( (i*i) <= m )
             i+=0.1;
    x1=i;
    for(int j=0;j<10;j++)
    {
          x2=m;
        x2/=x1;
        x2+=x1;
        x2/=2;
        x1=x2;
    }
    return x2;
}

Wednesday, July 25, 2012

C Password Program


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
int d;
         d=42;  // ascii OF *
char a[5],b[5];
printf(" Enter the 5 letter PASSWORD\n ");
printf(" pASSWORD ::: ");
for(i=0;i<5;i++)
  { a[i]=getch();
b[i]=d;
printf("%c",b[i]);
  }


  if(a[0]=='L' && a[1]=='O' && a[2]=='V' && a[3]=='E' && a[4]=='U' )
printf(" \n <<<<<<<...........Welcome...........>>>>>>>> ");
else
printf(" \n !.........Sorry!Incorrect password.........! ");




}






:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


for login......
Password : LOVEU

Tuesday, March 13, 2012

Double Ended Queue ( DEQUE)





It is a homogeneous list of elements in which insertion and deletion operations are performed from both the ends i.e, we can insert elements from the rear end or from the front ends. Hence, it is called double-ended queue. It is commonly referred to as deque.

There are two types of deques. These two types are due to the restrictions put to preform either the insertions or deletions only at the end. They are :

1) Input Restricted Deque 
2) Output Restricted Deque

Four Operations :

=> Insertion of an element at the REAR end of the queue
=> Deletion of an element from the FRONT end of the queue
=> Insertion of an element at the FRONT end of the queue
=> Deletion of an element from the REAR end of the queue


For an input-restricted deque only the operations 1,2,3 & 4 are valid. And for an output-restricated deque only the operations specified in 1,2,3 are valid.

a) Insertion of an element at the REAR end of the queue

void dqinsert_rear(int q[10],int front,int rear,int item,int MAXSIZE)
{
   if(rear == (MAXSIZE-1))
   {
      printf(" QUEUE is full ");
      return;
   }
  rear=rear+1;
  q[rear]=item;
}


b) Deletions of an element from the FRONT end of the queue

void dqdelete_front(int q[10],int front,int rear,int item)
{
    if(front == rear)
    {
         printf(" QUEUE is empty ");
         return;
    }

    front=front+1;
    item=q[item];
}



3) Insertion of an element at the FRONT end of the queue

void dqinsert_front(int q[10], int front, int rear, int item)
{
     if(front == 0)
     {
            printf(" QUEUE is full");
            return;
     }
     front=front-1;
     q[front]=item;

}



d) Deletion of an element from the REAR end of the queue


void dqdelete_rear( int q[10], int front, int rear, int item)
{
   if(front == rear)
   {
       printf(" QUEUE is empty ");
       return;
   }

   rear=rear-1;
   item=q[item];

}




e) Function to display the contents (status) of a QUEUE

void dq_display(int q[10], int front, int rear)
{
    if(front <= rear)
    {
         printf(" Status of the Queue \n");
         for(i = front; i<=rear; i++)
         printf("%d",dq[i]);
    }
    else
        printf(" QUEUE is empty ");

}

Thursday, March 8, 2012

Important Questions On Linked List :::

1) What pointer type is used to implement the heterogeneous linked list in C ?
Ans :

The answer is the void pointer.
The heterogeneous linked list contains different data types in its nodes and we need a link, pointer to connect them. Since we can't use ordinary pointers for this, we use the void pointer. Void Pointer is a generic pointer type, and capable of storing pointer to any type.





2) Can we do a Binary Search on a LInked List ?
Ans :

We can get to the middle of the array just by saying array[middle].
Now, we cannot do the same with a linked list. We will have to write your own, possible inefficient algorithm to get the value of the middle node of a linked list. In a linked list, you lose the ability to get the value of any node in a constant time.

One solution to the inefficiency of getting the middle of the linked list during a binary search is to have the first node contain one additional pointer that points to he node in the middle. Decide at the first node if you need to check the first or the second half of the linked list. Continue doing that with each half list.




3) Whether Linked List is linear or Non Linear data structure ?

Ans :


According to Access strategies Linked LIst is a linear one.
According to Storage Linked List is a Non Linear One.






4) How can we search for data in a Linked List ?

Ans :

The only way to search a linked list is with a linear search, because the only way a linked list's members cab be accessed is sequentially. Sometimes it is quicker to take the data from a linked list and store it in a different data structure so that searches can be more efficient.






5) What member function places a new node at the end of the Linked List ?

Ans:


The appendNode() member function places a new node at the end of the linked list. The appendNode() requires an integer representing the current data of the node.







6) What does the prototype of calloc() look like ?

Ans :  

void *calloc(size_t num, size_t size);




7) What is the lenth of the null() list ?

Ans :    1
 


Tuesday, February 21, 2012

What is the purpose of farmalloc() and farfree() ?

Ans :-


farmalloc() allocates a block of memory from the far heap. Using this function , blocks larger than 64K can also be allocated. Far pointers are used to access the allocated blocks.

farfree() releases a block of memory previously allocated from the far heap.

What would be the equivalent pointer expression for referring the array element a[i][j][k][l] ?

Ans :


a[i][j][k][l] can be represented as   *(*(*(*(a+i)+j)+k)+l)

What is the difference between *ptr++ and ++*ptr ?

Ans :--


*ptr++ increments the pointer and not the value pointed by it

++*ptr increments the value being pointed to by ptr

Friday, February 17, 2012

Predict the output Of the following Code snippet:

#include<stdio.h>

 void main()
{
    display();
}

void display()
{
     printf("\n Cliffhanger");
}

Ans:  Redeclaration error


Reason ::::
Here display() is called before it is defined. In such cases the compiler assumes that the function display() is declared as int display(); That is , an undeclared function is assumed to return an int and accept an unspecified number of arguments. Then when we define the function display() the compiler finds that it is returning void hence the compiler reports the discrepancy.

Wednesday, February 15, 2012

What are LG and LGP in the following program ?

#include<stdio.h>
void main()
{
     typedef long LG,*LGP;
     extern LGP lptr;
}


Ans :  

Here, the first statement declares LG as a typedef for long and LGP as a pointer to a long. 
The second statement declares lptr of type LGP with storage class as extern.

Predict the Outputs Of the following codes :

1) 
#include<stdio.h>
void main()
{
  int i;
  for(;scanf("%d",&i);printf("%d",i))
  ;
}


O/p: the for loop will be executed infinite number of times.\
eg:    
     8
     8

     10
     10



2)

#include<stdio.h>
void main()
{
    extern int i;
    i=20;
    printf("%d",sizeof(i));
}


// O/p: error.  because extern int i is a declaration and not a definition.




3)
#include<stdio.h>
void main()
{
     extern int a;
     printf("%d",a);
}


int a=20;

//Output :: 20


4)
#include<stdio.h>
void main()
{
    char *s1;
    char far *s2;
    char near *s3;
    char huge *s4;
    printf("%d %d %d %d", sizeof(s1),sizeof(s2),sizeof(s3),sizeof(s4));
}

// Output :  4 4 2 4




 

Thursday, February 9, 2012

C Program to count the number of ones in a 32 bit number.

#include<stdio.h>

int bitcount(unsigned int n)
{
  int count=0;
  while(n)
  {
      count+=n& 0x1u;
      n>>=1;
  }
  return count;
}

void main()
{
     unsigned int g;   int n;
    printf(" \n Enter the number : \n");
    scanf("%u",&g);
    n=bitcount(g);
  printf("It has %d ones",n);
 }

eg:

Enter the number:
7

It has 3 ones

C Program without using a semicolon

#include<stdio.h>

void main()

{
   if( printf(" Shahrukh Khan \n")
   {}
}

C Program to find factorial using recursion with no new function defined

#include<stdio.h>
void main()
{
    static int var=5;
    static int result=1;
    result*=var;
    printf("\n %d",var--);
    printf("\n Result is %d",result);

    if(var)
    {
        main();
    }
    return;
}

What are volatile variables ?

The volatile keyword acts as a DATA TYPE QUALIFIER . It alters the default way in which the compiler handles the variable and does not attempt to optimize the storage referenced by it.

volatile means the sorage is likely to change anytime by code outside the control of the user program. This means that if you reference a variable, the rogram should always read from the physical address and not its cached value.

A volatile keyword is an instruction to the optimizer to make sure that the variable or function is not optimized during compilation.

eg :
         
...
...
int flag=1;
while(flag);
...
...

Here , on compilation the compiler assumes that the value of the flag won't be changed during  the execution of the program. So the compiler is free to ignore the while(flag) loop instructions during optimization. However, if the flag variable is changed outside this program control ( say by an interrrupt routine or by some other thread) then it is advisable to declare the flag as a volatile variable.


Tuesday, February 7, 2012

What are STATIC variables ??

The storage class, static , has a lifetime lasting the entire program. static storage class can be specified for automatic (local) as well as global variables.

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus , the value of a static variable in a function is retained between repeated function calls to the same function. 

Static variables are allocated on the heap. The scope of static automatic variables is identical to that of automatic (local) variables,i.e. it is local to the block in which it is defined; however the storage allocated becomes permanent for the duration of the program.

Static variables may be initialized in their declarartions ; however the initializers must be constant expressions , and initialization is done only once at compile time when memory is allocated for the static variable.

The scope of a static global variable is only within the file which it is declared. A user cannot use extern in a different file and access the static global variable.

Sunday, February 5, 2012

How do you access command-line arguments ?

A Program is started by the operating system calling a program's main() function .
main() function is the only function in C that can be defined in multiple ways. It can take no arguments , two arguments or three arguments 

The two and three argument forms allow it to receive arguments from the shell ( command-line). The two argument form takes an int and an array of strings. When defining main() function arguments any name can be given but it is convention to call them argc and argv[].

The first argument (argc) holds a count of how many elements there are in the array of strings passed as the second argument(argv). The array is always null terminated so argv[argc]=NULL.

e.g :
          
int main( int argc, char *argv[])
{
         int i;
         for(i=0;i<argc;i++)
         printf("argv[%d] == %d\n",i,argv[i]);
         return 0;
}


The integer,argc, is the argument count(hence argc). It is the number of arguments passed into the program from the command line, including the name of the program.
The array of character pointers is the listing of all the arguments. argv[0] is the name of the program. After that , every element number less than argc is command line arguments. You can use each argv element just like a string , or use argv as a two dimensional aray.

               


What are the #pragma statements ?

Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for instance , need to exercise precise control over the memory areas where data may be stored or to control the way certain functions receive parameters.

The #pragma directives offer away for each compiler to offer machine and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine or operating system-specific by definition, and are usually different for every compiler.

Pragmas can be used in conditional ststements , to provide new preprocessor functionality, or to provide implementation-defined information to the compiler.

The `#pragma' directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself. Three forms of this directive (commonly known as pragmas) are specified by the 1999 C standard. A C compiler is free to attach any meaning it likes to other pragmas.

What is the meaning of #include ?

If a line starts with a hash, denoted by #, kit tells the compiler that a command should be sent to the C PREPROCESSOR. The C preprocessor is a program that is run before compilation takes place ( hence the name). 

Basically, when the preprocessor finds #include it looks for the file specified and replaces #include with the contents of that file. This makes the code more readable and easier to maintain if you needed to use common library functions.


Header files have the extension .h and the full filename follows from the #include directive . They contain functions that you may or may not have used in your program.

for e.g, the stdio.h file is required if you have used functions like printf() and scanf() in your program.

What is the difference between #include <...> and #include "..."

There are two ways to include a header file :

#include "stdio.h"
#include<stdio.h>

If you use the double quote marks , it  means that the directory you're currently in will be searched first , for the header file , before any other directories( mentioned in the INCLUDE_PATH ) are searched.

When you use the angled brackets , directories other than the one you're currently in , will be searched for the header file. The system dependent directories are searched. Usually this will be the default directory for header files specified in your compiler, so you'll probably be using square brackets all the time.

Sunday, January 1, 2012

What IsDangling Pointer ?

DAnging Pointer :::::::
If any pointer is pointing the memory address of any variable , but after sometime the variable is deleted  while the pointer is still pointing such memory location. Such pointer is known as dangling pointer .



  









e.g :
  

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

int x=25;
++x;

return &x;
}


Output :::::
 Garbage Value

variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.