Monday, July 30, 2018

C Questions And Answers – 2018B6

There are many commonly asked questions regarding C programming language. Below are some collected such question-answer examples. The questions are usually related with 32-bit system, Turbo C IDE in windows or GCC under Linux environment [not always].

For more such examples, click C_Q&A label.

-------------------------------------------------------------------------------------------------

 

What is a static function?

 

A static function is a function whose scope is limited to the current source file. Scope refers to the visibility of a function or variable. If the function or variable is visible outside of the current source file, it is said to have global, or external, scope. If the function or variable is not visible outside of the current source file, it is said to have local, or static, scope.

 

-------------------------------------------------------------------------------------------------

 

What is a pointer variable?

 

A pointer variable is a variable that may contain the address of another variable or any valid address in the memory.

 

-------------------------------------------------------------------------------------------------

 

What is a pointer value and address?

 

A pointer value is a data object that refers to a memory location. Each memory location is numbered in the memory. The number attached to a memory location is called the address of the location.

 

-------------------------------------------------------------------------------------------------

 

What is a modulus operator? What are the restrictions of a modulus operator?

 

A Modulus operator gives the remainder value. The result of x%y is obtained by (x-(x/y)*y). This operator is applied only to integral operands and cannot be applied to float or double.

 

-------------------------------------------------------------------------------------------------

 

Differentiate between a linker and linkage?

 

A linker converts an object code into an executable code by linking together the necessary build in functions. The form and place of declaration where the variable is declared in a program determine the linkage of variable.

 

-------------------------------------------------------------------------------------------------

 

What is a function and built-in function?

 

A large program is subdivided into a number of smaller programs or subprograms. Each subprogram specifies one or more actions to be performed for a large program. such subprograms are functions.

 

The function supports only static and extern storage classes. By default, function assumes extern storage class. functions have global scope. Only register or auto storage class is allowed in the function parameters. Built-in functions that predefined and supplied along with the compiler are known as built-in functions. They are also known as library functions.

 

-------------------------------------------------------------------------------------------------

 

Why should I prototype a function?

 

A function prototype tells the compiler what kind of arguments a function is looking to receive and what kind of return value a function is going to give back. This approach helps the compiler ensure that calls to a function are made correctly and that no erroneous type

conversions are taking place.

 

-------------------------------------------------------------------------------------------------

 

What is Polymorphism ?

 

'Polymorphism' is an object oriented term. Polymorphism may be defined as the ability of related objects to respond to the same message with different, but appropriate actions. In other words, polymorphism means taking more than one form. Polymorphism leads to two important aspects in Object Oriented terminology – Function Overloading and Function Overriding. Overloading is the practice of supplying more than one definition for a given function name in the same scope. The compiler is left to pick the appropriate version of the function or operator based on the arguments with which it is called. Overriding refers to the modifications made in the sub class to the inherited methods from the base class to change their behavior.

 

-------------------------------------------------------------------------------------------------

 

What is Operator overloading?

 

When an operator is overloaded, it takes on an additional meaning relative to a certain class. But it can still retain all of its old meanings.

 

Examples:

1) The operators >> and << may be used for I/O operations because in the header, they are overloaded.

2) In a stack class it is possible to overload the + operator so that it appends the contents of one stack to the contents of another. But the + operator still retains its original meaning relative to other types of data.

 

-------------------------------------------------------------------------------------------------

 

What is the difference between goto and longjmp() and setjmp()?

 

A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution.

 

Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.

 

A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.

 

When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.

 

However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for

every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient.

 

It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.

-------------------------------------------------------------------------------------------------

 

…till next post, bye-bye & take care.

No comments:

Post a Comment