Thursday, February 9, 2012

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.

Sunday, January 1, 2012

What IsDangling Pointer ?

DAnging Pointer :::::::
If any pointer is pointing the memory address of any variable , but after sometime the variable is deleted  while the pointer is still pointing such memory location. Such pointer is known as dangling pointer .



  









e.g :
  

#include<stdio.h>

int *call();
void main(){

int *ptr;
ptr=call();

fflush(stdin);
printf("%d",*ptr);

}
int * call(){

int x=25;
++x;

return &x;
}


Output :::::
 Garbage Value

variable x is local variable. Its scope and lifetime is within the function call hence after returning address of x variable x became dead and pointer is still pointing ptr is still pointing to that location.