Thursday, 31 January 2013

File Programming in C: PART - 2

Consider the Program given below. The program prints the values of member variables in the FILE structure of UNIX.

#include<stdio.h>

int main()
{
     FILE * fp;
     fp=fopen("File2.C","r");

     printf("\nThe File Structure Values are:\n");
     printf("\n_ptr = %d",fp->_ptr);
     printf("\n_cnt = %d",fp->_cnt);
     printf("\n_base = %d",fp->_base);
     printf("\n_flag = %d",fp->_flag);
     printf("\n_file = %d",fp->_file);

     fclose(fp);
}


The program prints the values of the member variables in the FILE structure.

_ptr :-   Next Character in Buffer.
_cnt :-   Number of available characters in Buffer.
_base :- The Buffer.
_flag :-  The state of the stream.
_file :-   Unix system file descriptor.

  
Click here to download the C Program to print the FILE Structure member variables.

Wednesday, 30 January 2013

Dijkstra's Algorithm: C/C++ Program

Dijkstra's algorithm is used to solve the Single Source Shortest Path problem in Graphs. The Algorithm's time complexity is O(n2) for a graph with n nodes. Dijkstra's algorithm works only for a graph with non-negative edge path costs. 


Tuesday, 29 January 2013

C/C++ Program to Perform Binary Search Tree Operations

Binary Search Tree is a very useful Data Structure. In this Program the following operations are implemented.
  • Insertion.
  • Deletion.
  • Inorder Traversal.
  • Preorder Traversal.
  • Postorder Traversal.
  • Finding the Root Node.
Figure Below Shows a Sample Output

Click here to download the C Program performing Binary Search Tree Operations.
Click here to download the C++ Program performing Binary Search Tree Operations.

Implementing MINUS Query in MySQL

  The SQL MINUS query returns all rows in the first SQL SELECT statement that are not returned in the second SQL SELECT statement. Each SQL SELECT statement within the SQL MINUS query must have the same number of fields in the result sets with similar data types.

The syntax for the SQL MINUS query is:

select field1, field2, ... field_n 
from tables1 
MINUS 
select field1, field2, ... field_n 
from table2; 

But this query is not implemented in MySQL. So to achieve the same effect you can use the following Query statement.  

select field1, field2, ..., field_n 
from table1 
where (field_x) NOT IN (select field_x from table2); 

Where field_x is the field over which the comparison is performed. 

 

Implementing INTERSECT Query in MySQL

  The SQL INTERSECT query returns the results of 2 or more "select" queries. However, it only returns the rows selected by all queries. If a record exists in one query and not in the other, it will be omitted from the INTERSECT results. 

The syntax for the SQL INTERSECT query is:

select field1, field2, ..., field_n 
from table1 
INTERSECT 
select field1, field2, ..., field_n
from table2
 
But this query is not implemented in MySQL. So to achieve the same effect you can use the following Query statement.  

select field1, field2, ..., field_n 
from table1 
where (field_x) IN (select field_x from table2);

Where field_x is the field over which the comparison is performed. 

Saturday, 26 January 2013

Library Call VS. System Call

  The difference between library calls and system calls has confused even expert programmers. Library calls are part of the language or application, and system calls are part of the operating system. A system call gets into the kernel by issuing a "trap" or interrupt. The other main differences between library calls and system calls are listed below.
 
  • The C library is the same on every ANSI C implementation, but the Systems Calls are different in each Operating System.
  • Library Call is a call to a routine in a library, Whereas System Call is a call to the kernel for a service.
  • Library call is linked with the user program. System Call is an entry point to the OS.
  • Library call executed in the user address space, but System Call is Executed in the kernel address space.
  • Library Call execution time is Counted as part of the "user" time, whereas System Call execution time is counted as part of the "system" time.
  • Library Call has the lower overhead of a procedure call, whereas System Call has a high overhead context switch to kernel and back.
  • There are about 300 routines in the C library "libc". There are about 90 system calls in UNIX. WinAPI provides similar System Calls in Windows.
  • Typical C library calls are system, fprintf, malloc, etc. Typical system calls are chdir, fork, write, brk, etc.

Thursday, 24 January 2013

Functions setjmp( ) and longjmp( ) in C

  C language provide only limited branching facility. Most of the constructs in C provide conditional jumps only. Of course there is goto, but goto is not very powerful. Consider the program given below.

#include <stdio.h>

void fun()
{
    printf("\nYou are in the function fun\n");
    goto x;
    printf("\nyou will not see this, because of goto\n");
}
int main()
{
    printf("\nYou are in the main function\n");
    fun();
    printf("\nThis line will not be printed\n");
    x:
    printf("\nThis line will be printed\n");
    return 0;
}


The program will give you the following error "label 'x' used but not defined". The label of a goto statement is only having a function scope. Because of this limitation you can not jump out side a function using goto statement. But don't worry, we can use setjmp( ) and longjmp( ) to jump out of a function. We can also jump out of a file using these functions. These functions are defined inside the header file setjmp.h. Consider the program given below.

#include <stdio.h>
#include <setjmp.h>

jmp_buf buf;
 

void fun()
{
    printf("\nYou are in function fun\n");
    longjmp(buf,1);
    printf("\nYou will never see this, because of longjmp");
}
int main()
{
     if(setjmp(buf))
     {
        printf("\nYou are back in main\n");
     }
     else
     {
        printf("\nYou are in main\n");
        fun();
     }
     return 0;
}

Figure shows the Output of the Program

In the program setjmp(jmp_buf j) must be called first. It says use the variable j to remember where you are now and returns 0 from the call. Function longjmp(jmp_buf j, int i) can then be called. It says go back to the place where j is set, i.e., to the location of the setjmp( ). Function longjmp( ) returns the value of variable i to the function setjmp( ).