Monday, 14 May 2012

what is the difference between exit() function and break statement?

exit( ) function terminates the entire program and gives the control to operating system.

break statement terminates the block and control goes out of the block. generally, break statements are often used in switch case statements.

What is volatile specifier in C?

It is a keyword in C. When a variable declared with volatile specifier in C, indicates the compiler that the value of that variable can be changed at any time. So, compiler loads a copy of the value every time when the program uses that variable.
For Example:
Volatile int i = 10;
If( I ==12)
Printf(“%d”,i);
Else
Printf(“%d”,i);

In the above example, compiler loads the value 10 into variable I and while checking the if condition it loads a copy of value again from that memory location since it is declared as volatile. If it is not declared as volatile, then the compiler becomes blind it doesn’t know the changes made in that variable. Ex: Hardware registry on the PCs, Temperature Sensors. So, the value of i can be changed between the first two statements of the code, if it is it will change the conditional checking.

Difference between Definition and declaration?

Definition: It indicates the type and name of the variable or memory location to the compiler. So, compiler reserves memory space and name for the variable or function.

Declaration: It indicates initializing the values,addition of modifier, specifier etc. to the variable.

Declaring a variable without defining is illegal. It gives compile error.

difference between Scope and Lifetime?

Scope: It defines the visibility/accessibility of the variable in a program. For example take a local variable where the scope is local to that function.For static global variable,scope is limited to that file only. For global variable the scope is across multiple files (if it is with extern specifier)
Lifetime: It is the duration of time for which the variable holds the value during the execution of a program. For local variable the lifetime is within the functional block in which it is declared. The memory allocated when function starts and deallocated when it terminates. So,the lifetime of the variable defined in that block is local the functional block.
On the other side, for static variable, the life time will be the entire execution of the program. When the variable is used it takes the last stored value in that memory location. The value persists even between function calls. The life time is entire execution of the program.

Difference between const char* p and char const* p?

In const char* p, the character pointed by ‘p’ is constant, so u cant change the value of character pointed by p but u can make ‘p’ refer to some other location(you can change the address of the .

 In char const* p, the ptr ‘p’ is constant not the character referenced by it, so u cant make ‘p’ to reference to any other location but u can change the value of the char pointed by ‘p’. You can't change the value of the variable but can change the address.

C interview Questions:What is the difference between %d and %*d in c language?

%d give the original value of the variable and %*d give the address of the variable.
eg:-int a=10,b=20;
printf("%d%d",a,b);
printf("%*d%*d",a,b);
Result is 10 20 1775 1775 .Here 1775 is the starting address of the memory allocation for the integer.a and b having same address because of contagious memory allocation.

Can a variable become both constant and volatile?

 Yes. The const modifier implies that this particular program code cannot change the value of the actual variable, but that will not imply that the value cannot be changed by means outside this code.
For instance, within the example in frequently asked questions, the timer structure was accessed through a volatile const pointer. The function by itself did not change the value of the timer, so it had been declared const. However, the value had been changed by hardware on the computer, so it was declared volatile. If a variable is both equally const and volatile, the two modifiers can come in either order.

What is static function in C?

A static function is a function with the static qualifier applied:
static void foo ( void )
{
  /* Blah blah */
}
What it does is restrict visibility of the function to the translation unit in which it's declared. Functions are implicitly declared as extern by default, which means they're visible across translation units. You can compile the following, but it won't link:
/* file1.c */
void foo ( void )
{
}

extern void bar ( void )
{
}

static void baz ( void )
{
}
/* file2.c */
void foo ( void );
void bar ( void );
void baz ( void );

int main ( void )
{
  foo(); /* OK: foo is extern by default */
  bar(); /* OK: bar is explicitly extern */
  baz(); /* Wrong: baz isn't visible in this translation unit */
}
Static function are used to avoid the access of the function from the other module.
By default normal functions can access by any module from the file; to avoid we can use static key word to the function.
Also if you have more than one; same functions name across file and if they were in static we will not get any errors.

Difference Between static global and global varibale?

Static and global variable differ a lot in their behaviour to life and scope. First, let me distinguish between life and scope.
Life of an object determines whether the object is still in the memory (of the process) whereas scope of the object is whether can I know the variable by its name at this position. It is possible that object is live, but not visible (not in scope) but not that object is not alive but in scope (except for dynamically allocated objects where you refer object through pointers).

Static variables are local in scope to their module in which they are defined, but life is throughout the program. Say for a static variable inside a function cannot be called from outside the function (because it's not in scope) but is alive and exists in memory. The next time this function is entered (within the same program) the same chunk of memory would be accessed now retaining the variables old value and no new memory is allocated this time for this variable like other variables in the function (automatic variables). So basically the variable persists throughout the program. Similarly if a static variable is defined in a global space (say at beginning of file) then this variable will be accessible only in this file (file scope).

On the other hand global variables have to be defined globally, persists (life is) throughout the program, scope is also throughout the program. This means such variables can be accessed from any function, any file of the program.

So if you have a global variable and u r distributing ur files as a library and you want others to not access your global variable, you may make it static by just prefixing keyword static (of course if same variable is not required in other files of yours).

        Static Global Variable                    Global Variable
ScopeFile in which it is declaredAcross files if declared as extern
LifetimeEntire program executionEntire program Execution
Value Persists across the function callsPersists across the function calls
 Accessibility Only the file in which it is declared Any file or function of the program