Saturday, 9 March 2013

The near, far and huge Pointers

  I have read about the near, far and huge pointers in many text books. Most of them were not helpful. In this post I just want to give you some basic ideas about them.
  • The near, far and huge pointers are not part of the standard C Programming language. Even if you haven't heard about them, it doesn't make you less of a C programmer. 
  • They are only relevant in case of 16 bit Intel Architectures. So these pointers are mostly a relic of the past without any proper use nowadays. I still remember the time when I used some of the functions in Turbo C Compiler to copy the memory content. I was unsuccessful in my attempt since my system was having a 32 bit architecture. So these pointers are not useful with most of today's machines.
  • Only 16 bit Compilers like Turbo C++, Borland C++, etc. have support for these compilers nowadays.
  • They were mainly used to deal with segmented memory architectures.
far pointer
A far pointer is a 32-bit pointer and contains both a segment address and an offset address.

huge pointer
A huge pointer is also 32-bit pointer and contains both a segment address and an offset address. When a far pointer is incremented or decremented ONLY the offset of the pointer is actually incremented or decremented but in case of huge pointer both segment and offset value will change.

near pointer
A near pointer is a 16-bit pointer. The current content of the Code Segment or Data Segment is used as the segment part and near pointer contains the offset part.   

C/C++ Puzzles: PART - 27


  Recently I have prepared a Multiple Choice Question Paper for a Coding and Debugging contest. I thought it might be a good idea to post those questions here. Most of the questions and their solutions are already discussed in this blog.

  1.     #include<stdio.h>
        int main( )
       {
           int x;
           int *y;
           int **z;
           x=10;
           y=&x;
           z=&y;
           printf(“%d”,*z);
           return 0;
       }
             
A. Syntax Error       B. 10                        C. Address of y                              D. Address of x

 2.         #include<stdio.h>
        int main( )
        {
           enum tag {a, b=3, c};
           printf(“%d%d”,a,c);
           return 0;
        }

A.     Syntax Error                          B. 14                            C. 13                        D. 04

3.      #include<stdio.h>
      int main( )
      {
         int a[ ] = {1,2,3};
         int i=2;
         printf(“%d”,i[a]);
         return 0;
      }

A.     Address of i                        B. 2                            C. 3                           D. Syntax Error

4.  #include<stdio.h>
     int main( )
     {
        int a,b;
        float c;
        a=10;
        b=5;
        c=(a+b)/2;
        printf(“%f”,c);
        return 0;
     }

A.     7.000000                               B. 7.500000              C. 7                          D. Syntax Error

5.      #include<stdio.h>
      int main()
      {
           int a;
           int *b;
           a=10;
           b=&a;
           printf(“%d”,b);
           return 0;
      }

A.     10                    B. Address of a                     C. Syntax Error                    D. Address of b 

 6.   #include<stdio.h>
      int main( )
      {
         float a;
         a = 10.5;
         printf(“%d”,a);
         return 0;
      }
                               
A. 10.500000             B. 10.000000                        C. 0                                                D. 10

 7.  #include<stdio.h>
      int main( )
      {
         int ch=1;
         switch (ch)
         {
            case 1: printf(“1”);
            case 2: printf(“2”);
            case 3: printf(“3”);
         }
      }

A. 123                      B. Syntax Error                        C. 2                                 D. 1

8.   #include<stdio.h>
     int main()
     {
       int x,y,z;
       x=5;
       y=10;
       z=x+++++y;
       printf(“%d”,z);
       return 0;
     }

A. 10                        B. Syntax Error                     C. 15                                    D. 5

 9.   #include<stdio.h>
     #define SQR (x) x * x
     int main( )
     {
        int a;
        a = SQR (3+5);
        printf(“%d”,a);
        return 0;
     }

A. 64                                 B. 25                            C. 23                                D. Syntax Error
    
10.  #include<stdio.h>
       int main()
       {
           int x,y,z;
           x=10;
           y=20;
           z=x+++y;
           printf(“%d”,z);
           return 0;
       }

A. 30                               B. 31                              C. 32                                   D. Syntax Error

11.   #include<stdio.h>
       int main( )
      {
         int x;
         int *ptr;
         x=10;
         ptr=&x;
         (*ptr)++;
         printf(“%d”,x);
         return 0;
      }

A. 10                            B. 11                                   C. 12                              D. Syntax Error

12.   #include<stdio.h>
       int main( )
       {
           const char str [ ] = “hello”;
           str[0] = ‘H’;
           printf(“%s”,str);
           return 0;
       }
 
 A. Syntax Error            B. Hello                           C. hello                               D. ello

13.   #include<stdio.h>
       int a;
       int main( )
       {
          printf(“%d”,a);
          return 0;
       }

A. Compile Time Error            B. Run Time Error                  C. 0                     D. Garbage Value

14.   #include<stdio.h>
      int main( )
     {
         int i=-1;
         if(i< sizeof(int))
         {
              printf(“%d”,-i);
         }
         else
        {
              printf(“%d”,+i);
        }
        return 0;
     }

 A. 1                                B. Syntax Error                      C. -1                        D. 11    

15.   #include<stdio.h>
      int main( )
      {
          int a=10,b;
          b=sizeof(++a);
          printf(“%d%d”,a,b);
          return 0;
      }
     
A. 104                             B. 114                            C. 102                            D. Syntax Error

C/C++ Puzzles: PART - 26

Consider the C code given below.


#include<stdio.h>

int main( )
{
    int x=0;
    const int y=10;
    /* You Should only change the test condition of the while loop */
    while( )
    {
       printf("\nHello\n");
       x--;
    }
}


The task is to print the message "Hello" 10 times by filling the test condition of the while loop. But the following restrictions are applicable while filling the test condition.

1) You should only use the variables a and y.
2) Only operator can be used in the test condition.

Solution:-

If you haven't got the answer please select the Red Colour Field given below using your mouse.

    while(x+y)
    {
       printf("\nHello\n");
       x--;
    }



Differences Between Java and C

  In this post I am listing out some differences between Java and C. Of course it does not cover all the differences between Java and C.

  • Java is an Object Oriented Programming Language, where as C is a Procedural Programming Language.
  • Java does not have the C Keywords sizeof and typedef.
  • There are no structures and unions in Java. Consequently the C Keywords struct and union are absent in Java.
  • Type Modifiers auto, extern, register, signed and unsigned present in C are not there in Java.
  • Explicit pointers are not supported in Java.
  • There is no preprocessor for java. So statements like #define, #include etc are absent in Java.
  • In Java functions with no argument must be declared with empty parenthesis. In C this can be done by using the void keyword.
  • New operators such as >>> and instanceof are added in Java.
  • Java supports labelled breaks and continue statements.
  • New data types like boolean, reference etc. are added in Java. 
  • Many new keywords like private, protected, public etc are added in Java.

MySQL: Finding the Details of Maximum Elements in Every Group from a Table

  The table for our discussion is called emp and contains 4 fields. The fields are ENAME, ID, SAL and DEPT, Where ENAME stores the employee name, ID stores the employee ID, SAL stores the salary and DEPT stores the department. The table with sample data is shown below.


Sample Table

  
  The Task was to find the NAME, DEPARTMENT and SALARY of the employee having highest salary in each Department. I used the Query given below to solve the problem. 

select 
    ENAME, DEPT, Max(SAL)
from
    emp
group by DEPT;        # Wrong Query

But the output was wrong to my dismay. The incorrect output is shown below.

Wrong Output

I used many different Queries and finally got the correct query to solve the problem. The correct query and output is shown below.

 select 
    e.ENAME, e.DEPT, e.SAL
from
    emp e
        join
    (select 
        DEPT, MAX(SAL) MS
    from
        emp
    group by DEPT) m ON e.DEPT = M.DEPT and e.SAL = m.MS        # Correct Query


Correct Output


Friday, 8 March 2013

MySQL: Finding the Details of a Maximum Element

  Recently I have come across a table of data containing 4 fields. The fields are ENAME, ID, SAL and DEPT, Where ENAME stores the employee name, ID stores the employee ID, SAL stores the salary and DEPT stores the department. The table with sample data is shown below.

TABLE 1

  I was supposed to find out the name, department, and salary of the employee with maximum salary from the given table. I used the following query.

Select ENAME, DEPT, MAX(SAL) from employee;         # Wrong Query

To my horror, I received the following output when applied on Table 1.

Wrong Output


At first I was confused. But then I have referred Table 2 given below.

TABLE 2


  I understood the mistake after referring Table 2. The reason was very simple. Max( ) returns the maximum salary, but it doesn't retrieve any other data corresponding to the maximum value. In order to get the correct result use the following query. 

Select ENAME, DEPT, MAX(SAL) from employee where SAL in
(
Select MAX(SAL) from employee);        # Correct Query

The following correct output is obtained when the query is applied on Table 2
.
Correct Output

Sunday, 3 March 2013

C/C++ Puzzles: PART - 25

Peculiar behavior of Arrays

Consider the program given below. The program prints the array elements in different ways. How is it possible? 

#include<stdio.h>

int main( )
{
    int a[ ]={1,2,3,4,5};
    int i;
    for(i=0;i<5;i++)
   {
       printf("\n%d\t%d\t%d\t%d\n",a[i],i[a],*(a+i),*(i+a));
   }
   return 0;
}

Click here to download the C Program.


Figure Below Shows the Output


Explanation:-


  The content of an array a[i] is calculated as follows. *(Starting Address  + Array Index ), where a is the starting address and i is the array index. Since addition is a commutative operation a+b and b+a are the same, so a[i], i[a], *(a+i) and *(i+a) all returns the ith element of the array a.