Viva Questions (C Programming)
Viva Questions (C Programming)
Ans: The Datatypes in C Language are broadly classified into 4 categories. They are as follows:
• Basic Datatypes
• Derived Datatypes
• Enumerated Datatypes
• Void Datatypes
Ans: A Pointer in C Programming is used to point the memory location of an existing variable.
In case if that particular variable is deleted and the Pointer is still pointing to the same memory
location, then that particular pointer variable is called as a Dangling Pointer Variable.
Q3. What do you mean by the Scope of the variable? What is the scope of the
variables in C?
Ans: Scope of the variable can be defined as the part of the code area where the variables
declared in the program can be accessed directly. In C, all identifiers are lexically (or statically)
scoped.
Ans: The variables and functions that are declared using the keyword Static are considered as
Static Variable and Static Functions. The variables declared using Static keyword will have their
scope restricted to the function in which they are declared.
Q5. Differentiate between calloc() and malloc()
Ans: calloc() and malloc() are memory dynamic memory allocating functions. The only
difference between them is that calloc() will load all the assigned memory locations with value 0
but malloc() will not.
Q6. What are the valid places where the programmer can apply Break Control
Statement?
Ans: Break Control statement is valid to be used inside a loop and Switch control statements.
Ans: To store a negative integer, we need to follow the following steps. Calculate the two’s
complement of the same positive integer.
Ans: The Parameters which are sent from main function to the subdivided function are called
as Actual Parameters and the parameters which are declared a the Subdivided function end are
called as Formal Parameters.
Ans: The program will be compiled but will not be executed. To execute any C program, main ()
is required.
Ans: When a data member of one structure is referred by the data member of another function,
then the structure is called a Nested Structure.
Ans: Keywords, Constants, Special Symbols, Strings, Operators, Identifiers used in C program
are referred to as C Tokens.
In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.
Ans: C introduced many core concepts and data structures like arrays, lists, functions,
strings, etc. Many languages designed after C are designed on the basis of C Language. Hence, it
is considered as the mother of all languages.
Ans: printf() is used to print the values on the screen. To print certain values, and on the other
hand, scanf() is used to scan the values. We need an appropriate datatype format specifier for
both printing and scanning purposes. For example,
• %d: It is a datatype format specifier used to print and scan an integer value.
• %s: It is a datatype format specifier used to print and scan a string.
• %c: It is a datatype format specifier used to display and scan a character value.
• %f: It is a datatype format specifier used to display and scan a float value.
Ans. The array is a simple data structure that stores multiple elements of the same datatype in a
reserved and sequential manner. There are three types of arrays, namely,
Ans: The Symbol mentioned is called a Null Character. It is considered as the terminating
character used in strings to notify the end of the string to the compiler.
Q17. What is the main difference between the Compiler and the Interpreter?
Ans: Compiler is used in C Language and it translates the complete code into the Machine Code
in one shot. On the other hand, Interpreter is used in Java Programming Language and other
high-end programming languages. It is designed to compile code in line by line fashion.
Ans: Dynamic Memory Allocation is the process of allocating memory to the program and its
variables in runtime. Dynamic Memory Allocation process involves three functions for
allocating memory and one function to free the used memory.
Syntax:
Syntax:
Syntax:
Syntax:
1 free(ptr);
Q20. What do you mean by Dangling Pointer Variable in C Programming?
Ans: A Pointer in C Programming is used to point the memory location of an existing variable.
In case if that particular variable is deleted and the Pointer is still pointing to the same memory
location, then that particular pointer variable is called as a Dangling Pointer Variable.
Ans: We cannot use & on constants and on a variable which is declared using the register
storage class.
Ans: Structure is defined as a user-defined data type that is designed to store multiple data
members of the different data types as a single unit. A structure will consume the memory equal
to the summation of all the data members.
Q23. Differentiate between call by value and call by reference.
Ans:
Ans: Both the functions are designed to read characters from the keyboard and the only
difference is that?
getch(): reads characters from the keyboard but it does not use any buffers. Hence, data is not
displayed on the screen.
getche(): reads characters from the keyboard and it uses a buffer. Hence, data is displayed on the
screen.
Ans. toupper() is a function designed to convert lowercase words/characters into upper case.
//Example
1
#include<stdio.h>
2
#include<ctype.h>
3 int main()
4 {
5 char c;
6 c=a;
printf("%c after conversions %c", c, toupper(c));
7
c=B;
8 printf("%c after conversions %c", c, toupper(c));
9
//Output:
a after conversions A
B after conversions B
1987384758
2057844389
3475398489
2247357398
1435983905
Ans: It is possible to create a new header file. Create a file with function prototypes that need to
be used in the program. Include the file in the ‘#include’ section in its name.
Ans: Memory Leak can be defined as a situation where programmer allocates dynamic memory
to the program but fails to free or delete the used memory after the completion of the code. This
is harmful if daemons and servers are included in the program.
1 #include<stdio.h>
2 #include<stdlib.h>
int main()
3
{
4 int* ptr;
5 int n, i, sum = 0;
6 n = 5;
7 printf("Enter the number of elements: %dn", n);
ptr = (int*)malloc(n * sizeof(int));
8
if (ptr == NULL)
9 {
10 printf("Memory not allocated.n");
11 exit(0);
12 }
else
13
{
14 printf("Memory successfully allocated using malloc.n");
15 for (i = 0; i<= n; ++i)
16 {
17 ptr[i] = i + 1;
18 }
printf("The elements of the array are: ");
19 for (i = 0; i<=n; ++i)
20 {
21 printf("%d, ", ptr[i]);
22 }
23 }
return 0;
24 }
25
26
27
28
29
//Output
In case you are facing any challenges with these C Programming Interview Questions, please
write your problems in the comment section below.
Ans: A local static variable is a variable whose life doesn’t end with a function call where it is
declared. It extends for the lifetime of the complete program. All calls to the function share the
same copy of local static variables.
1
2 #include<stdio.h>
3 void fun()
{
4
static int x;
5 printf("%d ", x);
6 x = x + 1;
7 }
8 int main()
{
9
fun();
10 fun();
11 return 0;
12 }
13
//Output
01
Q30. What is the difference between declaring a header file with < > and” “?
Ans: If the Header File is declared using < > then the compiler searches for the header file
within the Built-in Path. If the Header File is declared using” ” then the compiler will search for
the Header File in the current working directory and if not found then it searches for the file in
other locations.
Ans: We use Register Storage Specifier if a certain variable is used very frequently. This helps
the compiler to locate the variable as the variable will be declared in one of the CPU registers.
Ans: x++; is the most efficient statement as it just a single instruction to the compiler while the
other is not.
Q33. Can I declare the same variable name to the variables which have different
scopes?
Ans: Yes, Same variable name can be declared to the variables with different variable scopes as
the following example.
1
int var;
2
void function()
3 {
4 int variable;
5 }
6 int main()
{
7
int variable;
8 }
9
Q34. Which variable can be used to access Union data members if the Union
variable is declared as a pointer variable?
Ans: Arrow Operator (->) can be used to access the data members of a Union if the Union
Variable is declared as a pointer variable.
Ans: Basic File Handling Techniques in C, provide the basic functionalities that user can
perform against files in the system.
Function Operation
fopen() To Open a File
fclose() To Close a File
fgets() To Read a File
fprint() To Write into a File
• auto
• register
• static
• extern
Ans: Typecasting is a process of converting one data type into another is known as typecasting.
If we want to store the floating type value to an int type, then we will convert the data type into
another data type explicitly.
Q38. Write a C program to print hello world without using a semicolon (;).
Ans:
1 #include<stdio.h>
2 void main()
3 {
4 if(printf("hello world")){}
}
5
//Output:
hello world
Q39. Write a program to swap two numbers without using the third variable.
Ans:
1 #include<stdio.h>
#include<conio.h>
2 main()
3 {
4 int a=10, b=20;
5 clrscr();
6 printf("Before swapping a=%d b=%d",a,b);
a=a+b;
7 b=a-b;
8 a=a-b;
9 printf("nAfter swapping a=%d b=%d",a,b);
10 getch();
}
11
12
13
//Output
Q40. How can you print a string with the symbol % in it?
Ans: There is no escape sequence provided for the symbol % in C. So, to print % we should use
‘%%’ as shown below.
Ans: To print the above pattern, the following code can be used.
1
2 #include<stdio.h>
3 int main()
{
4
for(i=1;i<=5;1++)
5 {
6 for(j=1;j<=5;j++)
7 {
8 print("%d",j);
}
9
printf("n");
10 }
11 return 0;
12 }
13
Q42. What are the limitations of scanf () and how can it be avoided?
Ans: The differences between macros and functions can be explained as follows:
• Macro call replaces the templates with the expansion in a literal way.
• The Macro call makes the program run faster but also increases the program size.
• Macro is simple and avoids errors related to the function calls.
• In a function, call control is transferred to the function along with arguments.
• It makes the functions small and compact.
• Passing arguments and getting back the returned value takes time and makes the
program run at a slower rate.
Speed: C comes with support for system programming and hence it compiles and executes with
high speed when compared with other high-level languages.
Answer:
Answer: The words that are a part of the standard C language library are called reserved words.
Those reserved words have special meaning and it is not possible to use them for any activity
other than its intended functionality.
Answer: When there is a pointer pointing to a memory address of any variable, but after some
time the variable was deleted from the memory location while keeping the pointer pointing to
that location is known as a dangling pointer in C.
Answer: A function, which has a function definition prefixed with a static keyword is defined as
a static function. The static function should be called within the same source code.
Answer: Both functions are to retrieve absolute value. abs () is for integer values and fabs() is
for floating type numbers. Prototype for abs () is under the library file < stdlib.h > and fabs() is
under < math.h >.
Answer: Uninitialized pointers in the C code are known as Wild Pointers. They point to some
arbitrary memory location and can cause bad program behavior or program crash.
Q53. What is the difference between ++a and a++?
Answer: ‘++a” is called prefixed increment and the increment will happen first on a variable.
‘a++’ is called postfix increment and the increment happens after the value of a variable used for
the operations.
Answer: ‘==’ is the comparison operator which is used to compare the value or expression on
the left-hand side with the value or expression on the right-hand side.
‘=’ is the assignment operator which is used to assign the value of the right-hand side to the
variable on the left-hand side.
Answer: Prototype function is a declaration of a function with the following information to the
compiler.
In this example Name of the function is Sum, the return type is the integer data type and it
accepts two integer parameters.
Q56. What is the explanation for the cyclic nature of data types in C?
Answer: Some of the data types in C have special characteristic nature when a developer assigns
value beyond the range of the data type. There will be no compiler error and the value changes
according to a cyclic order. This is called cyclic nature. Char, int, long int data types have this
property. Further float, double and long double data types do not have this property.
Answer: The file containing the definitions and prototypes of the functions being used in the
program are called a header file. It is also known as a library file.
Example: The header file contains commands like printf and scanf is from the stdio.h library
file.
Q58. There is a practice in coding to keep some code blocks in comment symbols
than delete it when debugging. How this affects when debugging?
Answer: This concept is called commenting out and this is the way to isolate some part of the
code which scans possible reason for the error. Also, this concept helps to save time because if
the code is not the reason for the issue it can simply be removed from comment.
Q59. What are the general description for loop statements and available loop
types in C?
While loop
For Loop
Do…While Loop
Nested Loop
}
Return Type: Data type of the return value of the function.
Function Name: The name of the function and it is important to have a meaningful name that
describes the activity of the function.
Parameters: The input values for the function that are used to perform the required action.
Answer: A pointer variable that contains the address of another pointer variable is called pointer
on a pointer. This concept de-refers twice to point to the data held by a pointer variable.
Answer: The purpose of the Break keyword is to bring the control out of the code block which is
executing. It can appear only in looping or switch statements.