Tuesday, February 21, 2012

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")
   {}
}