Showing posts with label Programs. Show all posts
Showing posts with label Programs. Show all posts

Thursday, August 19, 2010

main() function called inside main() definition --- C Language

Many think that when the main() function call inside the main(), recursion goes on infinite times. But it is not true as such. It is always  important to know how function calling and recursion works. The function calls work with the help of a stack by PUSHing all the functions to a stack and POPing the functions from the stack when they are executed. The following figure gives the complete picture of this working. When a function "func1()" is called from within the "main()" method, the "func1()" is PUSHed onto the stack. Similar is the case with the function "func2()". When the function "func2()" gets completed, it is POPed from the stack. The function calls take place until the stack overflow occurs, that is until the stack is completely filled with the functions. This implies that the recursion without a suitable condition takes place not infinite times but stack size times.
   

This particular experiment when conducted, compiles successfully but it suffers from a run time error.

Lets have a look at the functionality of this entire concept and thus decide the output of the program. We also experiment on the number of times the function is called. The entire simulation is with the help of a simple program as given below:

#include "stdio.h"
int i=0;
int main()
{
            i++;
            printf(" %d\n",i);
            main();           /* Call main() inside main() */
            return 0;
}

Now,in the above program the value of "i" signifies the number of times the function is called.

First, I considered running this program on Turbo C on Windows platform, where the size of a variable of type "int" is 2 bytes. When I ran this program, the function got called around 15000 times and then the compiler crashed with the error message in the image shown below:
The next time I tried the same program on GNU C compiler on Ubuntu 10.04, where the size of a variable of type "int" is 4 bytes. Now when the program is compiled, it returned successful compilation.



But when it is run, a runtime error was issued as "Segmentation fault". The error message in the dialog box for Turbo C also indicated the same thing.

Now what is this segmentation fault? It is an error that occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed. It occurred in this case due to the stack overflow. So, main() called inside main() gives segmentation fault.

The number of times the function call took place varies continuously between two executions of the program. In Turbo C, the number of times the function call took place is around 16000 while in GNU C Compiler, this figure is around 327000.

Friday, July 30, 2010

Java Database Connectivity with MS Access --- Part 3

Now lets see how the program goes: I have considered a simple Java program that lists all the students in the "StudentInfo" database. The program is given below:

import java.sql.*;
public class AccessData {
    public static void main(String args[])
    {
        try
        {
            // Load the driver
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

            // Database Connectivity
            Connection con = DriverManager.getConnection("jdbc:odbc:StudentInfo");
            System.out.println("Connected to MS Access");

            // Running a query
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery("select StudentID,Name from StudentInfo");
            while(rs.next()){
                System.out.print(rs.getInt(1)+"::");
                System.out.println(rs.getString(2));
            }           
            rs.close(); // Close the ResultSet
            st.close(); // Close the statement
        }
        catch(Exception e)
        {
            System.out.print(e.getMessage());
        }
    }
}
The output would look like this:

Saturday, July 24, 2010

C Program to print your name without using semicolon

I just wondered if I could really write this program. But on rethinking, I found it to be easy. Given below is the C program to print name without using semicolon:


int main()
{
          if(printf("Sai Ravi Kiran \n"))
          {
          }
}


A silly mistake such as omitting the curly braces in the empty "if" block would prove expensive as it is a compilation time error.
Related Posts Plugin for WordPress, Blogger...