Tuesday, February 21, 2012

What is the purpose of farmalloc() and farfree() ?

Ans :-


farmalloc() allocates a block of memory from the far heap. Using this function , blocks larger than 64K can also be allocated. Far pointers are used to access the allocated blocks.

farfree() releases a block of memory previously allocated from the far heap.

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

C Program to find factorial using recursion with no new function defined

#include<stdio.h>
void main()
{
    static int var=5;
    static int result=1;
    result*=var;
    printf("\n %d",var--);
    printf("\n Result is %d",result);

    if(var)
    {
        main();
    }
    return;
}

What are volatile variables ?

The volatile keyword acts as a DATA TYPE QUALIFIER . It alters the default way in which the compiler handles the variable and does not attempt to optimize the storage referenced by it.

volatile means the sorage is likely to change anytime by code outside the control of the user program. This means that if you reference a variable, the rogram should always read from the physical address and not its cached value.

A volatile keyword is an instruction to the optimizer to make sure that the variable or function is not optimized during compilation.

eg :
         
...
...
int flag=1;
while(flag);
...
...

Here , on compilation the compiler assumes that the value of the flag won't be changed during  the execution of the program. So the compiler is free to ignore the while(flag) loop instructions during optimization. However, if the flag variable is changed outside this program control ( say by an interrrupt routine or by some other thread) then it is advisable to declare the flag as a volatile variable.


Tuesday, February 7, 2012

What are STATIC variables ??

The storage class, static , has a lifetime lasting the entire program. static storage class can be specified for automatic (local) as well as global variables.

Static automatic variables continue to exist even after the block in which they are defined terminates. Thus , the value of a static variable in a function is retained between repeated function calls to the same function. 

Static variables are allocated on the heap. The scope of static automatic variables is identical to that of automatic (local) variables,i.e. it is local to the block in which it is defined; however the storage allocated becomes permanent for the duration of the program.

Static variables may be initialized in their declarartions ; however the initializers must be constant expressions , and initialization is done only once at compile time when memory is allocated for the static variable.

The scope of a static global variable is only within the file which it is declared. A user cannot use extern in a different file and access the static global variable.

Sunday, February 5, 2012

How do you access command-line arguments ?

A Program is started by the operating system calling a program's main() function .
main() function is the only function in C that can be defined in multiple ways. It can take no arguments , two arguments or three arguments 

The two and three argument forms allow it to receive arguments from the shell ( command-line). The two argument form takes an int and an array of strings. When defining main() function arguments any name can be given but it is convention to call them argc and argv[].

The first argument (argc) holds a count of how many elements there are in the array of strings passed as the second argument(argv). The array is always null terminated so argv[argc]=NULL.

e.g :
          
int main( int argc, char *argv[])
{
         int i;
         for(i=0;i<argc;i++)
         printf("argv[%d] == %d\n",i,argv[i]);
         return 0;
}


The integer,argc, is the argument count(hence argc). It is the number of arguments passed into the program from the command line, including the name of the program.
The array of character pointers is the listing of all the arguments. argv[0] is the name of the program. After that , every element number less than argc is command line arguments. You can use each argv element just like a string , or use argv as a two dimensional aray.

               


What are the #pragma statements ?

Each implementation of C and C++ supports some features unique to its host machine or operating system. Some programs, for instance , need to exercise precise control over the memory areas where data may be stored or to control the way certain functions receive parameters.

The #pragma directives offer away for each compiler to offer machine and operating system-specific features while retaining overall compatibility with the C and C++ languages. Pragmas are machine or operating system-specific by definition, and are usually different for every compiler.

Pragmas can be used in conditional ststements , to provide new preprocessor functionality, or to provide implementation-defined information to the compiler.

The `#pragma' directive is the method specified by the C standard for providing additional information to the compiler, beyond what is conveyed in the language itself. Three forms of this directive (commonly known as pragmas) are specified by the 1999 C standard. A C compiler is free to attach any meaning it likes to other pragmas.

What is the meaning of #include ?

If a line starts with a hash, denoted by #, kit tells the compiler that a command should be sent to the C PREPROCESSOR. The C preprocessor is a program that is run before compilation takes place ( hence the name). 

Basically, when the preprocessor finds #include it looks for the file specified and replaces #include with the contents of that file. This makes the code more readable and easier to maintain if you needed to use common library functions.


Header files have the extension .h and the full filename follows from the #include directive . They contain functions that you may or may not have used in your program.

for e.g, the stdio.h file is required if you have used functions like printf() and scanf() in your program.

What is the difference between #include <...> and #include "..."

There are two ways to include a header file :

#include "stdio.h"
#include<stdio.h>

If you use the double quote marks , it  means that the directory you're currently in will be searched first , for the header file , before any other directories( mentioned in the INCLUDE_PATH ) are searched.

When you use the angled brackets , directories other than the one you're currently in , will be searched for the header file. The system dependent directories are searched. Usually this will be the default directory for header files specified in your compiler, so you'll probably be using square brackets all the time.