Saturday, November 10, 2012

preincreament and postincreament issue in printf("%d%d%d%d%d",i++,i--,++i,--i,i);

look at this sample code :

void main()
{
      int i=5;
      printf("%d%d%d%d%d", i++, i--, ++i, --i, i);
}

Answer: 45545

Explaination:

The arguments of a function (here printf) are pushed on to the stack from left to right, before evaluation. thus the stack will look like:

top             i
               --i
              ++i
              i--
bottom   i++

Now evaluation will start by poping up each expression from stack and result of expression will be send to its corresponding %d of printf. ie:

> when " i " will be popped its value (ie 5 here in this example)will be send to the 5th %d of printf (since this expression is 5th argument in printf function)

> when " --i " will be popped, since it is predecrement thus decrement to " i " will be done first ie 4 and now send to 4th %d of printf (since this expression is 4th argument in printf function) .

>when " ++i " will be popped , since it is preincrement thus increment to " i " will be done first ie 5 and now send to 3rd %d of printf. (since this expression is 3rd argument in printf function)

>when " i-- "  will be popped , since it is postdecrement thus current value of  " i " ie 5 will be send to 2nd %d of printf (since this expression is 2nd argument in printf function)   and now decrement to " i " will be done ie " i " will be now 4.

>when " i++ "  will be popped , since it is postincrement thus current value of  " i " ie 4 will be send to Ist %d of printf (since this expression is Ist argument in printf function)   and now increment to " i " will be done ie " i " will be now 5(but of no use now since stack is empty).

After stack becomes empty the display task is done by printf ie now the result will be 45545.




lvalue and rvalue


An object is a region of storage that can be examined and stored into. An lvalue is an expression that refers to such an object. An lvalue does not necessarily permit modification of the object it designates. For example, a const object is an lvalue that cannot be modified. The term modifiable lvalue is used to emphasize that the lvalue allows the designated object to be changed as well as examined. The following object types are lvalues, but not modifiable lvalues:
  • An array type
  • An incomplete type
  • const-qualified type
  • An object is a structure or union type and one of its members has a const-qualified type
Because these lvalues are not modifiable, they cannot appear on the left side of an assignment statement.
The term rvalue refers to a data value that is stored at some address in memory. An rvalue is an expression that cannot have a value assigned to it. Both a literal constant and a variable can serve as an rvalue. When an lvalue appears in a context that requires an rvalue, the lvalue is implicitly converted to an rvalue. The reverse, however, is not true: an rvalue cannot be converted to an lvalue. Rvalues always have complete types or the void type.

Sunday, October 14, 2012

loading images in mysql database


import java.io.*;
import java.sql.*;
class InsertImages{
    public static void main(String[] args) 
    {
    try{
    InputStream sImage;
    PreparedStatement pre=null;
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/emp", "root", "root" );
    String imagename[]={"c:/1.jpg","c:/2.jpg","c:/3.jpg","c:/4.jpg"};
    File imgfile[] = new File[4];
    for(int i=0;i<imgfile.length;i++){
        imgfile[i]=new File(imagename[i]);
        FileInputStream fin = new FileInputStream(imgfile[i]);
      pre = con.prepareStatement("insert into myfile( file_name, file_data) values ( ?, ?)");
      pre.setString(1,imgfile[i].getName());
      pre.setBinaryStream(2,fin,(int)imgfile[i].length());
      pre.executeUpdate();
    }
      System.out.println("Inserting Successfully!");
      pre.close();
      con.close();
}
catch(Exception ex){
System.out.println("error :"+ex);
}

    }
}