C Language Tutorial Home

C Language Top Interview Questions

Question:1 to 20
Question:21 to 40
Question:41 to 60
Question:61 to 80
Question:81 to 100

C Language Basic Questions

Question:1 to 20              Question:21 to 40

Question:41 to 60            Question:61 to 80

Question:81 to 100

Question: #21) Compare local variables and global variables.

Sol:

Following are the various differences between the local and global variables:

1. Declaration
Local variables are declared inside a function, while global variables are the variables declared outside the functions.

2. Life
The life of a local variable is tied with the function block where it is declared. The variable is destroyed when the function exits. Global variables remain for the entire lifetime of the program.

3. Scope
The scope of a local variable is confined to the function or code block where it is declared. Global variables have global scope, i.e., they are available throughout the program.

4. Storage
Unless specified explicitly, local variables are stored in a stack. The storage for a global variable is decided by the compiler itself.

Question: #22) What is a static variable? Why do we need it?

Sol:

A variable whose value is fixed and can’t be changed during program execution, i.e., it retains the stored value between multiple function calls, is called a static variable. The keyword static represents a static variable. A static variable is initially initialized to 0. When the value is updated, it gets assigned to the static variable. It is initialized only once in the memory heap. We use a static variable to:

Reduce memory consumption
Sharing a common value across all functions

Question: #23) Please explain recursion in C.

Sol:

Recursion is the process when a function calls itself, directly or indirectly. Such a function is called a recursive function. There are two phases involved with a recursive function:

Winding phase – It starts when the recursive function calls itself and ends once the condition is reached.
Unwinding phase – Starts when the condition is reached, i.e., when the winding phase ends, and ends when the control returns to the original call.

Question: #24) What is a far pointer in C?

Sol:

A far pointer is a 32-bit pointer capable of accessing all the 16 segments, i.e., the whole residence memory of RAM. It can access information outside the computer memory in a given segment. To use the far pointer, it is required to:

Allocate the sector register to store data address in the segment, and
Store another sector register within the most recent sector
Question: Please explain the auto keyword in C.
Answer: auto is the default storage class of all the variables declared inside a code block or function. Hence, local variables can also be referred to as automatic or auto variables. If no value is stored in an auto variable, then it gets a garbage value. Auto variables are called so because these variables allocate and deallocate memory upon entering and exiting the code block or function in which they are declared, respectively. Typically, there is no need to mention the auto keyword explicitly.

Question: #25) Why do we use the sprintf() function?

Sol:

The sprintf() function is called string print. We use the sprintf() function to store the output on a character buffer specified in the function, i.e., transferring data to the buffer. The general syntax of the function is:

int sprintf
(char *str, const char*string,…..);
The sprintf() function returns the total number of characters present in the string. Here is an example demonstrating the use of the sprintf() function:

#include <stdio.h>
#include <conio.h>
int main()
{
char buffer[25];
sprintf(buffer, “This string is now stored in the buffer.”); /* Using the sprintf() function for storing the string in the buffer.*/
printf(“%s”, buffer);
int n = sprintf(buffer, “This string is now stored in the buffer.”);
printf(“\nThe total number of characters in the string is %d.”, n); // The sprintf() function returns the total number of characters in the stored string.
return 0;
}
Output: This string is now stored in the buffer.

The total number of characters in the string is 40.

Question: #26) Tell the difference between getch() and getche() functions.

Sol:

Both getch() and getche() functions are used for reading a single character from the keyboard. The difference between the two, however, lies in terms of displaying the output. The getche() function displays the data, the entered character, on the output screen while the getch() function doesn’t. Use Alt+F5 to see the entered character.


Question: #27) Please define typecasting.

Sol:

Typecasting is the process of converting one data type into another. It is of two types:

1. Implicit type casting – Also known as an automatic conversion, implicit type conversion is performed automatically by the C compiler, i.e., it doesn’t require a casting operator. For example:

#include <stdio.h>
#include <conio.h>
void main ()
{
int x = 22;
float b = x; //implicit type conversion
printf(“%f”, b);
}
Output: 22.000000

2. Explicit type casting – Unlike implicit type conversion, explicit type casting is performed by the programmer. A type casting operator is used for telling the compiler to convert (cast) one data type into another. For example:

#include <stdio.h>
#include <conio.h>
void main ()
{
float x = 22.22;
int b = (int) x; //explicit type conversion
printf(“%d”, b);
}
Output: 22

Here, (int) is the typecasting operator.

C Language Programming Tutorial

C Language Tutorial Home    Introduction to C Language     Tokens     If Condition     goto statement and Labelname     Switch Statements     For loop     While Loop    Do while loop     break and continue    Functions    Recursion    Inbuild Functions    Storage Classes    Preprocessor    Arrays    Pointers     Structures and Unions    File Handling     Projects

Tutorials

Technical Questions

Interview Questions

C Programming
C++ Programming
Class 11 (Python)
Class 12 (Python)
C Language
C++ Programming
Python

C Interview Questions
C++ Interview Questions
C Programs
C++ Programs