Most Essential C++ And Oracle Interview Questions Part – 7
What Is The Benefit Of Using Const For Declaring Constants Over #define?
The basic difference between them is that, a const variable is a real variable which has a datatype and it exists at run time, and it can’t be altered. But a macro is not a real variable, but it carries a constant value which replaces all the occurrences of that macro at the time of pre-processing.
What Is A Static Function?
Static function is a special type of function whose scope is limited to the source file where the function is defined and can not be used other than that file. This feature helps you to hide some functions and to provide some standard interface or wrapper over that local function.
Should A Function Contain A Return Statement If It Does Not Return A Value?
In C, void functions does not return anything. So it is useless to put a return statement at the end of the function, where the control will any way return to the caller function. But, if you want to omit some portion of the function depending upon the scenario, then this return statement is perfect to avoid further execution of that void function.
How Can You Pass An Array To A Function By Value?
An array can be passed to a function by value, by keeping a parameter with an array tag with empty square brackets(like []). From the caller function, just pass the array tag. For instance,
void func(int i[]) {..} /* parameter */
…
int k[10];
func(k); /* caller function */
Is It Possible To Execute Code Even After The Program Exits The Main() Function?
There is a standard C function named atexit() for this purpose that can be used to perform some operations when your program exiting. You can register some functions with atexit() to be executed at the time of termination. Here’s an example:
#include
#include
void _some_FUNC_(void);
int main(int argc, char** argv)
{
…
atexit(_some_FUNC_);
….
}
What Does A Function Declared As Pascal Do Differently?
In C, when some function is called, the parameters are put at the top of the stack. Now the order in which they are put is the order in which the parameters are parsed. Normally, the order is right to left. That is, the right most is parsed first and the left most parameter is parsed at last.
If you want to alter this paradigm, then you have to define the function with PASCAL as following:
int PASCAL pascal_func(int, char*, long);
Here, the left most parameter(int) will be parsed first, then char* and then long.
Why Does Pascal Matter? Is There Any Benefit To Using Pascal Functions?
The main reason behind using PASCAL is that, in the left-to-right parsing the efficiency of switching increases in C.
Is Using Exit() The Same As Using Return?
No. They are not the same. Return statement returns control to the caller function, that is, it exits from the lowest level of the call stack. Where as, exit statement make the program returns to the system from where the application was started. So, exit always exits from the highest level of call stack. Eventually, if there is only one level of function call then they both do the same.
In C, What Is The Difference Between A Static Variable And Global Variable?
A static variable ia declared outside of any function and it is accessible only to all the functions defined in the same file (as the static variable). In case of global variable, it can be accessed by any function (including the ones from different files).
Write A C Program To Swap Two Variables Without Using Third Variable ?
#include
int main()
{
int a=5,b=10;
a=b+a;
b=a-b;
a=a-b;
printf(“a= %d b= %d”,a,b);
}
What Is Dangling Pointer In C?
If any pointer is pointing at the memory location/address of any variable, but if the variable is deleted or does not exist in the current scope of code, while pointer is still pointing to that memory location, then the pointer is called dangling pointer. For example,
#include
int *func();
int main()
{
int *ptr;
ptr=func();
printf(“%d”,*ptr);
return 0;
}
int * func()
{
int x=18;
return &x;
}
Output is Garbage value, since the variable x has been freed as soon as the function func() returned
What Is Wild Pointer In C?
A pointer is known as wild pointer c, if it has not been initialized. For Example:
int main()
{
int *ptr;
printf(“%un”,ptr);
printf(“%d”,*ptr);
return 0;
}
Output:
Any address, Garbage value.
Here ptr is wild pointer, because it has not been initialized. Wild pointer is not the same as NULL pointer. Because, NULL pointer doesn’t point to any location, but a wild pointer points to a specific memory location but the memory may not be available for current application, which is very fatal.
What Is The Meaning Of Prototype Of A Function?
Declaration of function is known as prototype of a function. Prototype says the name, parameter list and return type of a function but not the definition, this is same as declaring some variable but not defining it. For example,
int printf(const char *, int/);
Write A C Program To Find Size Of Structure Without Using Sizeof Operator?
struct XXX
{
int x;
float y;
char z;
};
int main()
{
struct XXX *ptr=(struct XXX *)0;
ptr++;
printf(“Size of structure is: %d”,*ptr);
return 0;
}
What Is Size Of Void Pointer?
Size of all pointers are same in C, regardless of their type because pointers variable holds a memory location. And for a given system, this size is constant. The type of pointer is used to know the size of the data that the pointer is pointer is pointing to.
How Do You Use A Pointer To A Function?
The hardest part about using a pointer-to-function is declaring it. Consider an example. You want to create a pointer, pf, that points to the strcmp() function. The strcmp() function is declared as shown below
int strcmp( const char *, const char * )
To set up “pf” to point to the strcmp() function, you want a declaration that looks just like the strcmp() function’s declaration, but that has *pf rather than strcmp:
int (*pf)( const char *, const char * );
Notice that you need to put parentheses around *pf.