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.